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
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
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.
monsters.
2
1 3 1
5 5 2
5
1 3
3 1
5 2
7 3
9 1
0
In the sample, three monsters with origin HP 5, 7 and 9 will survive.
#include <cstdio>
#include <cstring>
#include <iostream>
#define lowbit(x) (x&(-x)) using namespace std; int n, m, k, l, r, d, x, ans;
long long sum[], a[], h; void update(int i, int v)
{
while (i <= n)
{
a[i] += v;
i += lowbit(i);
}
} long long get_sum(int i) //求的是该点的和
{
long long all = ;
while (i>)
{
all += a[i];
i -= lowbit(i);
}
return all;
} int main()
{
while (scanf("%d", &n), n != )
{
scanf("%d", &m);
memset(a, , sizeof(a));
while (m-->)
{
scanf("%d%d%d", &l, &r, &d);
update(l, d);//类似气球贴标签,这样求前缀和就是该数对应块上的炮弹叠加和
update(r + , -d);
}
sum[n + ] = ;
for (int i = n; i>; --i)
{
sum[i] = sum[i + ] + get_sum(i); }
/*for (int i = 1; i <= n; i++)
cout << get_sum(i) << endl;*/
scanf("%d", &k);
ans = ;
while (k-->)
{
scanf("%I64d%d", &h, &x);
if (h>sum[x])
++ans;
}
printf("%d\n", ans);
}
return ;
}
巧妙的数组(类似贴气球)
解题思路:对于攻击台,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]
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = 1e5 + ;
typedef unsigned long long ll; int N, M;
ll v[maxn]; void init()
{
int l, r, val; scanf("%d", &M);
memset(v, , sizeof(v));
for (int i = ; i < M; i++)
{
scanf("%d%d%d", &l, &r, &val);
v[l] += val;
v[r + ] -= val;
} ll mv = ;
for (int i = ; i <= N; i++)
{
mv += v[i];
v[i] = mv;
} for (int i = ; i <= N; i++)
v[i] += v[i - ];
} int main()
{ while (scanf("%d", &N) == && N)
{
init(); scanf("%d", &M); int pos, ret = ;
ll HP;
for (int i = ; i < M; i++)
{
scanf("%I64d%d", &HP, &pos);
if (HP > v[N] - v[pos - ])
ret++;
}
printf("%d\n", ret);
}
return ;
}
HDU 4970 Killing Monsters(树状数组)的更多相关文章
- hdu 4970 Killing Monsters(数组的巧妙运用) 2014多校训练第9场
pid=4970">Killing Monsters ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
- hdu 5775 Bubble Sort 树状数组
Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- HDU - 1541 Stars 【树状数组】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...
- HDU 3854 Glorious Array(树状数组)
题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前 ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
随机推荐
- python decorator 装饰器
python装饰器是个对嵌套函数的语法糖 作用是在函数调用方法不变的情况下,将函数包装成另一个函数来使用 ---- import time def sum1(): sum = 1 + 2 print ...
- 虚拟机下Redhat9 网络配置问题(转)
原文链接:http://www.programgo.com/article/38031929690/ edhat 9/redhat as 3装在虚拟机vmware上之后,连接网络是出现问题 Deter ...
- flask中过滤器的使用
过滤器 过滤器的本质就是函数.有时候我们不仅仅只是需要输出变量的值,我们还需要修改变量的显示,甚至格式化.运算等等,而在模板中是不能直接调用 Python 中的某些方法,那么这就用到了过滤器. 使用方 ...
- js获取来源网址
举例: 1. a.html文件内容如下: <a href="b.html">浏览b.html </a> 2. b.html文件中的内容如下: <bod ...
- Linux:vim环境设置
vim环境设置 vim的环境设置在/etc/vimrc的这个文件中,不过不建议直接修改该配置文件.但是可以修改~/.vimrc文件,默认是不存在的,要手动创建并写入设置值. set hlsearch ...
- 自己WIN7旗舰版安装 SQLServer2005/2008的一些总结
准备工作:下载安装包,当然要保证安装包能用: 安装:1.设置setup.exe文件 右键属性选择 --兼容,兼容下面选择---以管理员方式运行,---兼容模式选择windows xp或者windows ...
- 在ROS Kinetic中使用Gazebo 8进行机器人仿真
在ROS Kinetic中使用Gazebo 8比在ROS Indigo中使用Gazebo 3-8要容易一些. 目前最新稳定版本的Gazebo8为8.1.1. 安装流程如下: $ sudo apt-g ...
- C#单元测试Nunit小结(20141018)
1. 下载和安装Nunit工具: NUnit是一款堪与JUnit齐名的开源的回归测试框架,供.net开发人员做单元测试之用,可以从www.nunit.org网站上免费获得,然后在系统中安装: 2. 在 ...
- 新浪云使用smarty模板的方法
在部署到sina app engine(sae)上时出现了问题,因为sae作为云计算平台式无法进行文件读写操作的,所以Smarty中输出的缓存文件就无法实现. 错误信息:“SAE_Fatal_erro ...
- 实现Python代码发送邮件
import smtplib from email.mime.text import MIMEText from email.utils import formataddr msg = MIMETex ...