再也不作死写FhqTreap作内层树了,卡的不如暴力呜呜呜……
题意翻译:给一个序列,每个下标包含两个属性$a$和$v$,求第一个属性与下标形成的所有逆序对的第二个属性和,给出$m$个交换两个下标的操作,每次操作之后查询。
考虑一下交换之后会发生什么:
假设这次要交换$x$和$y$,使$x \leq y$。
发现交换之后$x, y$和$x$的左边的数和$y$右边的数构成的逆序对产生的贡献不变,发生变化的是中间的数。
那么问题就简单了,只要先消去中间的数和$x, y$的贡献,具体来说就是$[x + 1, y - 1]$这个区间中$a_{i}$比$a_{x}$小的$b_{i}$和,以及$a_{i}$比$a_{y}$大的$b_{i}$和。
发现$(x, y)$可能也会构成一个逆序对,拎出来判一下可以保证这一对只被计算一次。
然后算一下交换之后产生的贡献,方法类似。
树套树维护即可。
时间复杂度$O(nlog^{2}n)$。
Code:
#include <cstdio> #include <cstring> using namespace std; typedef long long ll; const int N = 5e4 + 5; const int M = 2e7 + 5; const ll P = 1e9 + 7; int n, m, a[N]; ll v[N], ans = 0LL; template <typename T> inline void read(T &X) { X = 0; char ch = 0; T op = 1; for(; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') op = -1; for(; ch >= '0' && ch <= '9'; ch = getchar()) X = (X << 3) + (X << 1) + ch - 48; X *= op; } template <typename T> inline void swap(T &x, T &y) { T t = x; x = y; y = t; } namespace BinaryIndexTree { int cnt[N]; ll s[N]; #define lowbit(x) (x & (-x)) inline void modifyS(int p, ll val) { for(; p <= n; p += lowbit(p)) s[p] = (s[p] + val) % P; } inline void modifyC(int p) { for(; p <= n; p += lowbit(p)) cnt[p]++; } inline ll getSum(int p) { ll res = 0; for(; p > 0; p -= lowbit(p)) res = (res + s[p]) % P; return res; } inline int getCnt(int p) { int res = 0; for(; p > 0; p -= lowbit(p)) res += cnt[p]; return res; } #undef lowbit } using namespace BinaryIndexTree; namespace Tree { int nodeCnt, root[N * 30]; struct Node { int lc, rc, siz; ll sum; } s[M]; #define mid ((l + r) >> 1) inline void up(int p) { if(!p) return; s[p].siz = s[s[p].lc].siz + s[s[p].rc].siz; s[p].sum = (s[s[p].lc].sum + s[s[p].rc].sum) % P; } void modify(int &p, int l, int r, int x, ll selV) { if(!p) p = ++nodeCnt; if(l == r) { s[p].siz = (selV != -1); s[p].sum = (selV == -1 ? 0 : selV % P); return; } if(x <= mid) modify(s[p].lc, l, mid, x, selV); else modify(s[p].rc, mid + 1, r, x, selV); up(p); } ll query(int p, int l, int r, int x, int y, ll selV) { if(!p) return 0LL; if(x <= l && y >= r) return (selV * s[p].siz % P + s[p].sum) % P; ll res = 0LL; if(x <= mid) res = (res + query(s[p].lc, l, mid, x, y, selV)) % P; if(y > mid) res = (res + query(s[p].rc, mid + 1, r, x, y, selV)) % P; return res; } #define lowbit(p) (p & (-p)) inline void change(int p, int val, ll selV) { for(; p <= n; p += lowbit(p)) modify(root[p], 1, n, val, selV); } inline void clear(int p, int val) { for(; p <= n; p += lowbit(p)) modify(root[p], 1, n, val, -1); } inline ll ask(int p, int v, ll selV, int type) { ll res = 0LL; for(; p > 0; p -= lowbit(p)) { if(type) res = (res + query(root[p], 1, n, v + 1, n, selV)) % P; else res = (res + query(root[p], 1, n, 1, v - 1, selV)) % P; } return res; } inline ll qSum(int l, int r, int v, ll selV, int type) { return (ask(r, v, selV, type) - ask(l - 1, v, selV, type) + P) % P; } } using namespace Tree; int main() { read(n), read(m); nodeCnt = 0; for(int i = 1; i <= n; i++) { read(a[i]), read(v[i]); ans = (ans + (getSum(n) - getSum(a[i]) + P) % P + P + v[i] * (getCnt(n) - getCnt(a[i])) % P + P) % P; modifyS(a[i], v[i]), modifyC(a[i]); change(i, a[i], v[i]); } for(int x, y; m--; ) { read(x), read(y); if(x == y) { printf("%lld\n", ans); continue; } if(x > y) swap(x, y); ll res = 0LL; if(x + 1 <= y - 1) { res = (res + qSum(x + 1, y - 1, a[y], v[y], 1)) % P; res = (res + qSum(x + 1, y - 1, a[x], v[x], 0)) % P; } if(a[y] < a[x]) res = ((res + v[x]) % P + v[y]) % P; ans = (ans - res + P) % P; res = 0LL; if(x + 1 <= y - 1) { res = (res + qSum(x + 1, y - 1, a[y], v[y], 0)) % P; res = (res + qSum(x + 1, y - 1, a[x], v[x], 1)) % P; } if(a[y] > a[x]) res = ((res + v[x]) % P + v[y]) % P; ans = (ans + res) % P; printf("%lld\n", ans); clear(x, a[x]), clear(y, a[y]); change(x, a[y], v[y]), change(y, a[x], v[x]); swap(a[x], a[y]), swap(v[x], v[y]); } return 0; }