P3157 [CQOI2011] Dynamic reverse order
https://www.luogu.org/problemnew/show/P3157
Title Description
For sequence A, its inverse logarithm is defined as the number of pairs (i,j) that satisfy I<j and Ai>aj. An arrangement of 1 to N, in which the M elements are removed sequentially, your task is to count the inverse logarithm of the entire sequence before deleting one element at a time.
Input/output format
Input format:
Enter the first line containing two integers n and M, that is, the number of initial elements and the number of deleted elements. The following n rows each row contains a positive integer between 1 and N, which is the initial arrangement. The following M-line is a positive integer for each row, followed by each deleted element.
Output format:
The output contains m rows, followed by the number of reverse pairs before each element is deleted.
Input and Output Sample input example # #:
5 4153425142
Sample # # of output:
5221 Sample Interpretation (1,5,3,4,2) (1,3,4,2) (3,4,2) (3,2) (3).
Description
n<=100000 m<=50000
Tree-shaped array set of Chairman tree
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <Set>7#include <map>8#include <vector>9#include <queue>Ten #defineN 100005 One using namespacestd; A - structsair{ - intl,r,sum; the}tree[n* Max]; - intN; - introot[n],a[n],b[n],c[n],cnt; - voidADD (intx) { + while(x<=N) { -c[x]++; +x+= x& (-x); A } at } - - intGetsum (intx) { - intsum=0; - while(x) { -sum+=C[x]; inx-= x& (-x); - } to returnsum; + } - the voidAddintCurintLintRintPintv) { *tree[cur].sum+=v; $ if(l==R) {Panax Notoginseng return; - } the intMid= (L+R)/2; + if(p<=mid) { A if(!TREE[CUR].L) { thetree[cur].l=++CNT; + } - Add (tree[cur].l,l,mid,p,v); $ } $ Else{ - if(!TREE[CUR].R) { -tree[cur].r=++CNT; the } -Add (tree[cur].r,mid+1, r,p,v);Wuyi } the } - Wu intQueryintLintRintCurintLintR) { - if(!cur) { About return 0; $ } - if(l<=l&&r>=R) { - returntree[cur].sum; - } A intMid= (L+R)/2; + intans=0; the if(L<=mid) ans+=query (l,r,tree[cur].l,l,mid); - if(R>mid) Ans+=query (l,r,tree[cur].r,mid+1, R); $ returnans; the } the the intMain () { the intm,v,p,j; -scanf"%d%d",&n,&m); in Long Longans=0; the for(intI=1; i<=n;i++){ thescanf"%d",&a[i]); Aboutb[a[i]]=i; the Add (A[i]); theAns+=getsum (N)-getsum (A[i]); the for(j=i;j<=n;j+= j& (-j)) { + if(!Root[j]) { -root[j]=++CNT; the }BayiAdd (Root[j],1, N,a[i],1); the } the } - for(intI=1; i<=m;i++){ -printf"%lld\n", ans); thescanf"%d",&v); thep=B[v]; thej=p-1; the while(j) { -Ans-=query (v+1, N,root[j],1, n); thej-= j& (-j); the } thej=N;94 while(j) { theAns-=query (1, V-1, Root[j],1, n); thej-= j& (-j); the }98j=p; About while(j) { -Ans+=query (1, V-1, Root[j],1, n);101j-= j& (-j);102 }103j=p;104 while(j<=N) { theAdd (Root[j],1, n,v,-1);106j+= j& (-j);107 }108 }109}
View Code
P3157 [CQOI2011] Dynamic reverse order