首页 > 编程语言 > 详细

bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组_排序

时间:2019-05-30 21:20:43      阅读:80      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!