标签:def bsp 树状数组 const lib names 求逆 fine long
解题关键:离散化。
#include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> #define maxn 500004 using namespace std; typedef long long ll; struct node{ int id,val; }a[maxn]; int bits[maxn],p[maxn]; bool cmp(const node &a,const node &b){ return a.val<b.val; } int n; void add(int i,int x){ while(i<=maxn){ bits[i]+=x; i+=i&-i; } } int sum(int i){ int s=0; while(i>0){ s+=bits[i]; i-=i&-i; } return s; } int main(){ while(scanf("%d",&n)&&n){ for(int i=1;i<=n;i++) scanf("%d",&a[i].val),a[i].id=i; sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) p[a[i].id]=i; ll ans=0; memset(bits,0,sizeof bits); for(int i=1;i<=n;i++){ add(p[i],1); ans+=i-sum(p[i]); } printf("%lld\n",ans); } }
标签:def bsp 树状数组 const lib names 求逆 fine long
原文地址:https://www.cnblogs.com/elpsycongroo/p/9038357.html