Official Solution

Time Complexity: O((N+QlogN)logN)O((N+Q\log N)\log N)

Memory Complexity: O(NlogN)O(N\log N)

Alternative Solution 1 (Persistent Segment Tree)

The factor of Qlog2NQ\log^2N from the solution above can be reduced to QlogNQ\log N by replacing binary search with a segment tree walk.

This section is not complete.

Any help would be appreciated! Just submit a Pull Request on GitHub.

Alternative Solution 2 (Wavelet Tree)

Similar as the Wavelet tree module problem. All you need to change is the recursive query step.

Time Complexity: O(N+QlogN)O(N+Q\log N)

Memory Complexity: O(N)O(N)

#include <bits/stdc++.h>
using namespace std;
Code Snippet: BitVector Prefix Summer (Click to expand)
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!