P6619 [省选联考 2020 A/B 卷] 冰火战士

https://www.luogu.com.cn/problem/P6619

猜个二分+树状数组

一个小细节多拿 40 分,lowbit 要 define!!!

#include <bits/stdc++.h>
using namespace std;
int rd() {
	int f=1,sum=0; char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
	while(isdigit(ch)) {sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
	return sum*f;
}
#define N (int)(2e6+5) 
#define lowbit(x) (x&(-x))
struct node {
	int op,t,x,y;
}q[N];
int Q,n,m,lsh[N],tot;
int sum1[N],sum2[N];

void add1(int x,int v) {
	while(x<=tot) sum1[x]+=v,x+=lowbit(x); 
}
int qry1(int x) {
	int res=0; while(x) res+=sum1[x],x-=lowbit(x); return res;
}
void add2(int x,int v) {
	while(x<=tot) sum2[x]+=v,x+=lowbit(x); 
}
int qry2(int x) {
	int res=0; while(x) res+=sum2[x],x-=lowbit(x); return res;
}

signed main() {
	Q=rd();
	for(int i=1;i<=Q;i++) {
		q[i].op=rd(); q[i].t=rd();
		if(q[i].op==1) {
			q[i].x=rd(); q[i].y=rd();
			lsh[++tot]=q[i].x;
		}
	}
	sort(lsh+1,lsh+1+tot); tot=unique(lsh+1,lsh+1+tot)-lsh-1;
	for(int i=1;i<=Q;i++) {
		if(q[i].op==1) {
			int v=lower_bound(lsh+1,lsh+1+tot,q[i].x)-lsh;
			if(q[i].t==0) {
//				int v=lower_bound(lsh+1,lsh+1+tot,q[i].x);
				add1(v,q[i].y);
			} else {
				add2(v,q[i].y);
			}
		} else {
			int p=q[i].t,v=lower_bound(lsh+1,lsh+1+tot,q[p].x)-lsh;
			if(q[p].t==0) {
				add1(v,-q[p].y);
			} else {
				add2(v,-q[p].y);
			}
		}
		int l=1,r=tot,res=0,pos;
		while(l<=r) {
			int mid=(l+r)>>1,c1=qry1(mid),c2=qry2(tot)-qry2(mid-1);
			if(min(c1,c2)>res) pos=mid,res=min(c1,c2);
			else if(min(c1,c2)==res) {
				pos=max(pos,mid);
			}
			if(c1<=c2) l=mid+1; else r=mid-1;
		}
		l=pos; r=tot;
		while(l<=r) {
			int mid=(l+r)>>1,c1=qry1(mid),c2=qry2(tot)-qry2(mid-1);
			if(min(c1,c2)==res) pos=max(pos,mid),l=mid+1;
			else r=mid-1;
		}
		if(res==0) printf("Peace\n"); else printf("%d %d\n",lsh[pos],2*res);
	}
	return 0;
}
posted @ 2022-03-20 16:54  FxorG  阅读(42)  评论(0编辑  收藏  举报