很像二维偏序,我们把武器和怪物放在一起排序按照攻击力递增的顺序,顺序扫描,这样攻击力满足了要求,然后防御力可以用一个线段树来处理,具体看代码
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> #define forn(i, n) for (int i = 0 ; i < int(n) ; i++) #define fore(i, s, t) for (int i = s ; i < (int)t ; i++) #define fi first #define se second #define all(x) x.begin(),x.end() #define pf2(x,y) printf("%d %d ",x,y) #define pf(x) printf("%d ",x) #define each(x) for(auto it:x) cout<<it<<endl; #define pii pair<int,int> #define sc(x) scanf("%d",&x) using namespace std; typedef long long ll; const int maxn=1e6+5; int vv[maxn<<2],lz[maxn<<2]; #define ls o<<1 #define rs o<<1|1 #define lss ls,l,mid #define rss rs,mid+1,r void push_up(int o,int l,int r){ if(lz[o]){ lz[ls]+=lz[o]; lz[rs]+=lz[o]; vv[ls]+=lz[o]; vv[rs]+=lz[o]; lz[o]=0; } } void update(int o,int l,int r,int ql,int qr,int val){ if(l>=ql && r<=qr) { vv[o]+=val; lz[o]+=val; return ; } else { push_up(o,l,r); int mid=l+r>>1; if(ql<=mid) update(lss,ql,qr,val); if(qr>mid) update(rss,ql,qr,val); vv[o]=max(vv[ls],vv[rs]); } } int n,m,p; struct Weapons{ int a,ca; }weapon[maxn]; struct Armor{ int b,cb; }armor[maxn]; struct Monsters{ int x,y,z; }monster[maxn]; void build(int o,int l,int r){ if(l==r) vv[o]=-armor[l].cb; else { int mid=l+r>>1; build(lss); build(rss); vv[o]=max(vv[ls],vv[rs]); } } int Find(int x){ int l=1,r=m+1; while(l<r){ int mid=l+r>>1; if(armor[mid].b<x) l=mid+1; else r=mid; } return l; } int main(){ cin>>n>>m>>p; for(int i=1;i<=n;i++){ scanf("%d%d",&weapon[i].a,&weapon[i].ca); } for(int i=1;i<=m;i++){ scanf("%d%d",&armor[i].b,&armor[i].cb); } for(int i=1;i<=p;i++){ scanf("%d%d%d",&monster[i].x,&monster[i].y,&monster[i].z); } sort(weapon+1,weapon+n+1,[](Weapons X,Weapons Y){ return X.a<Y.a; }); sort(armor+1,armor+m+1,[](Armor X,Armor Y){ return X.b<Y.b; }); sort(monster+1,monster+p+1,[](Monsters X,Monsters Y){ return X.x<Y.x; }); build(1,1,m); int ans=-2e9,tl=1,tr=1; for(int i=1;i<=n+p;i++){ if(tr>p || (tl<=n && weapon[tl].a<=monster[tr].x)) { ans=max(ans,vv[1]-weapon[tl].ca); ++tl; } else { int pos=Find(monster[tr].y+1); if(pos<=m) { update(1,1,m,pos,m,monster[tr].z); } ++tr; } } printf("%d ",ans); }