HDU 4970 Killing Monsters(树状数组)

Killing Monsters

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2838    Accepted Submission(s): 1068


Problem 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.
 

 

Author
SYSU
 
题目大意:在长度为N的路上,有M个攻击塔,给定M个攻击台的攻击范围和伤害,现在有Q只怪物,给出Q只怪物的HP和出现位置,怪物固定向右进攻,问所有有多少只怪物活着。
 
 1 #include <cstdio>  
 2 #include <cstring>  
 3 #include <iostream>
 4 #define lowbit(x) (x&(-x))  
 5 
 6 using namespace std;
 7 
 8 int n, m, k, l, r, d, x, ans;
 9 long long sum[100005], a[100005], h;
10 
11 void update(int i, int v) 
12 {
13     while (i <= n) 
14     {
15         a[i] += v;
16         i += lowbit(i);
17     }
18 }
19 
20 long long get_sum(int i) //求的是该点的和
21 {
22     long long all = 0;
23     while (i>0) 
24     {
25         all += a[i];
26         i -= lowbit(i);
27     }
28     return all;
29 }
30 
31 int main() 
32 {
33     while (scanf("%d", &n), n != 0) 
34     {
35         scanf("%d", &m);
36         memset(a, 0, sizeof(a));
37         while (m-->0) 
38         {
39             scanf("%d%d%d", &l, &r, &d);
40             update(l, d);//类似气球贴标签,这样求前缀和就是该数对应块上的炮弹叠加和
41             update(r + 1, -d);
42         }
43         sum[n + 1] = 0;
44         for (int i = n; i>0; --i)
45         {
46             sum[i] = sum[i + 1] + get_sum(i);
47             
48         }
49         /*for (int i = 1; i <= n; i++)
50             cout << get_sum(i) << endl;*/
51         scanf("%d", &k);
52         ans = 0;
53         while (k-->0) 
54         {
55             scanf("%I64d%d", &h, &x);
56             if (h>sum[x])
57                 ++ans;
58         }
59         printf("%d\n", ans);
60     }
61     return 0;
62 }

 

巧妙的数组(类似贴气球)

 

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

 

做完后查看题解,发现本题查询时很有规律,所以不必使用树状数组,读入数据时,对damage数组进行如下操作:damage[L]+=d,damage[R+1]-=d,表示区间[L,R]内点每个点都会加上d,最后正着扫一边,即可得到i点能造成点伤害damage[i],再倒着扫一边,即可得到区间[i,n]能造成点伤害总和damage[i]

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 const int maxn = 1e5 + 5;
 7 typedef unsigned long long ll;
 8 
 9 int N, M;
10 ll v[maxn];
11 
12 void init() 
13 {
14     int l, r, val;
15 
16     scanf("%d", &M);
17     memset(v, 0, sizeof(v));
18     for (int i = 0; i < M; i++) 
19     {
20         scanf("%d%d%d", &l, &r, &val);
21         v[l] += val;
22         v[r + 1] -= val;
23     }
24 
25     ll mv = 0;
26     for (int i = 0; i <= N; i++) 
27     {
28         mv += v[i];
29         v[i] = mv;
30     }
31 
32     for (int i = 1; i <= N; i++)
33         v[i] += v[i - 1];
34 }
35 
36 int main() 
37 {
38 
39     while (scanf("%d", &N) == 1 && N) 
40     {
41         init();
42 
43         scanf("%d", &M);
44 
45         int pos, ret = 0;
46         ll HP;
47         for (int i = 0; i < M; i++) 
48         {
49             scanf("%I64d%d", &HP, &pos);
50             if (HP > v[N] - v[pos - 1])
51                 ret++;
52         }
53         printf("%d\n", ret);
54     }
55     return 0;
56 }
View Code

 

posted on 2018-03-01 17:11  蔡军帅  阅读(145)  评论(0编辑  收藏  举报