送花[树状数组+二分]

传送门

以c为下标插入树状数组 , 然后二分求最大最小 , 按题目要求删除,插入就好


#include<bits/stdc++.h>
#define N 1000050
#define LL long long
using namespace std;
int c[N],w[N],tot; LL ans1,ans2;
void Up(int x,int val){for(;x<=N-50;x+=x&-x)c[x]+=val;}
int Q(int x){int ans=0; for(;x;x-=x&-x) ans+=c[x];return ans;}
void Delete_max(){
	int l=0,r=N;
	while(l<r){
		int mid=(l+r)>>1;
		if(Q(mid)>=tot) r=mid;
		else l=mid+1;
	}Up(l,-1);
}
void Delete_min(){
	int l=0,r=N;
	while(l<r){
		int mid=(l+r+1)>>1;
		if(Q(mid)<=0) l=mid;
		else r=mid-1;
	}Up(l+1,-1);
}
int main(){
	int op,x,y; while(1){
		scanf("%d",&op); 
		if(op==-1) break;
		if(op==1){
			scanf("%d%d",&x,&y);
			if(Q(y)-Q(y-1)==1) continue;
			w[y]=x , Up(y,1) , tot++;
		}
		if(op==2 && Q(N-50)) Delete_max(),tot--;
		if(op==3 && Q(N-50)) Delete_min(),tot--;
	}
	for(int i=1;i<=N-50;i++) if(Q(i)-Q(i-1)==1) ans1+=(LL)w[i],ans2+=(LL)i;
	printf("%lld %lld",ans1,ans2); return 0;
}

猜你喜欢

转载自blog.csdn.net/sslz_fsy/article/details/84673010