P1908 reverse order - (Fenwick tree)

https://www.luogu.org/problem/P1908

Prefer the tree line, too lazy to use Fenwick tree (only sets the template, the essence of bit operation did not comprehend) , there has been no record Fenwick tree codes, they have to retrieve, while recording a bit to this question, three-dimensional partial order cdq bedding sets Fenwick tree look.

Problem-solving ideas: first, the original array a descending order, in turn added to the tree array c, the result of each prefix and the number is seeking to reverse the current number.

For example data: 55,44,22,66,33,11

Initialization Fenwick tree c, cleared;

66-4 sign bit is added, is added to the array 0,0,0, 1, 0,0; 66 before it is no greater than the number, the number of reverse is 0 , 0,0,0 Fenwick tree C, 1,0,0;

55-1 sign bit is added, is added to the array 1, 0,0,1,0,0; 55 before it is no greater than the number, the number of reverse is 0 , Fenwick tree C is 1,1,0, 2,0,0;

Add 44 to the 2nd bit, the array is added 1, 1, 0,1,0,0; 44 before it is larger than 55, the number of reverse is 1 , the array C is 1,2,0,3 tree , 0,0

33-5 sign bit is added, is added to arrays 1,1,0,1, 1, 0; 55,44,66 have greater than 33 before it is reverse to the number 3 , the array C is a tree 2 , 0,3,1,1

22 was added to the 3rd position, the array is added 1,1, 1, 1,1,0; 55,44,66 have greater than 22 before it is reverse to the number 2 , C is 1, 2 Fenwick tree , 1,4,1,2

6 to 11 is added, it is added to the array 1,1,1,1,1, 1; the former is larger than 11 has 55,44,22,66,33 it is reverse to the number 5 , Fenwick tree C as 1,2,1,4,1,2

Pit: If there is the same number, when the latter is first sorted according to add, so as not to repeat the calculation reverse pair.

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<ctime>
#define ll long long
#define inf 0x3f3f3f3f
const double pi=3.1415926;
using namespace std;

const int maxx=500005;
struct Node 
{ 
    int ID;
     int Val; 
}; 
Node A [Maxx]; /// original array 
int C [Maxx]; /// Fenwick tree 
int n-; 

BOOL CMP (Node P1, P2 Node) 
{ 
    IF (P1 == p2.val .val) /// pit 
        return p1.id> p2.id;
     return p1.val> p2.val; 
} 

int lowbit ( int X) 
{ 
    return X & (- X); 
} 

void the Add ( int the X-, int Val) ///Val added value of the position x 
{
     the while (x <= n-+ . 1 ) /// x is less than 32005 requires otherwise, those input data x = 20000. large number of planets can not find the lower left of 
    { 
        C [x] + = val; 
        X + = lowbit (X); 
    } 
} 

int SUM ( int X) 
{ 
    int RES = 0 ;
     the while (X> 0 ) 
    { 
        RES + = C [X]; 
        X - = lowbit (X); 
    } 
    return RES; 
} 



int main () /// P1908 
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i].val),a[i].id=i;
    sort(a+1,a+n+1,cmp);
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        ans +=(ll)sum(a[i].id);
        add(a[i].id,1);
    }
    printf("%lld\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/shoulinniao/p/11619715.html