Hi, I’m Nathan, the leader of the Computer Programming (CP) Club at St. George’s, and here is a breakdown of my approach to the “Vera and Modern Art” (CCO ’17) problem.
Here is a concise summary of the problem and my approach:
Vera paints on an infinite grid. Each paint operation starts at a point (x, y) and repeats upward and outward in steps equal to the largest power of two that fits in x and y. After up to 200,000 of these operations, the program has to answer up to 200,000 questions of the form “what is the total color at this point?”, with coordinates as large as 10¹⁸.
The key idea is to write the coordinates in binary. A point is covered by a paint operation exactly when its binary form ends with the same digits as the paint’s starting point, once the starting point’s leading 1 is removed. That turns the problem into matching binary endings, which the solution handles with a “trie of tries”: one tree for the x-coordinate, where each node holds a second tree for the y-coordinate. Each update and query then takes only about 60 steps per coordinate, however large the numbers get.
If you have any questions, feel free to reach out to me at nathan.dai27@stgeorges.bc.ca.
Here is my complete solution:
<bits/stdc++.h> <iostream>using namespace std;const int MAXN = 2e5+5;const int M = 65;typedef long long ll;//m is max length = should be log2(1e18) = ~60//represent all paints x and y in binary//any number affected by the paint will share a common suffix//use trie to queryint n,q, tot = 1;ll tr[MAXN*M*2][2], sum[MAXN*M*2];int msb(ll x){ return 64-__builtin_clzll(x);}void update(ll x, ll y, int val){ int u = 1; int t; for(int i = 0; i < msb(x)-1; i++) { t = (x>>i)&1; if(!tr[u][t]) tr[u][t] = ++tot; u = tr[u][t]; } if(!sum[u]) sum[u]= ++tot; u = sum[u]; for(int i = 0; i < msb(y)-1; i++){ t = (y>>i)&1; if(!tr[u][t]) tr[u][t] = ++tot; u = tr[u][t]; } sum[u] += val; }int query(ll x, ll y){ int ans = 0; int u = 1; for(int i = 0; u && i < msb(x); i++){ int v = sum[u]; for(int j = 0; v && j < msb(y); j++){ ans += sum[v]; v = tr[v][(y>>j)&1]; } u = tr[u][(x>>i)&1]; } return ans; }int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> q; while(n--){ ll x,y; int c; cin >> x >> y >> c; update(x,y,c); } while(q--){ ll x,y; cin >> x >> y; cout << query(x,y) << "\n"; }}



