Official Solution
This uses merge-sort tree.
Time Complexity:
Memory Complexity:
Alternative solution (Wavelet Tree)
Similar as K-query from SPOJ, but you additionally need to store prefix sums of .
Time Complexity: , where is the max value
Memory Complexity:
#include <bits/stdc++.h>using namespace std;struct PrefixSummer {const int BITS = 64;vector<uint64_t> packed;vector<int> psums;vector<int64_t> psumA;void init(const vector<bool> &v, vector<int> A) {packed.resize(size(v) / BITS + 1);
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!