E. Enemy is weak (BIT)

Posted Harris-H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了E. Enemy is weak (BIT)相关的知识,希望对你有一定的参考价值。

E. Enemy is weak (BIT)

三元组逆序对个数,显然对每个中点 维护前驱大于它的数,和后缀小于它的数。

BIT扫两遍即可。

// Problem: E. Enemy is weak
// Contest: Codeforces - Codeforces Beta Round #57 (Div. 2)
// URL: https://codeforces.ml/problemset/problem/61/E
// Memory Limit: 256 MB
// Time Limit: 5000 ms
// Date: 2021-08-04 10:55:17
// --------by Herio--------

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; 
const int N=1e6+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define fi first
#define se second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define ios ios::sync_with_stdio(false),cin.tie(0) 
void Print(int *a,int n){
	for(int i=1;i<n;i++)
		printf("%d ",a[i]);
	printf("%d\\n",a[n]); 
}
struct BIT{
	#define lowbit(x) x&(-x)
	#define il inline
	ll s[N];
	int n;
	il void upd(int x,int v){
		while(x<=n){
			s[x]+=v;x+=lowbit(x);
		}return;
	}
	il ll que(int x){
		ll ans=0;
		while(x){
			ans+=s[x];x-=lowbit(x);
		}return ans;
	}
}T;
int n,a[N],b[N],m;
void init(){
	sort(b+1,b+n+1);
	m=unique(b+1,b+n+1)-b-1;
	for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+m+1,a[i])-b;
	T.n=m;
}
int pre[N],suf[N];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
	init();
	for(int i=1;i<=n;i++){
		pre[i]=T.que(m)-T.que(a[i]);
		T.upd(a[i],1);
	}
	for(int i=1;i<=m;i++) T.s[i]=0;
	ll ans=0;
	for(int i=n;i;i--){
		suf[i]=T.que(a[i]-1);
		ans+=1LL*pre[i]*suf[i];
		T.upd(a[i],1);
	}
	printf("%lld\\n",ans);
	return 0;
}

以上是关于E. Enemy is weak (BIT)的主要内容,如果未能解决你的问题,请参考以下文章

题解-Enemy is weak

Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) E. Buy Low Sell High(代码片

ABC #150 E. Change a Little Bit

What is the bottleneck and weakness of AlphaGo?

Error in value[[3L]](cond) : You have a 32-bit version of Java. H2O works best with 64-bit Java.(代码片

Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(代码片