hdu 4970
来源:互联网 发布:淘宝客服是真人吗 编辑:程序博客网 时间:2024/04/29 04:38
题目链接: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): 327 Accepted Submission(s): 195
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.
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.
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
521 3 15 5 251 33 15 27 39 10
(1)我最开始的思路:因为有m个塔,每个塔对应着一个区间,区间肯定会有重合,那么某个格子上的伤害就会叠加,要求最后对敌人的总伤害,我用的是线段树成段更新,区间求和;然后给定k个敌人,输入 hp xi 。如果getsum(1,xi,n)比hp大,那么肯定无法存活,统计一下个数;但是TLE了,程序应该没错~但是时间怎么就过不去了~~?
TLE代码:求指点指点~
#include <stdio.h>#include <string.h>#include <cmath>#include <iostream>typedef long long ll;using namespace std;const int maxn=101000;struct node{ int l,r,cover; // 这里的cover的值要么为0 要么为1 ,当cover==1的时候表示这个区间所有的值都相等 ll inc;}data[maxn*5];void build(int l,int r,int k) //建树的过程{ int mid; data[k].l=l; data[k].r=r; data[k].inc=0; data[k].cover=0; if (l==r) { data[k].cover=1; return ; } mid=(l+r)/2; build(l,mid,k*2); build(mid+1,r,k*2+1);}void insert(int k,int l,int r,ll x) { int mid; if (data[k].l==l&&data[k].r==r&&data[k].cover==1) //找到这个区间后,叠加伤害,然后cover==1,表示这个区间内所有数都为x了 { data[k].inc+=x; data[k].cover=1; return ; } if(data[k].cover==1) //这一步的意思是,当所要更改的区间是子区间,那么这个cover等于1就被破坏了,那么我们将这个节点的属性转移给左右节点 { data[2*k].cover=data[k].cover; data[2*k+1].cover=data[k].cover; data[2*k].inc=data[k].inc; data[2*k+1].inc=data[k].inc; data[k].cover=0; data[k].inc=0; } mid=(data[k].l+data[k].r)/2; if(r<=mid) insert(2*k,l,r,x); else if (l>mid) insert(2*k+1,l,r,x); else {insert(2*k,l,mid,x); insert(2*k+1,mid+1,r,x);}}ll sove(int k,int l,int r) { if(data[k].l<=l&&r<=data[k].r) { if(data[k].cover==1) return data[k].inc*(r-l+1); } int mid=(data[k].l+data[k].r)/2; if(l>mid)return sove(2*k+1,l,r); else if(r<=mid)return sove(2*k,l,r); else return sove(2*k,l,mid)+sove(2*k+1,mid+1,r);}int main(){ int n,m,k; int li,ri,xi; ll di,hp; while(scanf("%d",&n)!=EOF) { if(n==0)break; build(1,n,1); scanf("%d",&m); while(m--) { scanf("%d%d%I64d",&li,&ri,&di); insert(1,li,ri,di); } scanf("%d",&k); ll cnt=0; while(k--) { scanf("%I64d%d",&hp,&xi); ll temp=sove(1,xi,n); if(hp-temp>0)cnt++; } printf("%I64d\n",cnt); } return 0;}
(2)下面说下我队友的思路~和解题报告上的思路差不多;
1. 对于 n个格子,如果区间某个塔的攻击范围是[l ,r],攻击力是d, 那么我们就用一个数组a[]来处理一下,a[l]+=d;a[r+1]-=d;
2.用path[]数组来处理,path[i]表示从第一个格子走到第i个格子所受到的总伤害,那么如果要求 从第xi 个格子走到第n个格子所受的伤害,就转化为了 path[n]-path[xi-1];这应该能看懂吧~有点dp的意思;
3.那么就下来看看代码就知道如何求path[i]了;
附上代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <string>#include <cmath>const int maxn=100100;using namespace std;long long a[maxn];long long path[maxn];int main(){ int n,m,k; int li,ri,xi; long long di,hp; while(scanf("%d",&n)!=EOF) { if(n==0)break; scanf("%d",&m); memset(a,0,sizeof(a)); memset(path,0,sizeof(path)); while(m--) { scanf("%d%d%I64d",&li,&ri,&di); a[li]+=di; a[ri+1]-=di; } long long cnt=0; for(int i=1;i<=n;i++) { cnt+=a[i]; path[i]=path[i-1]+cnt; } long long sum=0; scanf("%d",&k); while(k--) { scanf("%I64d%d",&hp,&xi); if(hp-path[n]+path[xi-1]>0)sum++; } printf("%I64d\n",sum); } return 0;}
1 1
- hdu 4970
- hdu 4970
- hdu 4970
- HDU 4970
- 2014多校训练九(HDU 4960 HDU 4961 HDU 4965 HDU 4968 HDU 4969 HDU 4970)
- hdu 4970 Killing Monsters
- HDU - 4970 Killing Monsters
- hdu 4970 Killing Monsters
- HDU 4970 Killing Monsters
- hdu 4970 Killing Monster
- hdu 4970 Killing Monsters
- hdu 4970 trick
- HDU 4970 思路题
- HDU 4970-Killing Monsters
- hdu
- hdu
- HDU
- hdu ()
- 淘宝数据库OceanBase SQL编译器部分 源码阅读--Schema模式
- poj 1546 进制转换
- Linux系统编程之访问文件夹及其文件属性
- xml简单功能
- HDU 1507 Uncle Tom's Inherited Land*(二分图最大匹配:输出一组解)
- hdu 4970
- std::string
- C++11 新特性(5) 统一初始化
- PAT 1045. Favorite Color Stripe
- 多校第九场:贪心+矩阵快速幂中间优化+线性递推&线段树递推
- IOS学习笔记-Category与Extension
- 将画布(canvas)图像保存成本地图片的方法
- PS 色调——颜色运算
- Opencv2与opencv1的区别: