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,我们需要的 ...
随机推荐
- VUE -- ejs模板的书写
1.EJS是一个简单高效的模板语言,通过数据和模板,可以生成HTML标记文本.可以说EJS是一个JavaScript库,EJS可以同时运行在客户端和服务器端,客户端安装直接引入文件即可,服务器端用np ...
- JAVA常见算法题(六)
package com.xiaowu.demo; /** * 输入两个正整数m和n,求其最大公约数和最小公倍数. * * @author WQ * */ public class Demo6 { pu ...
- ElasticSearch 结构化搜索全文
1.介绍 上篇介绍了搜索结构化数据的简单应用示例,现在来探寻 全文搜索(full-text search) :怎样在全文字段中搜索到最相关的文档. 全文搜索两个最重要的方面是: 相关性(Relevan ...
- smarty在循环的时候计数来显示这是第几次循环的功能
想必有很多人比较喜欢这个smarty循环的时候有个变量增加的功能或比较需要这个功能吧?其实不需要额外的变量,当然你也许根本用不了.我们用smarty内置的就可以了.就是smarty有foreach和s ...
- 倍福TwinCAT(贝福Beckhoff)基础教程2.0 TwinCAT常用快捷键
F5:运行程序 CTRL+F5:停止运行当前程序 F12:登出 F11:登录 CTRL+F7:强制写入数值 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.you ...
- windows pip源
首先在window的文件夹窗口输入 : %APPDATA% 然后在底下新建pip文件夹,然后到pip文件夹里面去新建个pip.ini,然后再里面输入内容 [global] timeout = 6000 ...
- uva 10034 Freckles (kruskal||prim)
题目上仅仅给的坐标,没有给出来边的长度,不管是prim算法还是kruskal算法我们都须要知道边的长度来操作. 这道题是浮点数,也没啥大的差别,处理一下就能够了. 有关这两个算法的介绍前面我已经写过了 ...
- 【BIEE】11_根据显示指标展示不同报表
报表开发过程中,我们经常会需要根据所选择的不同指标,展示不同的报表!例如: 显示指标有:金额与合同数,可以根据显示指标选择的内容进行相应报表数据展示 一.环境准备 create table crm_i ...
- linux与开发板串口通信
研究了一天的linux串口,结果改了树莓派的系统配置文件config.txt给改了导致系统崩溃....其实我感觉网上的大多数方法都是不符合新版本树莓派的,网上的方法是通过修改系统配置文件后安装mini ...
- Spring 应用外部属性文件 配置 context 错误
在Spring配置文件中出现通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明这个错误,其实主要是我们在引入命名空间时没有正确引入它的DTD解析 ...