Codeforces 785 E. Anton and permutation (chunked, tree-like array)

Source: Internet
Author: User

Codeforces 785 E. Anton and permutation
The main topic: Give n,q. n represents an array of elements from 1 to n (corresponding to index 1~n), q indicates there is a Q query. Each query gives two number l,r, which requires that the index be indexed to the two-digit exchange position of l,r and gives the inverse logarithm of the array after the interchange.
Idea: This problem uses the idea of chunking, will be divided into Bsz block, in each piece built Fenwick tree, for each query, only need to deal with l,r Middle Block and l,r block affected parts. See Code and annotations for specific implementations.

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <map>#include <stack>#include <queue>#include <set>#include <cmath>#include <algorithm>#include <climits>using namespaceStdtypedef Long Longlltypedefpair<int,int> P;typedefmap<int,int> M;typedefqueue<int> Q;typedefset<int> S;typedefvector<int> V;Const intmaxn=2e5+10, bsz= -;intNintBR[MAXN],BL[MAXN],B[MAXN];//br[i] is the right boundary numbered I block, Bl[i] is the left boundary numbered I block, B[i] is the block number corresponding to the I pointstructfenwick{intSUM[MAXN];voidAddintPintx) { while(p<=n)            {sum[p]+=x;        p+=p&-p; }    }intQryintP) {intres=0; while(p) {res+=sum[p];        p-=p&-p; }returnRes }} Fen[maxn/bsz+1];intA[MAXN];intMain () {Std::ios::sync_with_stdio (false); Std::cin.tie (0);intI,j,m,q; cin>>n>>q;//Sub-block, delimitation    intBn= (n-1)/bsz+1; for(i=0; i<bn;++i) {Bl[i]=i*bsz; Br[i]=min (N,I*BSZ+BSZ); for(j=bl[i];j<br[i];++j) b[j]=i; }//data initialization, Upper tree     for(i=0; i<n;++i) {a[i]=i+1; Fen[b[i]].add (A[i],1); }//Processing queryll ans=0; while(q--) {intL,r;        cin>>l>>r;        l--; r--;if(L==R) {cout<<ans<<endl;Continue; }Else if(l>r) swap (L,R);intLess_l=0, less_r=0;//Handling L,r Intermediate blocks         for(I=b[l]+1; i<b[r];++i) {less_l+=fen[i].qry (a[l]);        Less_r+=fen[i].qry (A[r]); }//handling the affected part of the block where the L,r is located        if(B[l]!=b[r]) { for(I=l;i<br[b[l]];++i)                {less_l+=a[i]<a[l];            less_r+=a[i]<a[r]; } for(I=bl[b[r]];i<=r;++i)                {less_l+=a[i]<a[l];             less_r+=a[i]<a[r]; }        }Else{ for(I=l;i<=r;++i)                {less_l+=a[i]<a[l];            less_r+=a[i]<a[r]; }        }//Because the processing of the endpoint l,r is counted repeatedly when calculating less_l and less_r, the answer needs to be corrected        if(A[l]<a[r]) ans--;Elseans++;//Update Fenwick TreeFen[b[l]].add (A[l],-1); Fen[b[r]].add (A[r],-1);         Swap (a[l],a[r]); Fen[b[l]].add (A[l],1); Fen[b[r]].add (A[r],1);//Handling answers        intTotal=r-l; ll tmpl= (total-less_l)-less_l;//increment inverse logarithm-reduced inverse logarithmll tmpr=less_r-(Total-less_r);//increment inverse logarithm-reduced inverse logarithmANS+=TMPL+TMPR;     cout<<ans<<endl; }return 0;}

Codeforces 785 E. Anton and permutation (chunked, tree-like array)

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.