hdu 4970 Killing Monsters(高效)

题目链接:hdu 4970 Killing Monsters

题目大意:在长度为N的路上,有M个攻击塔,给定M个攻击台的攻击范围和伤害,现在有Q只怪物,给出Q只怪物的HP和出现位置,怪物固定向右进攻,问所有有多少只怪物活着。

解题思路:对于攻击台,l,r,val,在v[l]处加上val,v[r+1]处减掉val。然后遍历一遍v数组递推出各个点的伤害。并且处理处伤害的前缀和。然后对于每只怪物去判断。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 1e5+5;
typedef unsigned long long ll;

int N, M;
ll v[maxn];

void init () {
    int l, r, val;

    scanf("%d", &M);
    memset(v, 0, sizeof(v));
    for (int i = 0; i < M; i++) {
        scanf("%d%d%d", &l, &r, &val);
        v[l] += val;
        v[r+1] -= val;
    }

    ll mv = 0;
    for (int i = 0; i <= N; i++) {
        mv += v[i];
        v[i] = mv;
    }

    for (int i = 1; i <= N; i++)
        v[i] += v[i-1];
}

int main () {

    while (scanf("%d", &N) == 1 && N) {
        init();

        scanf("%d", &M);

        int pos, ret = 0;
        ll HP;
        for (int i = 0; i < M; i++) {
            scanf("%I64d%d", &HP, &pos);
            if (HP > v[N] - v[pos-1])
                ret++;
        }
        printf("%d\n", ret);
    }
    return 0;
}

你可能感兴趣的:(hdu 4970 Killing Monsters(高效))