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,我们需要的 ...
随机推荐
- poj 2324 Anniversary party(树形DP)
/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...
- C++之纯虚函数
1. 纯虚函数形式 class Parent { public: ; }; 代码中的func1就是纯虚函数,没有函数体,没有函数的具体实现,有virtual,在函数名后面带有“ = 0”形式: 2.对 ...
- JS方面重点摘要(一)
1.获取样式(1)style只能获取到行间样式的属性(2)火狐getComputedStyle(obj,null)[attr],IE:obj.currentStyle[attr] 2.ready.on ...
- 一个java调用python的问题
使用 ProcessBuilder List<String> commands = new ArrayList(); commands.add("python"); c ...
- RedHat虚拟机相关操作
在VM虚拟机中安装完Redhat系统之后 如果需要用secureCRT连接linux系统的话 操作步骤如下: 1.进入linux系统,在终端输入ifconfig(注意,不是windows的ipconf ...
- 使用Spring开发和监控线程池服务
第1步:添加maven 项目 第2步:添加依赖库 将Spring的依赖添加到Maven的pom.xml文件中. 1 2 3 4 5 6 7 8 9 10 11 <!-- Spring 3 dep ...
- UVA 111 (复习dp, 14.07.09)
History Grading Background Many problems in Computer Science involve maximizing some measure accor ...
- python raise assert
class MyException(Exception): def __init__(self,error_msg): self.error_msg=error_msg def __str__(sel ...
- 【Excle】如何隐藏数据透视表中的错误值
如下:数据透视表出现错误 怎么解决呢 步骤 方法① 单击数据透视表任意单元格→数据透视表工具→分析→选项→勾选"对于错误值显示"→确定 方法② 右键→数据透视表选项(同样可以修改)
- C# 反编译工具
justdecompile http://down.51cto.com/data/2067031 ILSpy http://www.fishlee.net/soft/ilspy_chs/