An example on petition. Spent a lot of time looking for bugs, just learn the tree-like array, this problem add operation should pay attention to the upper limit is not n.
#include <bits/stdc++.h>using namespacestd;#definell Long LongConstll MAXN = 1e5 +Ten; ll C[maxn];ll N;inline ll Lowbit (ll x) {returnX & (-x);} ll sum (ll x) {LL ret=0; while(X >0) {ret+ = C[x]; X-=lowbit (x); } returnret;}voidAdd (ll x, ll D) { while(x <=MAXN) {C[x]+ = D; X + =lowbit (x); }}ll a[maxn];ll R[MAXN], L[MAXN];intMain () {ll t; scanf ("%i64d", &t); while(t--) {memset (C,0,sizeof(C)); scanf ("%i64d", &N); for(LL i =1; I <= N; i++) {scanf ("%i64d", &A[i]); Add (A[i],1); L[i]= SUM (A[i]-1); } memset (C,0,sizeof(C)); for(ll i = n; I >=1; i--) {Add (A[i],1); R[i]= SUM (A[i]-1); } ll ans=0; for(LL i =1; I <= N; i++) {ans+ = l[i] * (N-i-r[i]) + (I-l[i]-1LL) *R[i]; } cout<< ans <<Endl; }}
Uvalive 4329 Ping Pong (tree-like array)