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) {
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;
}
__EOF__
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】