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 <stdio.h>

int n,node[20005],ls[20005],rs[20005],pos[20005],c[20005];

int lowbit(int x)
{
return x & -x;
} int sum(int x)
{
int res=0; while(x>0)
{
res+=node[x]; x-=lowbit(x);
} return res;
} void add(int x,int val)
{
while(x<=n)
{
node[x]+=val; x+=lowbit(x);
}
} int main()
{
int T,q,t,i,a,b,cnt,cases=1;
char s[10]; scanf("%d",&T); while(T--)
{
scanf("%d%d%d",&n,&q,&t); cnt=0; for(i=0;i<=n;i++) c[i]=node[i]=pos[i]=0; printf("Case %d:\n",cases++); while(q--)
{
scanf("%s",s); if(s[0]=='A')
{
scanf("%d%d",&a,&b); ls[cnt]=a;
rs[cnt]=b; cnt++; add(a,1);
add(b+1,-1);
}
else
{
scanf("%d",&a); for(i=pos[a];i<cnt;i++)
{
if(a>=ls[i] && a<=rs[i] && i>=pos[a])
{
pos[a]=i+t;
c[a]++; i+=t-1;
}
} printf("%d\n",sum(a)-c[a]);
}
}
}
}

HDU-4031-Attack(树状数组)的更多相关文章

  1. hdu 4031(树状数组+辅助数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Attack Time Limit: 5000/3000 MS (Java/Others)    ...

  2. HDU 2838 (DP+树状数组维护带权排序)

    Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...

  3. HDU 2689Sort it 树状数组 逆序对

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. hdu 4046 Panda 树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...

  5. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  6. HDU 5493 Queue 树状数组

    Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...

  7. POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

    一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...

  8. hdu 1541 (基本树状数组) Stars

    题目http://acm.hdu.edu.cn/showproblem.php?pid=1541 n个星星的坐标,问在某个点左边(横坐标和纵坐标不大于该点)的点的个数有多少个,输出n行,每行有一个数字 ...

  9. HDU 4325 Flowers 树状数组+离散化

    Flowers Problem Description As is known to all, the blooming time and duration varies between differ ...

  10. hdu 5877 (dfs+树状数组) Weak Pair

    题目:这里 题意: 给出一个n个结点的树和一个数k,每个结点都有一个权值,问有多少对点(u,v)满足u是v的祖先结点且二者的权值之积小于等于k. 从根结点开始dfs,假设搜的的点的权值是v,我们需要的 ...

随机推荐

  1. Android可伸缩列表ExpandableListView

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. 隐藏服务器真实IP的方法来防止DDOS攻击

    2017-08-22 作者:小唐 点击: 10,500次 在无盘系统的环境下,服务器软件存在漏洞,就容易受到DDOS攻击,隐藏服务器真实IP是解决问题最好的方法,下面小编与大家分享一下隐藏服务器真实I ...

  3. docker之人手一台服务器

    安装docker uname –r 检查内核版本 yum update 升级本地yum包 vim /etc/yum.repos.d/docker.repo #添加yum仓库配置 [dockerrepo ...

  4. Java学习之自动装箱和自动拆箱源码分析

    自动装箱(boxing)和自动拆箱(unboxing) 首先了解下Java的四类八种基本数据类型   基本类型 占用空间(Byte) 表示范围 包装器类型 boolean 1/8 true|false ...

  5. 机器学习第4课:多变量线性回归(Linear Regression with Multiple Variables)

    4.1  多维特征 目前为止,我们探讨了单变量/特征的回归模型,现在我们对房价模型增加更多的特征, 例如房间数楼层等,构成一个含有多个变量的模型,模型中的特征为(x1,x2,...,xn).

  6. 18 Tar Command Examples in Linux

    FROM: http://www.tecmint.com/18-tar-command-examples-in-linux/ 18 Tar Command Examples in Linux By R ...

  7. 2017.3.31 spring mvc教程(一)核心类与接口

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  8. 使用HttpClient测试SpringMVC的接口

    转载:http://blog.csdn.net/tmaskboy/article/details/52355591 最近在写SSM创建的Web项目,写到一个对外接口时需要做测试,接受json格式的数据 ...

  9. django book学习问题记录

    —————————————————————————————————— 位置:第五章<模型> 问题描述(已解决): >>> p1 = Publisher.objects.c ...

  10. d3.js封装文本实现自动换行和旋转平移等功能

    我们下面话不多说,本文主要介绍的是利用D3.js封装文本实现自动换行功能的步骤,下面来一起看看吧. 一.引用 multext.js 文件 multext.js function appendMulti ...