Main topic: Given a sequence, m exchange two numbers, the initial reverse logarithm and the inverse logarithm after each exchange
First discretization, chunking, creating a tree array for each block, saving all the elements in this block
Then there is no effect on the number of sides of each query (x, y) (X<y), and the number of intervals (x, y) a[i] is discussed below:
A[I]<A[X]--ans
A[I]>A[X] ++ans
A[i]<a[y] ++ans
A[i]>a[y]--ans
Then for the tree array in the block processing, the violence outside the block
Note that this problem element has repeated pro-test trust
RANK5 scare 0.0 Why a set of trees is faster than a tree set of trees ...
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define M 20200using namespace Std;int n,m,ans,tot,block,a[m];p air<int,int>b[m];int pre[m],cnt[ 200][m];void Update (int c[],int x) {for (; x<=n;x+=x&-x) ++c[x];} void downdate (int c[],int x) {for (; x<=n;x+=x&-x)--c[x];} int Get_ans (int c[],int x) {int re=0;for (; x;x-=x&-x) Re+=c[x];return re;} int main () {int i,j,x,y;cin>>n;for (i=1;i<=n;i++) scanf ("%d", &b[i].first), B[i].second=i;sort (b+1,b+n+1 ); for (i=1;i<=n;i++) {if (B[i].first!=b[i-1].first) ++tot;a[b[i].second]=tot;} for (i=n;i;i--) Ans+=get_ans (pre,a[i]-1), Update (Pre,a[i]);block=static_cast<int> (sqrt (n) +1e-7); for (i=1;i <=n;i++) Update (cnt[(i-1)/block],a[i]), Cout<<ans<<endl;cin>>m;for (i=1;i<=m;i++) {scanf (" %d%d ", &x,&y), if (x>y) swap (x, y),//query (x, y) interval number of numbers than a[x] and a[y] small int b1= (x-1)/block+1;int b2= (y-1)/block-1; if (B1<=B2) {for (j=b1;j<=b2;j++) {Ans-=get_ans (cnt[j],a[x]-1); Ans+=get_ans (cnt[j],n)-get_ans (cnt[j],a[x]); Ans+=get_ans (cnt[j],a[y]-1); Ans-=get_ans (cnt[j],n)-Get_Ans ( Cnt[j],a[y]);} for (j=x+1;j<=b1*block;j++) {if (a[j]<a[x])--ans;if (a[j]>a[x]) ++ans;if (A[j]<a[y]) ++ans;if (a[j]>a[y ])--ans;} For (j= (b2+1) *block+1;j<y;j++) {if (a[j]<a[x])--ans;if (a[j]>a[x]) ++ans;if (A[j]<a[y]) ++ans;if (a[j]> A[y])--ans;}} Else{for (j=x+1;j<=y-1;j++) {if (a[j]<a[x])--ans;if (a[j]>a[x]) ++ans;if (A[j]<a[y]) ++ans;if (a[j]>a[y ])--ans;}} if (A[x]<a[y]) ++ans;else if (A[x]>a[y])--ans;printf ("%d\n", ans);D owndate (cnt[(x-1)/block],a[x]);D owndate ( cnt[(y-1)/block],a[y]); swap (a[x],a[y]); Update (cnt[(x-1)/block],a[x]); Update (cnt[(y-1)/block],a[y]);}}
Bzoj 2141 Queued block + Tree array