Chika gives you an integer sequence a1,a2,…,an and m tasks. For each task, you need to answer the number of " friendly pairs" in a given interval.
friendly pair: for two integers ai and aj, if i < j and the absolute value of ai−aj is no more than a given constant integer K, then (i,j) is called a “friendly pair”.A friendly pair (i,j) in a interval [L,R] should satisfy L≤i<j≤R.
intlowbit(int x){ return x & -x; } voidftadd(int pos, int x){ for (; pos < XM; pos += lowbit(pos)) FT[pos] += x; }
intftget(int pos){ int res = 0; for (; pos; pos -= lowbit(pos)) res += FT[pos]; return res; }
vector<int> vec;
structQ { int l, r; int i; } qs[XN]; int ans[XN]; intmain() { int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; i++) { int x; cin >> x; num[i] = x; vec.push_back(x); vec.push_back(x + k); vec.push_back(x - k - 1); } sort(vec.begin(), vec.end()); for (int i = 1; i <= n; i++) { nL[i] = lower_bound(vec.begin(), vec.end(), num[i] - k - 1) - vec.begin(); nR[i] = lower_bound(vec.begin(), vec.end(), num[i] + k) - vec.begin(); num[i] = lower_bound(vec.begin(), vec.end(), num[i]) - vec.begin();
nL[i]++; nR[i]++; num[i]++; }
for (int i = 0; i < m; i++) { Q& q = qs[i]; cin >> q.l >> q.r; q.i = i; } int B = sqrt(n); sort(qs, qs + m, [B](const Q& a, const Q& b) { if (a.l / B != b.l / B)return a.l / B < b.l / B; return a.r < b.r; });
int l = 1, r = 1; int res = 0; ftadd(num[l], 1);
for (int i = 0; i < m; i++) { Q q = qs[i]; while (l < q.l) { ftadd(num[l], -1); res -= ftget(nR[l]) - ftget(nL[l]); l++; } while (q.l < l) { l--; res += ftget(nR[l]) - ftget(nL[l]); ftadd(num[l], 1); } while (r < q.r) { r++; res += ftget(nR[r]) - ftget(nL[r]); ftadd(num[r], 1); } while (q.r < r) { ftadd(num[r], -1); res -= ftget(nR[r]) - ftget(nL[r]); r--; } ans[q.i] = res; } for (int i = 0; i < m; i++)cout << ans[i] << "\n";
Again Alice and Bob is playing a game with stones. There are N piles of stones labelled from 1 to N, the i th pile has ai stones.
First Alice will choose piles of stones with consecutive labels, whose leftmost is labelled with L and the rightmost one is R. After, Bob will choose another consecutive piles labelled from l to r (L≤l≤r≤R). Then they’re going to play game within these piles.
Here’s the rules of the game: Alice takes first and the two will take turn to make a move: choose one pile with nonegetive stones and take at least one stone and at most all away. One who cant make a move will lose.
Bob thinks this game is not so intersting because Alice always take first. So they add a new rule, which is that Bob can swap the number of two adjacent piles’ stones whenever he want before a new round. That is to say, if the i th and i+1 pile have ai and ai+1 stones respectively, after this swapping there will be ai+1 and ai.
Before today’s game with Bob, Alice wants to know, if both they play game optimally when she choose the piles from L to R, there are how many pairs (l, r) chosed by Bob that will make Alice win.
memset(ans, -1, sizeof(ans)); int n, m, q; cin >> n >> m >> q; for (int i = 1; i <= m; i++)cin >> delicious[i]; for (int i = 1; i <= n; i++)cin >> w[i]; for (int i = 0; i < n-1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; i++)cin >> kind[i]; for (int i = 1; i <= n; i++)origin[i] = kind[i];
for (int i = 0; i < q; i++) { int opt, u, v; cin >> opt >> u >> v; if (opt == 0) { modified[i].first = u; modified[i].second = v; } else { qs[i].idx = i; qs[i].u = u; qs[i].v = v; } }
dfs(1, 0); //for (int i = 1; i <= tick; i++)cout << ref_tick[i] << " "; //cout << endl;
for (int p = 1; p < 20; p++) { for (int i = 1; i <= n; i++) { fa[i][p] = fa[fa[i][p - 1]][p - 1]; } }
//区间[) for (int i = 0; i < q; i++) { Q& cur = qs[i]; if (cur.u == 0)continue; if (in[cur.u] > in[cur.v])swap(cur.u, cur.v); if (lca(cur.u, cur.v) == cur.u) { cur.l = in[cur.u]; cur.r = in[cur.v]+1; } else { cur.l = out[cur.u]; cur.r = in[cur.v] + 1; } //cout << cur.l << " " << cur.r << endl; }
int B = pow(tick, 2.0 / 3); for (int i = 0; i < XN; i++) { belongs[i] = i / B; } sort(qs, qs + q, [=](const Q& a, const Q& b) { if (belongs[a.l] != belongs[b.l])return belongs[a.l] < belongs[b.l]; if (belongs[a.r] != belongs[b.r])return belongs[a.r] < belongs[b.r];
return a.idx < b.idx; });
int L = 1, R = 1, T = 0;//T) ll wage=0; for (int i = 0; i < q; i++) { Q& cur = qs[i]; //cout <<cur.idx<<","<< "from [" << L << "," << R << ") to [" << cur.l << "," << cur.r << ")" << endl; if (cur.l == cur.r && cur.r == 0)continue; //time while (cur.idx < T) { T--; if (modified[T].first == 0) { continue; } int u = modified[T].first, v = modified[T].second; if (inq[u]) { wage -= (ll)delicious[kind[u]] * w[cnt[kind[u]]]; //cout << "remove " << u << "(" << kind[u] << ")" << (ll)delicious[kind[u]] * w[cnt[kind[u]]] << endl; cnt[kind[u]]--; } kind[u] = his[u].back(); his[u].pop_back(); if (inq[u]) { cnt[kind[u]]++; wage += (ll)delicious[kind[u]] * w[cnt[kind[u]]]; //cout << "add " << u << "(" << kind[u] << ")" << (ll)delicious[kind[u]] * w[cnt[kind[u]]] << endl;
} } while (cur.idx > T) {
if (modified[T].first == 0) { T++; continue; } int u = modified[T].first, v = modified[T].second; if (inq[u]) { wage -= (ll)delicious[kind[u]] * w[cnt[kind[u]]]; //cout << "remove " << u << "(" << kind[u] << ")" << (ll)delicious[kind[u]] * w[cnt[kind[u]]] << endl;