Test instructions: Exchange some logarithm of the natural number, in order to find out how many inverse number pairs after the exchange.
Idea: Because the topic number range is larger, can not directly use the tree-like array to calculate, first of all to discretization. Then an algorithm is the official question of whether the logarithm of the reverse order is an exchange-based number classification discussion statistics. My algorithm is to consider the number of consecutive numbers without swapping as a number, using a tree-like array to directly add the number of this interval, so that there is no need for tedious (? ) was discussed.
#include <iostream>#include<map>#include<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<vector>#include<queue>#include<stack>#include<functional>#include<Set>#definePB Push_back#defineFS First#defineSe Secondusing namespaceStd;typedefLong LongLl;typedef pair<ll,ll>P;Const intmaxv=1e5*4;structbit{ll A[MAXV]; voidAddintPintx) { while(p<MAXV) {A[p]+=x; P+=p&-p; }} ll sum (intp) {ll ans=0; while(p>0) {ans+=A[p]; P-=p&-p; } returnans; } ll after (inta) { returnSUM (maxv-1)-sum (a); }}b;intA[MAXV];intB[MAXV];intC[MAXV];intMID[MAXV];intSQ[MAXV];intN;map<int,int>Mii;intMain () {Freopen ("/home/files/cppfiles/in","R", stdin); CIN>>N; for(intI=0; i<n;i++) {scanf ("%d%d", a+i,b+i); C[i*2]=A[i]; C[i*2+1]=B[i]; } sort (C,c+2*N); intH=unique (c,c+2*n)-C; for(intI=1; i) {Mid[i]=c[i]-c[i-1]-1; } for(intI=0; i) {Mii[c[i]]=i+1; } for(intI=0; i<maxv;i++) {Sq[i]=i; } for(intI=0; i<n;i++) {swap (sq[mii[a[i]]],sq[mii[b[i]]); } ll ans=0; for(intI=1; i<=h;i++) {ll now=Sq[i]; Ans+=b.after (now*2-1); B.add ( now*2-1,1); Ans+=mid[i]*b.after (i*2); B.add (2*I,mid[i]); } cout<<ans<<Endl; return 0;}
View Code
Codeforeces 540E (tree-like array