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; }

__EOF__

本文作者F x o r G
本文链接https://www.cnblogs.com/xugangfan/p/16030525.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   FxorG  阅读(46)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示