HDU-4031-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.
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
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
Case 1:
0
1
0
1
Case 2:
3
2
用树状数组维护总次数,一个辅助数组记录当前点被攻击之后恢复的时间,还有一个数组记录当前点的被防御次数。
#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(树状数组)的更多相关文章
- hdu 4031(树状数组+辅助数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Attack Time Limit: 5000/3000 MS (Java/Others) ...
- HDU 2838 (DP+树状数组维护带权排序)
Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...
- HDU 2689Sort it 树状数组 逆序对
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu 4046 Panda 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...
- hdu 5497 Inversion 树状数组 逆序对,单点修改
Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...
- HDU 5493 Queue 树状数组
Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...
- POJ 2352 && HDU 1541 Stars (树状数组)
一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...
- hdu 1541 (基本树状数组) Stars
题目http://acm.hdu.edu.cn/showproblem.php?pid=1541 n个星星的坐标,问在某个点左边(横坐标和纵坐标不大于该点)的点的个数有多少个,输出n行,每行有一个数字 ...
- HDU 4325 Flowers 树状数组+离散化
Flowers Problem Description As is known to all, the blooming time and duration varies between differ ...
- hdu 5877 (dfs+树状数组) Weak Pair
题目:这里 题意: 给出一个n个结点的树和一个数k,每个结点都有一个权值,问有多少对点(u,v)满足u是v的祖先结点且二者的权值之积小于等于k. 从根结点开始dfs,假设搜的的点的权值是v,我们需要的 ...
随机推荐
- java 中的VO,PO,DTO,DO对象
经常会接触到VO,DO,DTO的概念,本文从领域建模中的实体划分和项目中的实际应用情况两个角度,对这几个概念进行简析. 得出的主要结论是:在项目应用中,VO对应于页面上需要显示的数据(表单),DO对应 ...
- 一篇不错的讲解Java异常的文章(转载)----感觉很不错,读了以后很有启发
六种异常处理的陋习 你觉得自己是一个Java专家吗?是否肯定自己已经全面掌握了Java的异常处理机制?在下面这段代码中,你能够迅速找出异常处理的六个问题吗? OutputStreamWriter ou ...
- Shell--变量键盘读取、数组与声明:read,array,declare
1.read read [-pt] variable -P:后面可以接提示信息 -t:后面可以接等待的秒数,时间到后等待结束 read后面不加任何参数,直接加变量名称,那么就会主动出现一个空白行等待你 ...
- CentOS 7.2安装lepus数据库监控系统
环境说明 系统版本 CentOS 7.2 x86_64 软件版本 lepus 3.7 Lepus是一套开源的数据库监控平台,目前已经支持MySQL.Oracle.SQLServer.MongoDB ...
- zabbix2.2.22 升级3.0.18
环境说明 系统版本 CentOS 7.2 x86_64 zabbix2.2.22界面如下 升级过程: 清除之前的zabbix的yum源缓存 [root@zabbix ~]# yum clean a ...
- 你今天Restful了吗?
大家都宣称自己已经满足REST架构的风格, 那到底需要怎么去评价是否符合REST架构, 至少得有以下几个特征. REST 的约束包括: 无状态.在不同的客户端请求之间,服务器并不保存客户端相关的上下文 ...
- fmt标签的格式化日期使用
声明: <%@ taglib prefix="fmt" uri="/WEB-INF/fmt.tld" %> or <%@ taglib pre ...
- 解决dubbo问题:forbid consumer(2)
线下环境经常出现类似这种异常: com.alibaba.dubbo.rpc.RpcException: Forbid consumer 10.0.53.69 access service com.ku ...
- Elasticsearch教程(七) elasticsearch Insert 插入数据(Java)
首先我不赞成再采用一些中间件(jar包)来解决和 Elasticsearch 之间的交互,比如 Spring-data-elasticsearch.jar 系列一样,用就得依赖它.而 Elastic ...
- android中非堵塞socket通信
1.什么是同步与异步,堵塞与非堵塞 首先我们要明确搞明确:同步就等于堵塞?异步就等于非堵塞?这是不正确的,同步不等于阻 塞.而异步也不等于非堵塞. 1)那什么是同步编程? 什么是同步,就是在发出一个功 ...