[Template] [CDQ] [] [merge sort of reverse seeking]

#include<cstdio>
#include<cstdlib>
using namespace std;
int n;
const int N=500003;
int d[N],t[N];
long long sum;

void CDQ(int l,int r)
{
    if(l==r) return ;
    int t1=l,mid=(l+r)>>1,t2=mid;
    CDQ(l,mid);
    CDQ(++t2,r);
    
    int pos=t1;
    while(t1<=mid && t2<=r)
        if(D [T1] <= D [T2]) 
            T [POS ++] = D [T1 ++ ];
         the else 
        { 
            SUM + = MID-T1 + . 1 ; 
            T [POS ++] = D [T2 ++ ]; 
        } 
    // will range produced contributions:
     // I <= MID && d [i]> d [j] && J <= R & lt
     // we add t is the array of front to back, care is d [j] instead of d [i ],
     // if a d [i] contribute, and that his contribution was less than his maximum j that d [j], all contributions are made to provide j ++
     // so if there is surplus t1, nor It will regenerate contribution 
    the while (T2 <= R & lt) T [POS ++] = D [T2 ++ ];
     the while (T1 <= MID) T [POS ++] = D [T1 ++ ];
     for ( int I = L; I <= R & lt; i ++) d [i] =t[i];
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&d[i]);
    
    CDQ(1,n);
    printf("%lld\n",sum);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xwww666666/p/11293799.html