光荣的梦想[逆序对]

传送门

水一道题 , 注意逆序对是严格大于 , 所以每次树状数组查询都要查<=x的


#include<bits/stdc++.h>
#define N 100050
#define LL long long
using namespace std;
int c[N],n; LL ans;
void Up(int x,int val){for(;x<=N-50;x+=x&-x) c[x]+=val;}
int Q(int x){int ans=0; for(;x;x-=x&-x) ans += c[x]; return ans;}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x; scanf("%d",&x);
		ans += (i-Q(x)-1); Up(x,1);
	}printf("%lld",ans); return 0;
}

猜你喜欢

转载自blog.csdn.net/sslz_fsy/article/details/84933467
今日推荐