the_xin @ 2020-03-03 14:49:23
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int n,tree[500005],a[500005],m,b[500005];
long long ans;
inline int kth(int num){
return lower_bound(b+1,b+m+1,num)-b;//位置
}
inline void add(register int x,int num){
for(;x<=n;x+=x&(-x))tree[x]+=num;
}
inline int query(register int x){
int num=0;for(;x;x-=x&(-x))num+=tree[x];return num;
}
int main(){
n=read();
for(register int i=1;i<=n;i++){
a[i]=read();
b[i]=a[i];
}
sort(b+1,b+n+1);
m=unique(b+1,b+1+n)-(b+1);
for(register int i=1;i<=n;i++){
add(kth(a[i]),1);
ans+=kth(a[i])-(query(kth(a[i])-1))-1;
}
cout<<ans;
return 0;
}
离散化的树状数组 30分求助
by 万万没想到 @ 2020-03-03 14:54:36
算的不对吧
by 万万没想到 @ 2020-03-03 14:57:24
是否应该为
ans+=query(m)-query(kth(a[i]));
by the_xin @ 2020-03-03 15:00:20
@万万没想到 Orz,明白了