Problem solution · C++

CCC 2025 S5 - To-do List

CCC 2025 S5 - To-do List: a C++ solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Segment tree or range structure
Source
CCCSolutions
Length
79 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Segment tree or range structure

For CCC 2025 S5 - To-do List, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. Apply updates and combine the affected nodes to answer each query.

Code notes

  • 79 lines of C++ from the credited upstream file ccc25s5.cpp.
  • The implementation visibly relies on sequence storage.
  • 2 loop blocks detected.

Complexity

Count the build once, then multiply the logarithmic update or query path by the number of operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2025 S5 - To-do List · C++C++
Use this to learn the idea, then write your own version.
// By Daniel Zhang, Pinetree Secondary // Idea - maintain a segment tree where each node stores the latest time possible to complete all the tasks// in as little time as possible, which is the sum of the times of the tasks #include <bits/stdc++.h> using namespace std; typedef long long ll; struct Node {    ll begin, time;}; const ll MAXN = 1000025;Node tree[4*MAXN];ll arr[MAXN];vector<pair<ll, ll>> hw; Node recalculate(ll node) {    Node l = tree[2 * node + 1], r = tree[2 * node + 2];    if(l.begin == INT_MAX) return r;  // left is empty    if(r.begin == INT_MAX) return l;  // right is empty     return {max(l.begin + l.time, r.begin) - l.time, l.time + r.time};}  void update(ll node, ll left, ll right, ll index, ll value) {    if (left == right) {        if (value == 0) {            tree[node] = {INT_MAX, INT_MIN};        }        else tree[node] = {index, value};    }    else {        ll middle = (left + right) / 2;        if (index <= middle) update(node * 2 + 1, left, middle, index, value); // update left branch        else update(node * 2 + 2, middle + 1, right, index, value);        tree[node] = recalculate(node);    }} ll ans = 0;const ll mod = 1e6 + 3; signed main() {    ios::sync_with_stdio(0); cin.tie(0);     ll q; cin >> q;     for (ll i=0; i<4*MAXN; i++) tree[i] = {INT_MAX, INT_MIN};     for (ll i=0; i<q; i++) {        char command; cin >> command;        if (command == 'A') {            ll s, t; cin >> s >> t;             s = (s + ans - 1 + mod) % mod; t = (t + ans) % mod;            arr[s] += t;            update(0, 0, MAXN, s, arr[s]);            hw.push_back({s, t});        }         else {            ll idx; cin >> idx;             idx = (idx + ans - 1) % mod;            ll s = hw[idx].first;            ll t = hw[idx].second;            arr[s] -= t;            update(0, 0, MAXN, s, arr[s]);        }         ans = tree[0].begin + tree[0].time;        cout << ans << "\n";    }}  

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗