hdu 4970

题目链接: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.
 

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
题意:n个格子,m个塔,每个塔有攻击范围[  l  ,   r  ],从格子  l  开始到格子  r  的范围内~每个敌人会受到di点伤害,然后给出k个敌人的hp和初始位置,这k个敌人朝着第n个格子走去,如果敌人的hp小于等于0就会死亡,问最后又多少个敌人存活;

(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值