5
void update(int tind, int tl, int tr, int ind, int val) {11
int tm = tl + ((tr - tl) >> 1), left = tind << 1;13
update(left, tl, tm, ind, val);15
update(left | 1, tm + 1, tr, ind, val);16
st[tind] = st[left] + st[left | 1];19
int query(int tind, int tl, int tr, int ql, int qr) {20
if (tl > tr or qr < tl or ql > tr) return 0;21
if (ql <= tl and tr <= qr) return st[tind];22
int tm = tl + ((tr - tl) >> 1), left = tind << 1;23
return query(left, tl, tm, ql, qr) + query(left | 1, tm + 1, tr, ql, qr);26
vector<int> countPairs(int n, vector<vector<int>> &a, vector<int> &q) {27
vector<int> f(n + 2, 0);28
vector<unordered_map<int, int>> mp(n + 2); // using a normal map gives TLE31
int mx = max(v[0], v[1]), mn = min(v[1], v[0]);37
vector<int> ans(q.size(), 0);39
st.resize(4 * m + 40, 0);41
for (int i = n; i; i--) {42
// reducing the common edges between current node and its adjacent43
for (auto e : mp[i]) {44
update(1, 0, m, f[e.first], -1);45
update(1, 0, m, f[e.first] - e.second, 1);49
for (int j = 0; j < q.size(); j++) ans[j] += query(1, 0, m, max(0, q[j] - curr + 1), m);51
// reversing the changes made initially52
for (auto e : mp[i]) {53
update(1, 0, m, f[e.first], 1);54
update(1, 0, m, f[e.first] - e.second, -1);57
update(1, 0, m, f[i], 1);