【BZOJ】【P3192】【JLOI2013】【删除物品】【题解】【BIT】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3192

感觉最近状态不好啊……一边给bc出题一边打aicraft,刷题也只能刷这种水题……

显然此题就是裸模拟

把两个堆连接起来,BIT模拟即可

Code:

#include<bits/stdc++.h>
#define fst first
#define sec second
#define lowbit(x) (x&-x)
using namespace std;
const int maxn=1e5+5;
typedef pair<int,int> pi;
int n1,n2,n;
pi a[maxn];
int d[maxn];
void updata(int x,int f){while(x<=n)d[x]+=f,x+=lowbit(x);}
int get(int x){int o=0;while(x)o+=d[x],x-=lowbit(x);return o;}
int main(){
	scanf("%d%d",&n1,&n2);n=n1+n2+1;
	for(int i=n1;i>=1;i--)scanf("%d",&a[i].fst);
	for(int i=n1+2;i<=n;i++)scanf("%d",&a[i].fst);
	int cur=n1+1;a[cur].sec=cur;
	for(int i=1;i<=n;i++)if(i!=cur)updata(i,1),a[i].sec=i;
	sort(a+1,a+1+n,greater<pi>());
	long long ans=0;
	for(int i=1;i<n;i++){
		int l=a[i].sec,r=cur;
		if(l>r)swap(l,r);
		ans+=get(r)-get(l-1)-1;
		cur=a[i].sec;updata(cur,-1);
	}cout<<ans<<endl;
	return 0;
}


你可能感兴趣的:(bzoj)