2014 Multi-University Training Contest 9#11

本文介绍了一种基于线段树的区间更新算法,用于解决TD游戏中怪物被塔防攻击后的生存数量计算问题。通过构建线段树并进行区间更新与查询操作,可以高效地计算出每个怪物在遭受攻击后的剩余生命值,进而判断其是否能存活。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2014 Multi-University Training Contest 9#11

Killing MonstersTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 562    Accepted Submission(s): 308


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

521 3 15 5 251 33 15 27 39 10

 


Sample Output

3

Hint

In the sample, three monsters with origin HP 5, 7 and 9 will survive.
 
简单的线段树,区间更新就可以!
 
  1 #include<cstdio>
  2 
  3 using namespace std;
  4 
  5 #define maxn 100000+10
  6 
  7 typedef long long LL;
  8 
  9 struct node{
 10 
 11     int l,r,m;
 12 
 13     LL sum,mark;
 14 
 15 }T[maxn<<2];
 16 
 17 int a[maxn];
 18 
 19 void build(int id,int l,int r){
 20 
 21      T[id].l=l;   T[id].r=r;  T[id].m=(l+r)>>1;  T[id].mark=0;
 22 
 23      if(l==r)   { T[id].sum=a[l]; return;  }
 24 
 25      int m=(l+r)>>1;
 26 
 27      build(id<<1,l,m);  build((id<<1)+1,m+1,r);
 28 
 29      T[id].sum=(T[id<<1].sum+T[(id<<1)+1].sum);
 30 
 31 }
 32 
 33 void update(int id,int l,int r,int val){
 34 
 35      if(T[id].l==l&&T[id].r==r){
 36 
 37         T[id].mark+=val; return ;
 38 
 39      }
 40 
 41      T[id].sum+=(LL)val*(r-l+1);
 42 
 43      if(T[id].m>=r)
 44 
 45           update(id<<1,l,r,val);
 46 
 47      else if(T[id].m<l)
 48 
 49           update((id<<1)+1,l,r,val);
 50 
 51      else{
 52 
 53           update(id<<1,l,T[id].m,val);
 54 
 55           update((id<<1)+1,T[id].m+1,r,val);
 56 
 57      }
 58 
 59 }
 60 
 61 LL query(int id,int l,int r){
 62 
 63     if(T[id].l==l&&T[id].r==r)  return T[id].sum+T[id].mark*(LL)(r-l+1);
 64 
 65     if(T[id].mark!=0) {
 66 
 67         T[id<<1].mark+=T[id].mark;
 68 
 69         T[(id<<1)+1].mark+=T[id].mark;
 70 
 71         T[id].sum+=(LL)(T[id].r-T[id].l+1)*T[id].mark;  T[id].mark=0;
 72 
 73     }
 74 
 75 
 76 
 77     if(T[id].m>=r){
 78 
 79           return query(id<<1,l,r);
 80 
 81     }
 82 
 83     else if(T[id].m<l){
 84 
 85           return query((id<<1)+1,l,r);
 86 
 87     }
 88 
 89     else{
 90 
 91           return query(id<<1,l,T[id].m)+query((id<<1)+1,T[id].m+1,r);
 92 
 93     }
 94 
 95 }
 96 
 97 int main(){
 98 
 99     int n,Q,m;  char str[8];   int b,c,d,l,r,v;
100 
101     while(scanf("%d",&n)==1&&n){
102 
103             scanf("%d",&Q);
104 
105           for(int i=1;i<=n;i++){
106 
107              a[i] = 0;
108 
109           }
110 
111           build(1,1,n);
112 
113           for(int i=0;i<Q;i++){
114 
115                scanf("%d%d%d",&l,&r,&v);
116 
117                     update(1,l,r,v);
118 
119                }
120 
121           scanf("%d",&m);
122 
123           LL h;
124 
125           int x;
126 
127           int ans = 0;
128 
129           for(int i = 0;i<m;i++)
130 
131           {
132 
133               scanf("%I64d%d",&h,&x);
134 
135               if(query(1,x,n)<h) ans++;
136 
137           }
138 
139           printf("%d\n",ans);
140 
141     }
142 
143     return 0;
144 
145 }

 

转载于:https://www.cnblogs.com/haohaooo/p/4035334.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值