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
 
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的和):

         ll one=an,many=an;
for(i=n;i>;i--){
c[i]=many;
one-=b[i];
many+=one;
}

这样就轻松算啦。

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

全代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back const int maxn=; int n,m,k;
int l[maxn],r[maxn],d[maxn];
int x[maxn];
ll h[maxn];
ll b[maxn];
ll c[maxn];
ll an=; void Update(int L, int R, int x){
b[L]+=x;
b[R+]-=x;
if(R==n)an+=x;
} int main(){
int i;
while(scanf("%d",&n)!=EOF){
if(n==)break;
scanf("%d",&m);
mz(b);mz(c);an=;
REP(i,m) {
scanf("%d%d%d",&l[i],&r[i],&d[i]);
Update(l[i],r[i],d[i]);
}
ll one=an,many=an;
for(i=n;i>;i--){
c[i]=many;
one-=b[i];
many+=one;
}
//for(i=1;i<=n;i++)printf("%I64d\n",c[i]);
int ans=;
//for(i=1;i<=n;i++)printf("(%d,%d),%d\n",i,n,Query(i,n,1,n,1));
scanf("%d",&k);
REP(i,k) {
scanf("%I64d%d",&h[i],&x[i]);
if(c[x[i]]< h[i])ans++;
}
printf("%d\n",ans);
}
return ;
}

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

  1. Killing Monsters(hdu4970)

    Killing Monsters Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...

  2. CF460C Present (二分 + 差分数列)

    Codeforces Round #262 (Div. 2) C C - Present C. Present time limit per test 2 seconds memory limit p ...

  3. 周赛-Killing Monsters 分类: 比赛 2015-08-02 09:45 3人阅读 评论(0) 收藏

    Killing Monsters Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...

  4. UVALive 4119 Always an integer (差分数列,模拟)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Always an integer Time Limit:3000MS     M ...

  5. [CF 295A]Grag and Array[差分数列]

    题意: 有数列a[ ]; 操作op[ ] = { l, r, d }; 询问q[ ] = { x, y }; 操作表示对a的[ l, r ] 区间上每个数增加d; 询问表示执行[ x, y ]之间的o ...

  6. HDU 4970 Killing Monsters(树状数组)

    Killing Monsters Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  7. [CF 276C]Little Girl and Maximum Sum[差分数列]

    题意: 给出n项的数列A[ ], q个询问, 询问 [ l, r ] 之间项的和. 求A的全排列中该和的最大值. 思路: 记录所有询问, 利用差分数列qd[ ], 标记第 i 项被询问的次数( 每次区 ...

  8. hdu 4970 Killing Monsters(数组的巧妙运用) 2014多校训练第9场

    pid=4970">Killing Monsters                                                                   ...

  9. 【HDU4970】Killing Monsters

    题意 数轴上有n个点,有m座炮塔,每个炮塔有一个攻击范围和伤害,有k个怪物,给出他们的初始位置和血量,问最后有多少怪物能活着到达n点.n<=100000 分析 对于某个怪物,什么情况下它可以活着 ...

随机推荐

  1. 【matlab】输出固定位数的数字

    有时候需要集中处理数字,比如处理图片,并将它们编号为000001~009963 而matlab用fprintf输出时这些数字编号时,需要指定格式: %.nd n表示n位长度.%d表示10进制数字 e. ...

  2. codeforces 723E:One-Way Reform

    Description There are n cities and m two-way roads in Berland, each road connects two cities. It is ...

  3. codeforces 723A : The New Year: Meeting Friends

    Description There are three friend living on the straight line Ox in Lineland. The first friend live ...

  4. C++实现类似飞鸽的内网聊天工具,采用多播的协议实现

    项目相关地址 源码:https://github.com/easonjim/FreeAnt bug提交:https://github.com/easonjim/FreeAnt/issues

  5. Android成长日记-使用PagerAdapter实现页面切换

    Tip:此方式可以实现页面切换 1. 创建view1.xml,view2.xml,view3.xml,main.xml 在main.xml中创建 <android.support.v4.view ...

  6. Uva11374 Airport Express

    最短路问题. 从起点和终点开始各跑一次dijkstra,可以得到起点.终点到任意点的距离.枚举使用的商业线路,找最优解. 破题卡输出,记录前驱和输出什么的仿佛比算法本身还麻烦. /*by Silver ...

  7. MongoDB: CURD操作

    >> 创建:·db.foo.insert({"bar":"baz"}) //如果文档中没有"_id"键会自动增加一个·db.fo ...

  8. delete表1条件是另一个表中的数据,多表连接删除(转)

    DELETE删除多表数据,怎样才能同时删除多个关联表的数据呢?这里做了深入的解释: 1. delete from t1 where 条件 2.delete t1 from t1 where 条件 3. ...

  9. 网络存储(四)之ISCSI的进阶

    前言 上一篇博客中我们讲了如何搭建一个简单的iscsi网络存储系统,这块有个安全问题就是,任何知道target name的客户端都可以随意连接ISCSI服务器.但是很多时候,通过授权认证连接共享磁盘或 ...

  10. java编程思想-java中的并发(一)

    一.基本的线程机制 并发编程使我们可以将程序划分为多个分离的.独立运行的任务.通过使用多线程机制,这些独立任务中的每一个都将由执行线程来驱动. 线程模型为编程带来了便利,它简化了在单一程序中同时jia ...