Code:
#include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) #define maxn 200001 using namespace std; int arr[maxn],l[maxn],r[maxn],A[maxn],C[maxn]; int cmp(int a,int b) { return l[a]<l[b]; } int lowbit(int t) { return t&(-t); } void update(int x,int delta) { while(x<maxn) C[x]+=delta,x+=lowbit(x); } int query(int x) { int tmp=0; while(x>0) tmp+=C[x],x-=lowbit(x); return tmp; } int main(){ // setIO("input"); int n,m; scanf("%d",&n),m=(n<<1) ; for(int i=1,a;i<=m;++i) { scanf("%d",&a); if(l[a]) r[a]=i; else l[a]=i; } for(int i=1;i<=n;++i) A[i]=i; sort(A+1,A+1+n,cmp); int ans=0; for(int i=1;i<=n;++i) { int cur=A[i],a; a=query(l[cur])-query(r[cur]); ans+=a; update(l[cur],1),update(r[cur],-1); } printf("%d\n",ans); return 0; }
bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组_排序
原文:https://www.cnblogs.com/guangheli/p/10951609.html