码迷,mamicode.com
首页 > 其他好文 > 详细

P1966 火柴排队

时间:2019-10-29 19:32:51      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:str   using   long   mod   owb   sum   scanf   n+1   bool   

//P1966 火柴排队
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=99999997;
const int mxn=1e6+5;
int n;
int /*a1[mxn],a2[mxn],d1[mxn],d2[mxn],*/c[mxn],r[mxn];
struct ss{
    int v,num;
}a[mxn],b[mxn];
bool cmp(ss a,ss b){
    return a.v<b.v;
}
inline int lowbit(int x){
    return x&-x;
}
inline ll get(int x){
    ll sum=0;
    for(;x;x-=lowbit(x)){
        sum+=c[x];
        sum%=mod;
    }
    return sum%mod;
}
inline void motify(int x,int k){
    for(;x<=n;x+=lowbit(x)){
        c[x]+=k;
        c[x]%=mod;
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i].v);
        a[i].num=i;
//        d1[i]=a1[i];
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i].v);
        b[i].num=i;
//        d2[i]=a2[i]; 
    }
    sort(a+1,a+n+1,cmp);
    sort(b+1,b+n+1,cmp);
/*    离散化求出a[i]是第几小 并不是第几个 
    int N1=unique(d1+1,d1+n+1)-d1-1;
    int N2=unique(d2+1,d2+n+1)-d2-1;
    for(int i=1;i<=n;i++){
        a1[i]=lower_bound(d1+1,d1+N1+1,a1[i])-d1;
    }
    for(int i=1;i<=n;i++){
        a2[i]=lower_bound(d2+1,d2+N2+1,a2[i])-d2;
    }*/
    for(int i=1;i<=n;i++){
        r[a[i].num]=b[i].num;
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        motify(r[i],1);
        ans+=i-get(r[i]);
        ans%=mod;
    }
    cout<<ans;
    return 0;
}

 

P1966 火柴排队

标签:str   using   long   mod   owb   sum   scanf   n+1   bool   

原文地址:https://www.cnblogs.com/duojiaming/p/11760783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!