此处有
目录↑
Killing Monsters
http://acm.hdu.edu.cn/showproblem.php?pid=4970
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 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.
解法一:树状数组
刚开始没仔细看题,以为给的L和R给点是拓展的距离,很奇怪这么多人很快就AC,再次读题时发现L和R指点是坐标,然后就想到树状数组可解(区间更新)
#include <cstdio>
#include <cstring>
#define lowbit(x) (x&(-x))
using namespace std;
int n,m,k,l,r,d,x,ans;
long long sum[100005],damage[100005],h;
void update(int i,int v) {
while(i<=n) {
damage[i]+=v;
i+=lowbit(i);
}
}
long long getSum(int i) {
long long all=0;
while(i>0) {
all+=damage[i];
i-=lowbit(i);
}
return all;
}
int main() {
while(scanf("%d",&n),n!=0) {
scanf("%d",&m);
memset(damage,0,sizeof(damage));
while(m-->0) {
scanf("%d%d%d",&l,&r,&d);
update(l,d);
update(r+1,-d);
}
sum[n+1]=0;
for(int i=n;i>0;--i)
sum[i]=sum[i+1]+getSum(i);
scanf("%d",&k);
ans=0;
while(k-->0) {
scanf("%I64d%d",&h,&x);
if(h>sum[x])
++ans;
}
printf("%d\n",ans);
}
return 0;
}
解法二:数组模拟(区间更新思想)
做完后查看题解,发现本题查询时很有规律,所以不必使用树状数组,读入数据时,对damage数组进行如下操作:damage[L]+=d,damage[R+1]-=d,表示区间[L,R]内点每个点都会加上d,最后正着扫一边,即可得到i点能造成点伤害damage[i],再倒着扫一边,即可得到区间[i,n]能造成点伤害总和damage[i]
#include <cstdio>
#include <cstring>
using namespace std;
int n,m,k,l,r,d,x,ans;
long long damage[100005],h;
int main() {
while(scanf("%d",&n),n!=0) {
scanf("%d",&m);
memset(damage,0,sizeof(damage));
while(m-->0) {
scanf("%d%d%d",&l,&r,&d);
damage[l]+=d;
damage[r+1]-=d;
}
for(int i=2;i<=n;++i)
damage[i]+=damage[i-1];
for(int i=n-1;i>0;--i)
damage[i]+=damage[i+1];
scanf("%d",&k);
ans=0;
while(k-->0) {
scanf("%I64d%d",&h,&x);
if(h>damage[x])
++ans;
}
printf("%d\n",ans);
}
return 0;
}