"Luogu" P3157 dynamic Inverse pair (tree array set of Chairman tree)

Source: Internet
Author: User

Topic links

MD The first way in Noilinux under the purple problem of vim. Since I'm not very familiar with this operating system, there seems to be something wrong with MD tuning to death. (I also played two times code, adjusted for two hours)

But this problem is not difficult, is the tree array set on the Chairman tree ... This should be a routine with a modified chairman tree, which can meet the two conditions of the required position and size at the same time.

But it's been a long time. Reflecting my ability to structure my data is extremely weak.

Last put code

  

#include <cstdio>#include<algorithm>#include<cstring>#include<cctype>#include<cstdlib>#defineMAXN 500020#defineMid ((l+r) >>1)inlineLong LongRead () {Long Longnum=0, f=1; CharCh=GetChar ();  while(!isdigit (CH)) {        if(ch=='-') f=-1; CH=GetChar (); }     while(IsDigit (CH)) {num=num*Ten+ch-'0'; CH=GetChar (); }    returnnum*F;}intsum[maxn* the];intrt[maxn* the];intls[maxn* the];intrs[maxn* the];intc[maxn* the];intQ[MAXN],BACK[MAXN];intX[MAXN],Y[MAXN];intA1[MAXN],A2[MAXN];intn,m;intTot;inlineintLowinti) {returni& (-i); }inlineintAskintPOS) {    intans=0;  while(POS) {ans+=C[pos]; POS-=Low (POS); }    returnans;}intQuerypre (int  from,intTo,inte) {    intL=1, R=n;inttotx=0, toty=0, ans=0; from--;  for(intI= from; I;i-=low (i)) x[++totx]=Rt[i];  for(intI=to;i;i-=low (i)) y[++toty]=Rt[i];  while(l<R) {        if(e>mid) {             for(intI=1; i<=totx;++i) ans-=Sum[ls[x[i]];  for(intI=1; i<=toty;++i) ans+=Sum[ls[y[i]];  for(intI=1; i<=totx;++i) x[i]=Rs[x[i]];  for(intI=1; i<=toty;++i) y[i]=Rs[y[i]]; L=mid+1; }        Else{             for(intI=1; i<=totx;++i) x[i]=Ls[x[i]];  for(intI=1; i<=toty;++i) y[i]=Ls[y[i]]; R=mid; }    }    returnans;}intQuerysub (int  from,intTo,inte) {    intL=1, R=n;inttotx=0, toty=0, ans=0; from--;  for(intI= from; I;i-=low (i)) x[++totx]=Rt[i];  for(intI=to;i;i-=low (i)) y[++toty]=Rt[i];  while(l<R) {        if(e<=mid) {             for(intI=1; i<=totx;++i) ans-=Sum[rs[x[i]];  for(intI=1; i<=toty;++i) ans+=Sum[rs[y[i]];  for(intI=1; i<=totx;++i) x[i]=Ls[x[i]];  for(intI=1; i<=toty;++i) y[i]=Ls[y[i]]; R=mid; }        Else{             for(intI=1; i<=totx;++i) x[i]=Rs[x[i]];  for(intI=1; i<=toty;++i) y[i]=Rs[y[i]]; L=mid+1; }    }    returnans;}voidUpdateint&o,intLintRintp) {    if(!o) O=++tot; sum[o]++; if(L==R)return; if(p<=mid) Update (LS[O],L,MID,P); ElseUpdate (rs[o],mid+1, r,p);}intMain () {n=read (), m=read ();  for(intI=1; i<=n;++i) {Q[i]=read (); back[q[i]]=i; }    Long Longans=0;  for(intI=1; i<=n;++i) {A1[i]=ask (N)-ask (Q[i]); Ans+=A1[i];  for(intJ=q[i];j<=n;j+=low (j)) c[j]++; } memset (c,0,sizeof(c));  for(inti=n;i;--i) {A2[i]=ask (q[i]-1);  for(intJ=q[i];j<=n;j+=low (j)) c[j]++; }     for(intI=1; i<=m;++i) {printf ("%lld\n", ans); intPos=read ();p os=Back[pos]; Ans-= (A1[pos]+a2[pos]-querypre (pos+1, N,q[pos])-querysub (1, pos-1, Q[pos]));  for(intJ=pos;j<=n;j+=low (j)) Update (Rt[j],1, N,q[pos]); }    return 0;}

"Luogu" P3157 dynamic Inverse pair (tree array set of Chairman tree)

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.