[bzoj2141] Queued (Block Dafa good)

Source: Internet
Author: User

Description

Row and row sit, eat fruit fruit, fruit sweet-winded, everybody smiling. You one, I a, big points to you, small left me, eat fruit fruit sing a song, Everybody Happy and. Red Star Kindergarten children lined up a long team, ready to eat fruit fruit. However, because of the children's height is different, the ranks of the high and low level of confusion, very unattractive. The height of the first child is Hi, and we define a sequence with a degree of clutter: the number of IHJ (I,J) to be met. The kindergarten aunt will choose two children each time, exchange their position, please help calculate the sequence after each exchange, the degree of clutter. For the convenience of kindergarten Aunt statistics, you should also output the clutter level of the sequence without any exchange operations.

Input

The first behavior is a positive integer n, which indicates the number of children; the second line contains n spaces separated by a positive integer h1,h2,..., HN, which in turn represents the height of the child in the initial queue; the third behavior is a positive integer m, which represents the number of times the interchange operation is performed, and the following m rows contain two positive integers ai and bi¬. Represents the exchange of location AI with location Bi's children.

Output

Output file total m line, line I a positive integer indicates how messy the sequence is after the exchange operation I ends.

Sample Input "Sample Input"
3  the  Max  $ 2 2 3 1 3
Sample Output
1 0 3

"Sample description"
(2,3) satisfies the condition when nothing is done;
At the end of Operation 1, the sequence is 130 140 150, there is no ihj (i,j) pair;
After Operation 2, the sequence is 150 140 130, (), (1,3), (2,3) a total of 3 to meet the conditions (I,J).
"Data size and conventions"
For 100% of data, 1≤m≤2*103,1≤n≤2*104,1≤hi≤109,ai≠bi,1≤ai,bi≤n.

Solution

Block the block, with a set of tree arrays, block processing reverse order

Consider swapping, incomplete block on Brute force enumeration update complete directly with a tree array

//Kaiba_seto 20170116//Otz Cjkmao#include <math.h>#include<stdio.h>#include<string.h>#include<algorithm>#defineMAXN 20010#defineMaxbuf 1<<22#defineRG Register#defineInline __inline__ __attribute__ ((always_inline))#defineBlue () (((s = = t) && (t= (s=b) +fread (B,1,maxbuf,stdin), S = = t))? 0: *s++)Charb[maxbuf],*s=b,*t=B;inlinevoidRin (RGint&x) {x=0; RgintC=blue (), f=1;  for(; c< -|| C> $; C=Blue ())if(c== $) f=-1;  for(; c> -&&c< -; C=Blue ()) x= (x<<1) + (x<<3) +c- -; X*=F;}structpr{intfir,sec; BOOL operator< (ConstPr &other)Const {        returnFir <Other.fir;} }B[MAXN];intn,m,a[maxn],block_size,c1[maxn],c2[ $][maxn],ans;inlinevoidEXC (RGint&x,rgint&y) {x^=y; Y^=x; X^=y;} InlinevoidModify (RGint*c,rgintX,rgintd) { for(; x<=n; x+=x& (-x)) c[x]+=D;} InlineintSUM (RGint*c,rgintx) {RGintres=0;  for(; x; x-=x& (-x) Res+=C[x]; returnRes;}intMain () {Rin (n); Block_size=static_cast<int> (sqrt (n) +1e-6);  for(RGintI=1; i<=n; i++) Rin (B[i].fir), B[i].sec=i; Std::sort (b+1, B +1+N);  for(RGintI=1; i<=n; i++) {        Static inttop=0; if(B[i].fir! = b[i-1].fir)++top; A[B[I].SEC]=top;}  for(RGintI=n; I i--) ans+=sum (c1,a[i]-1), modify (C1,a[i],1);  for(RGintI=1; i<=n; i++) Modify (c2[(i-1)/block_size],a[i],1); printf ("%d\n", ans);    Rin (m);  while(m--) {RGintX,y,l,r,i;        Rin (x), Rin (y); if(X >y) exc (x, y); L= (X-1)/block_size+1; R= (y1)/block_size-1; if(L <=r) { for(i=l; i<=r; i++) ans=ans-sum (c2[i],a[x]-1) +sum (c2[i],n)-sum (C2[i],a[x]) +sum (c2[i],a[y]-1)-sum (c2[i],n) +sum (c2[i],a[y]);  for(i=x+1; i<=l*block_size; i++) {                if(A[i] < a[x]) ans--; if(A[i] > a[x]) ans++; if(A[i] > A[y]) ans--; if(A[i] < a[y]) ans++; }  for(I= (r+1) *block_size+1; i<y; i++) {                if(A[i] < a[x]) ans--; if(A[i] > a[x]) ans++; if(A[i] > A[y]) ans--; if(A[i] < a[y]) ans++; } }        Else {             for(i=x+1; i<y; i++) {                if(A[i] < a[x]) ans--; if(A[i] > a[x]) ans++; if(A[i] > A[y]) ans--; if(A[i] < a[y]) ans++; } }        if(A[x] < a[y]) ans++; Else if(A[x] > A[y]) ans--; printf ("%d\n", ans); Modify (c2[(x-1)/block_size],a[x],-1); Modify (c2[(x-1)/block_size],a[y],1); Modify (c2[(Y-1)/block_size],a[x],1); Modify (c2[(Y-1)/block_size],a[y],-1); EXC (A[x],a[y]);    } fclose (stdin); return 0; }

Orz Cjkmao/mr.cjk.cat

[bzoj2141] Queued (Block Dafa good)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.