codeforces912C. Perun, Ult!

C. Perun, Ult!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A lot of students spend their winter holidays productively. Vlad has advanced very well in doing so! For three days already, fueled by salads and tangerines — the leftovers from New Year celebration — he has been calibrating his rating in his favorite MOBA game, playing as a hero named Perun.

Perun has an ultimate ability called "Thunderwrath". At the instant of its activation, each enemy on the map (n of them in total) loses  health points as a single-time effect. It also has a restriction: it can only activated when the moment of time is an integer. The initial bounty for killing an enemy is . Additionally, it increases by  each second. Formally, if at some second t the ability is activated and the i-th enemy is killed as a result (i.e. his health drops to zero or lower), Vlad earns  units of gold.

Every enemy can receive damage, as well as be healed. There are multiple ways of doing so, but Vlad is not interested in details. For each of n enemies he knows:

  •  — maximum number of health points for the i-th enemy;
  •  — initial health of the enemy (on the 0-th second);
  •  — the amount of health the i-th enemy can regenerate per second.

There also m health updates Vlad knows about:

  •  — time when the health was updated;
  •  — the enemy whose health was updated;
  •  — updated health points for enemyj.

Obviously, Vlad wants to maximize his profit. If it's necessary, he could even wait for years to activate his ability at the right second. Help him determine the exact second (note that it must be an integer) from 0 (inclusively) to  + ∞ so that a single activation of the ability would yield Vlad the maximum possible amount of gold, and print this amount.

Input

In the first line, two integers are given (separated by spaces) — n and m (1 ≤ n ≤ 1050 ≤ m ≤ 105).

In the second line, there are three integers:  and  ().

Each of the following n lines has three integers —  ().

The next m lines contain three integers each —  (). It is guaranteed that there is no more than one hearth change per second for each enemy: more formally, for each a, b so that 1 ≤ a, b ≤ m, a ≠ b holds that if , then .

Output

Output the single integer — the maximum amount of gold Vlad can obtain if he applies "Thunderwrath" exactly once, or -1 if this amount can be infinitely large.

Examples
input
3 2
1000 10 50
70 5 5
90 70 1
110 20 2
20 2 10
30 3 10
output
3000
input
1 1
500 50 1000
750 750 20
10 1 300
output
-1

题解:(贪心+离散化+前缀和)对每个敌人进行预处理,处理出每个敌人的可杀死的区间,可能有多个区间。处理后,将区间的端点进行离散,然后进行前缀和,这样就知道在区间端点处共有多少个敌人。然后枚举敌人的个数找出,端点最远的前缀和即可。复杂度O(n*log(n))

(注意数据为0的情况)。

#include"bits/stdc++.h"
using namespace std;
typedef long long LL;
int inf = 2e9+7;
const int MAXN = 1e5+7;
int n,m,bd,inc,dam;
int maxh[MAXN],hi[MAXN],maxt[MAXN];
int sum[MAXN<<2],cnt[MAXN<<2];

struct node{
    int h,t;
    node(){}
    node(int h, int t): h(h), t(t){}
    bool operator < (const node &b) const{
        return t < b.t;
    }
};
vector<int> id;
vector<int> L[MAXN],R[MAXN];
vector<node> q[MAXN];

int get_id(int x)
{
    return (lower_bound(id.begin(), id.end(), x) - id.begin());
}
void Discrete()
{
    for(int i = 1; i <= n; i++)
        sort(q[i].begin(),q[i].end());
    for(int i = 1; i <= n; i++){
        L[i].push_back(0);
        R[i].push_back(0);
        for(int j = 0;j < q[i].size(); j++){
            int l = q[i][j].t, r;
            if(dam < q[i][j].h) r = l;
            else{
                if(hi[i] == 0){
                    if(j+1 < q[i].size()) r = q[i][j+1].t;
                    else r = inf;
                }
                else{
                    int ti = (dam-q[i][j].h)/hi[i];
                    r = l+ti+1;
                }
            }
            int sz = R[i].size();
            if(l <= R[i][sz-1]) R[i][sz-1] = r;
            else{
                L[i].push_back(l);
                R[i].push_back(r);
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < L[i].size(); j++){
            id.push_back(L[i][j]);
            id.push_back(R[i][j]);
        }
    }
    sort(id.begin(), id.end());
    id.erase( unique(id.begin(), id.end()), id.end() );
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < L[i].size(); j++){
            cnt[get_id(L[i][j])]++;
            cnt[get_id(R[i][j])]--;
        }
    }
}

int main()
{
#ifdef __LOCAL__
    freopen("input.txt","r",stdin);
#endif // __LOCAL__
    int minh = 0x3f3f3f3f;
    scanf("%d%d",&n,&m);
    scanf("%d%d%d",&bd,&inc,&dam);
    for(int i = 1; i <= n; i++){
        int h;
        scanf("%d%d%d",&maxh[i],&h,&hi[i]);
        minh = min(minh,maxh[i]);
        q[i].push_back(node(h,0));
    }
    for(int i = 1; i <= m; i++){
        int t,id,h;
        scanf("%d%d%d",&t,&id,&h);
        q[id].push_back(node(h,t));
    }
    if(minh <= dam && inc != 0){
        puts("-1");
        return 0;
    }


    Discrete();
    if(id[id.size()-1] == inf && inc != 0){
        puts("-1");
        return 0;
    }
    memset(maxt,-1,sizeof(maxt));
    sum[0] = cnt[0];
    maxt[sum[0]] = max(maxt[sum[0]],id[0]);
    for(int i = 1; i < id.size(); i++){
        maxt[sum[i-1]] = max(maxt[sum[i-1]],id[i]-1);
        sum[i] = sum[i-1] + cnt[i];
    }

    LL ans = 0;
    for(LL i = 1; i <= n; i++)
        if(maxt[i] != -1)
            ans = max(ans,i*(bd+(LL)inc*maxt[i]));
    cout<<ans<<endl;
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值