50分树状数组查错
  • 板块P1908 逆序对
  • 楼主墨希
  • 当前回复3
  • 已保存回复3
  • 发布时间2020/11/1 18:49
  • 上次更新2023/11/5 09:17:04
查看原帖
50分树状数组查错
328985
墨希楼主2020/11/1 18:49

RT

Code:

#include<iostream>  
#include<stdio.h>
#include<algorithm>
using namespace std;
const int M=500005;
int n,m,ans,a[M],b[M],c[M],x[M];
inline void read(int &v)
{
	int rec=0,flag=1;
	char ch=getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch=='-')  flag=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9')  rec=rec*10+ch-'0',ch=getchar();
	v=flag*rec;
return;
}
int lowbit(int x){ return x&-x; }
int ask(int x)
{
	int rec=0;
	for(;x;x-=lowbit(x))  rec+=c[x];
return rec;
}
void add(int x)
{
	for(;x<=n;x+=lowbit(x))  c[x]++;
return;
}
int main()  
{  
    read(n);
    for(int i=1;i<=n;i++)  read(a[i]),x[i]=a[i];
    sort(x+1,x+1+n);
    m=unique(x+1,x+1+n)-(x+1);
    for(int i=1;i<=n;i++)  b[i]=lower_bound(x+1,x+1+m,a[i])-x;
	for(int i=n;i>=1;i--)
    {
    	ans+=ask(b[i]-1);
    	add(b[i]);
	}
	printf("%d",ans);
return 0;
}
2020/11/1 18:49
加载中...