Codeforces 1321E World of Darkraft: Battle for Azathoth

题意

\(n\)个武器,第\(i\)个武器攻击力为\(a_i\),价值\(ca_i\)

\(m\)个防具,第\(i\)个防具防御力为\(b_i\),价值\(cb_i\)

\(p\)个怪,第\(i\)个怪攻击力为\(x_i\),防御力为\(y_i\),价值\(z_i\)

可以选择\(1\)个武器和\(1\)个防具,假设选择第\(i\)个武器和第\(j\)个防具,那么你需要花费\(a_i+b_j\),且可以杀死所有满足攻击力小于\(a_i\)且防御力小于\(b_j\)的怪,并获得这些怪物的价值。

问怎么选择从而使收益最大。

注意必须选择1个武器和1个防具。

解题思路

用权值线段树维护防具,表示选择防御力为\(i\)的防具的收益,初始值为代价的负数。

枚举攻击力,将所有攻击力小于当前枚举值的怪都加入到权值线段树中,加入是为区间加操作,范围是\(\left[当前怪的防御力+1,防御力的最大值\right]\)。注意这里怪可以按照攻击力升序排序以降低复杂度。

现在,权值线段树中的最大值减去选择当前攻击力的代价就是选择当前攻击力的最大收益。在枚举攻击力的过程中更新最大收益即可。

AC代码

#include <bits/stdc++.h>
using namespace std;
  
typedef long long ll;
typedef pair<int,int> pi;
  
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define endl '\n'
  
const double PI=acos(-1.0);
  
namespace IO{
    bool REOF = 1;//为0表示文件结尾
    inline char nc() {
        static char buf[100000], *p1 = buf, *p2 = buf;
        return p1 == p2 && REOF && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? (REOF = 0, EOF) : *p1++;
    }
  
    template<class T>
    inline bool read(T &x) {
        if(!REOF)return false;
        char c = nc();bool f = 0; x = 0;
        while (c<'0' || c>'9')c == '-' && (f = 1), c = nc();
        while (c >= '0'&&c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = nc();
        if(f)x=-x;
        return true;
    }
  
    template<typename T, typename... T2>
    inline bool read(T &x, T2 &... rest) {
        if(!REOF)return false;
        read(x);
        return read(rest...);
    }
  
  
    inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')); }
    // inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || c==' '; }
  
    inline bool read_str(char *a) {
        if(!REOF)return false;
        while ((*a = nc()) && need(*a) && REOF)++a; *a = '\0';
        return true;
    }
  
    inline bool read_dbl(double &x){
        if(!REOF)return false;
        bool f = 0; char ch = nc(); x = 0;
        while(ch<'0'||ch>'9')  {f|=(ch=='-');ch=nc();}
        while(ch>='0'&&ch<='9'){x=x*10.0+(ch^48);ch=nc();}
        if(ch == '.') {
            double tmp = 1; ch = nc();
            while(ch>='0'&&ch<='9'){tmp=tmp/10.0;x=x+tmp*(ch^48);ch=nc();}
        }
        if(f)x=-x;
        return true;
    }
  
    template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
  
    template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
        while(*sdbg!=',')cerr<<*sdbg++;
        cerr<<'='<<h<<','<<' '; _dbg(sdbg+1, a...);
    }
     
    template<class T> ostream &operator<<(ostream& os, vector<T> V) {
        os << "["; for (auto vv : V) os << vv << ","; return os << "]";
    }
  
    template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
        return os << "(" << P.st << "," << P.nd << ")";
    }
     
    #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
}
  
using namespace IO;
const int maxn=2e5+5;
const int maxv=1e6+5;
const int mod=998244353; // 998244353 1e9+7
const ll INF=0x3f3f3f3f3f3f3f3f; // 1e9+7 0x3f3f3f3f 0x3f3f3f3f3f3f3f3f
const double eps=1e-12;
  
int dx[4]={0,1,0,-1};
//int dx[8]={1,0,-1,1,-1,1,0,-1};
int dy[4]={1,0,-1,0};
//int dy[8]={1,1,1,0,0,-1,-1,-1};
 
#define ls (x<<1)
#define rs (x<<1|1)
#define mid ((l+r)>>1)
#define lson ls,l,mid
#define rson rs,mid+1,r
 
  
/**
 * **********     Backlight     **********
 * 仔细读题
 * 注意边界条件
 * 记得注释输入流重定向
 * 没有思路就试试逆向思维
 * 加油,奥利给
 */
int n,m,p;
struct weapon{
	int a,ca;
}a[maxn];
ll A[maxv];
 
struct armor{
	int b,cb;
}b[maxn];
ll B[maxv];
 
struct monster{
	int x,y,z;
	bool operator<(const monster& r){
		if(x==r.x)return y<r.y;
		return x<r.x;
	}
}c[maxn];
 
 
 
 
 
 
 
ll ma[maxv<<2],tag[maxv<<2];
void change(int x,int val){
    ma[x]+=val; tag[x]+=val;
}
 
void push_up(int x){
    ma[x]=max(ma[ls],ma[rs]);
}
 
void push_down(int x){
    if(tag[x]){
        change(ls,tag[x]); change(rs,tag[x]);
        tag[x]=0;
    }
}
 
void build(int x,int l,int r){
    if(l==r){
        ma[x]=-B[l];
        return;
    }
    build(lson); build(rson);
    push_up(x);
}
 
void update(int x,int l,int r,int L,int R,int val){
    if(l==L && r==R){
        change(x,val);
        return;
    }
    push_down(x);
    if(R<=mid)update(lson,L,R,val);
    else if(L>mid)update(rson,L,R,val);
    else{
        update(lson,L,mid,val);
        update(rson,mid+1,R,val);
    }
    push_up(x);
}
 
 
 
 
 
 
 
 
 
void solve(){
	read(n,m,p);
 
	for(int i=1;i<=n;i++)read(a[i].a,a[i].ca);
	for(int i=1;i<=m;i++)read(b[i].b,b[i].cb);
	for(int i=1;i<=p;i++)read(c[i].x,c[i].y,c[i].z);
 
    sort(c+1,c+1+p);
 
    memset(A,0x3f,sizeof(A));
    for(int i=1;i<=n;i++)A[a[i].a]=min(A[a[i].a],(ll)a[i].ca);
 
    memset(B,0x3f,sizeof(B));
    for(int i=1;i<=m;i++)B[b[i].b]=min(B[b[i].b],(ll)b[i].cb);
    for(int i=maxv-2;i>=1;i--)B[i]=min(B[i],B[i+1]);
 
    build(1,1,maxv-1);
 
    ll ans=-INF;
    for(int i=1,j=1;i<=1e6;i++) if(A[i]!=INF){
        while(j<=p && c[j].x<i){
            update(1,1,maxv-1,c[j].y+1,maxv-1,c[j].z);
            j++;
        }
        ans=max(ans,ma[1]-A[i]);
    }
 
	printf("%lld\n",ans);
}
 
int main()
{
    // freopen("in.txt","r",stdin);
    // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    // int _T; read(_T); for(int _=1;_<=_T;_++)solve();
    // while(read(n))solve();
    solve();
    return 0;
}
posted @ 2020-03-03 20:24  _Backl1ght  阅读(207)  评论(0编辑  收藏  举报