hdu4970 Killing Monsters (差分数列)

2014多校9 1011

http://acm.hdu.edu.cn/showproblem.php?pid=4970

Killing Monsters

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

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.

Source

2014 Multi-University Training Contest 9

Recommend

hujie   |   We have carefully selected several similar problems for you:  4970 4969 4968 4967 4966

题意:塔防,怪走一条直线,可以分成1~n共n格。给出m个塔的攻击范围(Li~Ri),攻击力Di,怪物走过这格会减少Di血量。给出k个怪物的血量、出生格,求有多少个怪物可以走到终点。

题解:差分数列搞。

粗略一看,是区间加减、区间求和,线段树!会超时,怕了。

再一看,是区间加减完再区间求和,而且求和还是有限制的,就求i~n的和。

用差分数列可以轻松区间加减,差分数列就是b[i]=a[i]-a[i-1],区间[i,j]加D就是b[i]=b[i]+d  ,  b[j+1]=b[j+1]-d。

但是怎么求和呢?我们把和写出来观察一下:

        an = bn

    an-1 + an =2an - bn

an-2 + an-1 + an = 3an - 2bn - bn-1

看起来很好算的样子!

于是这样就能算(其中an就是an ,其中c[i]就是ai加到an的和):

1         ll one=an,many=an;
2         for(i=n;i>0;i--){
3             c[i]=many;
4             one-=b[i];
5             many+=one;
6         }

这样就轻松算啦。

我一开始想到差分数列,不过没仔细想怎么求和,然后就去线段树了,逗乐。后来才发现居然这么好求。

全代码:

 1 //#pragma comment(linker, "/STACK:102400000,102400000")
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<map>
 9 #include<set>
10 #include<stack>
11 #include<queue>
12 using namespace std;
13 #define ll long long
14 #define usll unsigned ll
15 #define mz(array) memset(array, 0, sizeof(array))
16 #define minf(array) memset(array, 0x3f, sizeof(array))
17 #define REP(i,n) for(i=0;i<(n);i++)
18 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
19 #define RD(x) scanf("%d",&x)
20 #define RD2(x,y) scanf("%d%d",&x,&y)
21 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
22 #define WN(x) prllf("%d\n",x);
23 #define RE  freopen("D.in","r",stdin)
24 #define WE  freopen("1biao.out","w",stdout)
25 #define mp make_pair
26 #define pb push_back
27
28 const int maxn=111111;
29
30 int n,m,k;
31 int l[maxn],r[maxn],d[maxn];
32 int x[maxn];
33 ll h[maxn];
34 ll b[maxn];
35 ll c[maxn];
36 ll an=0;
37
38 void Update(int L, int R, int x){
39     b[L]+=x;
40     b[R+1]-=x;
41     if(R==n)an+=x;
42 }
43
44 int main(){
45     int i;
46     while(scanf("%d",&n)!=EOF){
47         if(n==0)break;
48         scanf("%d",&m);
49         mz(b);mz(c);an=0;
50         REP(i,m) {
51             scanf("%d%d%d",&l[i],&r[i],&d[i]);
52             Update(l[i],r[i],d[i]);
53         }
54         ll one=an,many=an;
55         for(i=n;i>0;i--){
56             c[i]=many;
57             one-=b[i];
58             many+=one;
59         }
60         //for(i=1;i<=n;i++)printf("%I64d\n",c[i]);
61         int ans=0;
62         //for(i=1;i<=n;i++)printf("(%d,%d),%d\n",i,n,Query(i,n,1,n,1));
63         scanf("%d",&k);
64         REP(i,k) {
65             scanf("%I64d%d",&h[i],&x[i]);
66             if(c[x[i]]< h[i])ans++;
67         }
68         printf("%d\n",ans);
69     }
70     return 0;
71 }

hdu4970 Killing Monsters (差分数列),布布扣,bubuko.com

时间: 2024-07-30 13:49:45

hdu4970 Killing Monsters (差分数列)的相关文章

HDU4970 Killing Monsters dp

题意:给你n个操作,每一次对区间相加,询问区间和. 解题思路:这里没有动态更新, 所以我们用括号匹配那种方法就行 就是 a[l] + x  ,a[r+1] -x 这种做法. 解题代码: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #define MAX 100005 6 #define LL long long 7 LL a[10

【HDU4970】Killing Monsters

HDU - 4970 Killing Monsters 先输入一个n表示一共有n个格子,然后输入k表示有k个攻击塔,紧跟k行数据,每行三个数字l,r,d,表示该攻击塔可以在l到r范围内造成d的伤害,然后输入一个m后面跟m行,每行两个数字h,x分别表示该怪物的血量和位置,怪物们将从他的位置x走到n,问能活着走到的有几只怪物. dalao说这是一种懒操作 线性区间更新的懒操作 将l~r的区间每个都加d 我们只想知道每个点的具体值 所以就不用树状数组.线段树什么的 只用将它a[l]+d,a[r+1]-

HDU - 4970 Killing Monsters

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 mons

hdu 4970 Killing Monsters(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4970 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 k

HDU 4970 Killing Monsters 【搜索】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4970 题目大意:给你一些防御塔的位置和其攻击力,以及一些怪物的血量和位置,问走到终点还有几个怪物活着. 题目思路:最开始看题目的时候就是区间更新的过程觉得会花很多时间,然后用的树状数组,后来发现用的一个机智方法可以过,简单了很多. 区间更新的主要时间是花在塔的伤害,(L,R)D伤害上面,我们用一个sttack数组记录伤害,在attack[L]+=D,在attack[R]-=D,然后从前往后扫一遍,可

【HDOJ 4970】 Killing Monsters

[HDOJ 4970] Killing Monsters 数据很大 立马想预处理 每只怪物会从点x出现移动到点n(终点) 问能剩几只怪物 预处理求出每个位置到终点所受伤害 出现一只怪物直接判断死活即可 代码如下: #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <queue>

HDOJ 4970 Killing Monsters

明明扫一遍的题目,比赛的时候居然用线段树...TLE 5发... Killing Monsters Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 225    Accepted Submission(s): 128 Problem Description Kingdom Rush is a popular TD game, in

hdu 4970 Killing Monsters(简单题) 2014多校训练第9场

Killing Monsters                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Kingdom Rush is a popular TD game, in which you should b

HDU 4970(杭电多校#9 1011题)Killing Monsters(瞎搞)

题目地址:HDU 4970 先进行预处理,在每个炮塔的火力范围边界标记一个点.然后对每个点的伤害值扫一遍就能算出来.然后在算出每个点到终点的总伤害值,并保存下来,也是扫一遍即可.最后在询问的时候直接判断即可,复杂度O(2*n). 代码如下: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define maxn 110000 #define LL __int6