61E - Enemy is weak(思维+树状数组)

https://codeforces.com/problemset/problem/61/E


思路:

枚举j,然后前面的跑逆序对,后面的跑一个后缀的值域树状数组。后缀的边扫边减。

数字大了离散化。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define lowbit(x) x&(-x)
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e6+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL tree1[maxn],tree2[maxn];
LL a[maxn],b[maxn];
LL add(LL tree[],LL x,LL d){
    while(x<maxn){
        tree[x]+=d;
        x+=lowbit(x);
    }
}
LL query(LL tree[],LL x){
    LL sum=0;
    while(x>0){
       sum+=tree[x];
       x-=lowbit(x);
    }
    return sum;
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   for(LL i=1;i<=n;i++){
      cin>>a[i];b[i]=a[i];
   }
   sort(b+1,b+1+n);
   LL siz=unique(b+1,b+1+n)-b-1;
   for(LL i=1;i<=n;i++){
       LL num=lower_bound(b+1,b+1+siz,a[i])-b;
       add(tree2,num,1);
   }
   LL ans=0;
   ///前面逆序对
   for(LL j=1;j<=n;j++){
      LL num=lower_bound(b+1,b+1+siz,a[j])-b;
      add(tree1,num,1);
      LL res1=j-query(tree1,num);
      add(tree2,num,-1);
      LL res2=query(tree2,num-1);
      ans+=res1*res2;
   }
   cout<<ans<<"\n";
   return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/115385664
今日推荐