4.4 Provincial election simulation game Lagrange count Fenwick tree + inclusion and exclusion.

avatar
avatar

Counting Problems like this what is the most annoying ...

Central to consider is the number of back ring adjourned to the previous paragraph violent enumerate two digital Fenwick tree statistics third number of n ^ 2log.

Consider only the enumeration of the number of discovered what the problem because the border is difficult to deal with.

Then found that the direct enumeration onto the ring but there is no boundary problem 123231312 presence of three forms after enumeration of the second number.

The first form good look at the statistics pretreatment left how many numbers are small compared to their own.

And a third form is difficult to consider the second figured out considering these two forms of inclusion and exclusion 231 = XX1-321. 312 = 3XX-321 at 2 when the enumeration.

3XX good statistics and found XX1 321 and 123 above the same statistical methods to O (nlogn) to get an answer.

const int MAXN=200010;
int n,T,m,top;ll ans=0;
int a[MAXN],c[MAXN];ll f[MAXN];
inline void add(int x,int y){while(x<=n){c[x]+=y;x+=x&(-x);}}
inline int ask(int x){int cnt=0;while(x){cnt+=c[x];x-=x&(-x);}return cnt;}
signed main()
{
	freopen("1.in","r",stdin);
	get(n);
	rep(1,n,i)
	{
		get(a[i]);
		f[i]=ask(a[i]-1);
		add(a[i],1);
	}
	rep(1,n,i)
	{
		ans+=f[i]*(n-i-(a[i]-1-f[i]));//123 左边比我小 右边比我大
		ans+=(a[i]-1-f[i])*(a[i]-1-f[i]-1)/2;//3XX
		ans+=(i-1-f[i])*(i-1-f[i]-1)/2;//XX1
		ans-=(i-1-f[i])*(a[i]-1-f[i])*2;//左边比我大 右边比我小
	}
	putl(ans);return 0;
}

Guess you like

Origin www.cnblogs.com/chdy/p/12632918.html