Killing Monsters(树状数组)

B - Killing Monsters

Time Limit: 1000 MS Memory Limit: 131072 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status]

Description

Kingdom Rush is a popular TD game, in which you should build some towers to protect your kingdom from monsters. And now another wave of monsters is coming and you need again to know whether you can get through it.

The path of monsters is a straight line, and there are N blocks on it (numbered from 1 to N continuously). Before enemies come, you have M towers built. Each tower has an attack range [L, R], meaning that it can attack all enemies in every block i, where L<=i<=R. Once a monster steps into block i, every tower whose attack range include block i will attack the monster once and only once. For example, a tower with attack range [1, 3] will attack a monster three times if the monster is alive, one in block 1, another in block 2 and the last in block 3.

A witch helps your enemies and makes every monster has its own place of appearance (the ith monster appears at block Xi). All monsters go straightly to block N.

Now that you know each monster has HP Hi and each tower has a value of attack Di, one attack will cause Di damage (decrease HP by Di). If the HP of a monster is decreased to 0 or below 0, it will die and disappear.
Your task is to calculate the number of monsters surviving from your towers so as to make a plan B.

Input

The input contains multiple test cases.

The first line of each case is an integer N (0 < N <= 100000), the number of blocks in the path. The second line is an integer M (0 < M <= 100000), the number of towers you have. The next M lines each contain three numbers, Li, Ri, Di (1 <= Li <= Ri <= N, 0 < Di <= 1000), indicating the attack range [L, R] and the value of attack D of the ith tower. The next line is an integer K (0 < K <= 100000), the number of coming monsters. The following K lines each contain two integers Hi and Xi (0 < Hi <= 10^18, 1 <= Xi <= N) indicating the ith monster’s live point and the number of the block where the ith monster appears.

The input is terminated by N = 0.

Output

Output one line containing the number of surviving monsters.

Sample Input

5
2
1 3 1
5 5 2
5
1 3
3 1
5 2
7 3
9 1
0

Sample Output

3

Hint
In the sample, three monsters with origin HP 5, 7 and 9 will survive.

题意:

在N个格子中有M个攻击范围为[Li,Ri]、攻击值为Di塔。问出生在位置xi的血量为hi的怪物能否走到N。(每走一个格子,在攻击范围内的塔都能,且只能攻击一次)

思路:

树状数组,一般来说,常用树状数组求前缀和,但在此处求的是后缀和,即是从每个点到N点要花费的血量,用怪物hp与需要的血量比较比较。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long
#define lowbit(x) x&(-x)
using namespace std;
LL tree[100005], sum[100005], N;
void add(LL x, LL y)
{
    for(LL i=x; i>0; i-=lowbit(i)) ///求前缀和时是i+=lowbit(i) 在此处是i-=lowbit(i)表示每次更新一个节点时相应的改变之前的节点值
        tree[i] += y;
}
LL getsum(LL x)  
{
    LL sum = 0;
    for(LL i=x; i<=N; i+=lowbit(i))    ///i+=lowbit(i)代表从x点到N点的和
        sum += tree[i];
    return sum;
}
int main()
{
    LL M, K;
    while(scanf("%lld", &N) && N)
    {
        memset(sum, 0, sizeof(sum));
        memset(tree, 0, sizeof(tree));
        scanf("%lld", &M);
        for(int i=0; i<M; i++){
            LL l, r, d;
            scanf("%lld%lld%lld", &l, &r, &d);
            add(r, d);
            add(l-1, -d);
        }
        sum[N+1] = 0;
        for(int i=N; i>0; i--)
            sum[i] = sum[i+1] + getsum(i);
        LL ans = 0;
        scanf("%lld", &K);
        for(int i=0; i<K; i++){
            LL hp, x;
            scanf("%lld%lld", &hp, &x);
            if(sum[x] <= hp) ans ++;
        }
        printf("%lld\n", ans);
    }
}

猜你喜欢

转载自blog.csdn.net/a_thinking_reed_/article/details/80219657