Dynamic open point of the tree line on the reverse

For a given period sequence of positive integers, is the reverse of the sequence ai> aj and i <j ordered pair.
This required a positive integer number in reverse order of the sequence.
Input
a first row, a number n, there is a sequence number n. N <= 5 * 10 ^ 5
the number of second row n, the given sequence. Each number in the sequence. 9 ^ no more than 10
the Output
number sequence in reverse order given.
The Input the Sample
. 6
. 5. 4. 3. 1 2. 6
the Sample the Output
. 11

 

#include<cstdio>
#define ll long long
using namespace std;
const int N=1e7+7; 
int n,r;
ll ans;
struct A
{
	int cnt,c[N],lc[N],rc[N];
	int sum(int p,int l,int r,int x,int y)
	{
		if(!p) return 0;
		if(l==x&&r==y) 
		    return c[p];
		int mid=l+r>>1;
		if(y<=mid) return sum(lc[p],l,mid,x,y);
		if(x>mid) return sum(rc[p],mid+1,r,x,y);
		return sum(lc[p],l,mid,x,mid)+sum(rc[p],mid+1,r,mid+1,y);    
	}
	
	void add(int &p,int l,int r,int x,int k)
	{
		if(!p) 
		    p=++cnt; 
		if(l==r) 
		{
			c[p]+=k; 
			return;
		}
		int mid=l+r>>1;
		if(x<=mid) 
		    add(lc[p],l,mid,x,k);
		else 
		    add(rc[p],mid+1,r,x,k);
		c[p]=c[lc[p]]+c[rc[p]];
	}
}tree;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x; scanf("%d",&x);
		ans+=tree.sum(r,1,1e9,x+1,1e9);
		tree.add(r,1,1e9,x,1);
	}
	printf("%lld",ans);
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/cutemush/p/11833958.html