// Persistent BIT
vector< pair<int,int> > bit[4100][4100];
// Add val to cell (x, y) at time = time
void update(int x, int y, int val, int time) {
for(int u = x; u <= 4096; u += _(u))
for(int v = y; v <= 4096; v += _(v)) {
if (bit[u][v].empty()) {
bit[u][v].push_back(make_pair(time, val));
}
else {
int cur = bit[u][v][bit[u][v].size()-1].second;
bit[u][v].push_back(make_pair(time, cur + val));
}
}
}
// Get the sum of square (1,1) --> (x, y) at time = time
int get(int time, int x, int y) {
int res = 0;
for(int u = x; u > 0; u -= _(u))
for(int v = y; v > 0; v -= _(v)) {
if (bit[u][v].empty()) {
}
else if (bit[u][v][bit[u][v].size()-1].first <= time) {
res += bit[u][v][bit[u][v].size()-1].second;
}
else {
int pos = upper_bound(bit[u][v].begin(), bit[u][v].end(), make_pair(time, 2000111000))
- bit[u][v].begin() - 1;
if (pos >= 0)
res += bit[u][v][pos].second;
}
}
return res;
Comments
This comment is hidden due to too much negative feedback. Click here to view it.