Codeforeces 540E (tree-like array

Source: Internet
Author: User

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

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.