hdu 4031Attack

 

Attack

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 710    Accepted Submission(s): 90


Problem Description
Today is the 10th Annual of “September 11 attacks”, the Al Qaeda is about to attack American again. However, American is protected by a high wall this time, which can be treating as a segment with length N. Al Qaeda has a super weapon, every second it can attack a continuous range of the wall. American deployed N energy shield. Each one defends one unit length of the wall. However, after the shield defends one attack, it needs t seconds to cool down. If the shield defends an attack at kth second, it can’t defend any attack between (k+1)th second and (k+t-1)th second, inclusive. The shield will defend automatically when it is under attack if it is ready.

During the war, it is very important to understand the situation of both self and the enemy. So the commanders of American want to know how much time some part of the wall is successfully attacked. Successfully attacked means that the attack is not defended by the shield.
 

 

Input
The beginning of the data is an integer T (T ≤ 20), the number of test case.
The first line of each test case is three integers, N, Q, t, the length of the wall, the number of attacks and queries, and the time each shield needs to cool down.
The next Q lines each describe one attack or one query. It may be one of the following formats
1. Attack si ti
  Al Qaeda attack the wall from si to ti, inclusive. 1 ≤ si ≤ ti ≤ N
2. Query p
  How many times the pth unit have been successfully attacked. 1 ≤ p ≤ N
The kth attack happened at the kth second. Queries don’t take time.
1 ≤ N, Q ≤ 20000
1 ≤ t ≤ 50
 

 

Output
For the ith case, output one line “Case i: ” at first. Then for each query, output one line containing one integer, the number of time the pth unit was successfully attacked when asked.
 

 

Sample Input
2
3 7 2
Attack 1 2
Query 2
Attack 2 3
Query 2
Attack 1 3
Query 1
Query 3
9 7 3
Attack 5 5
Attack 4 6
Attack 3 7
Attack 2 8
Attack 1 9
Query 5
Query 3
 

 

Sample Output
Case 1:
0
1
0
1
Case 2:
3
2
 

 

Source
 
主要思路是:一个点被攻击数目=总攻击数目-防御数目;
 
代码:
 
    #include <iostream>
#include <cstdio>
#include <cstring>
#define N 20010
#define lowbit(x)  x&(-x)

using namespace std;
int def[N],attack_num[N],n,pos[N];
typedef struct
{
  int l,r;
 }Attack;
Attack a[N];
void insert(int p,int d)
{
    for(int i=p;i<=n;i+=lowbit(i))
    {
        attack_num[i]+=d;
    }
}
int get_sum(int p)
{
    int sum=0;
    for(int i=p;i>0;i-=lowbit(i))
       sum+=attack_num[i];
    return sum;
}

int main()
{
    int t,t0,Q,a_num;
    char cmd[10];
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        printf("Case %d:\n",cas);
        scanf("%d%d%d",&n,&Q,&t0);
        memset(attack_num,0,sizeof(attack_num));
        memset(pos,0,sizeof(pos));
        memset(def,0,sizeof(def));
        int a_cnt=0;
        while(Q--)
        {
            scanf("%s",cmd);
            if(cmd[0]=='A')
            {
                a_cnt++;
                int l,r;
                scanf("%d%d",&l,&r);
                a[a_cnt].l=l,a[a_cnt].r=r;
                insert(l,1);//时间复查度O(logn)
                insert(r+1,-1);
            }
            else
            {
                int p;
                scanf("%d",&p);
                if(t0==1)  {puts("0"); continue;}/*这句加与不加的区别

       


                for(int i=pos[p];i<=a_cnt;i++)//最坏的话是0(logm)
                {
                    if(p>=a[i].l&&p<=a[i].r)
                    {
                        def[p]++;
                        pos[p]=i+t0;
                        i+=t0-1;
                    }
                }
                printf("%d\n",get_sum(p)-def[p]);
            }
        }
    }
    return 0;
}



转载于:https://www.cnblogs.com/one--world--one--dream/archive/2011/10/10/2206522.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值