题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031

Attack

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 1890    Accepted Submission(s): 554

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
 
思路:这题出的好!。树状数组的成段更新+单点查询
           successfully attacked=attack-defend;
          总的attack 由树状数组求和就可以。至于defend数得想办法求出~
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
const int N=20200;
using namespace std; int c[N],n,m,t,T;
int shield[N],pos[N]; struct node
{
int l,r;
}attack[N]; int lowbit(int x)
{
return x&(-x);
} void update(int x,int d)
{
while(x<=n)
{
c[x]+=d;
x+=lowbit(x);
}
} int getsum(int x)
{
int ans=0;
while(x>0)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
} void Init()
{
scanf("%d%d%d",&n,&m,&t);
memset(c,0,sizeof(c));
memset(shield,0,sizeof(shield));
memset(pos,0,sizeof(pos));
} int main()
{
char s[10];
int test=1;
scanf("%d",&T);
while(T--)
{ Init();
int cnt=0;
printf("Case %d:\n",test++);
while(m--)
{
scanf("%s",s);
if(s[0]=='A')
{
cnt++;
int si,ti;
scanf("%d%d",&si,&ti);
attack[cnt].l=si;
attack[cnt].r=ti;
update(si,1);
update(ti+1,-1);
}
else
{
int a;
scanf("%d",&a);
for(int i=pos[a];i<=cnt;)
{
if(a>=attack[i].l&&a<=attack[i].r)
{
pos[a]=i+t;
shield[a]++;
i=i+t;
}
else i++;
}
printf("%d\n",getsum(a)-shield[a]);
}
} }
return 0;
}

hdu 4031(树状数组+辅助数组)的更多相关文章

  1. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. HDU 2852 (树状数组+无序第K小)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...

  4. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

  5. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  6. HDU 1934 树状数组 也可以用线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 或者是我自己挂的专题http://acm.hust.edu.cn/vjudge/contest/view. ...

  7. 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...

  8. 【模板】HDU 1541 树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数. 题解: ...

  9. hdu 5147 树状数组

    题意:求满足a<b<c<d,A[a]<A[b],A[c]<A[d]的所有四元组(a,b,c,d)的个数 看到逆序对顺序对之类的问题一开始想到了曾经用归并排序求逆序对,结果 ...

随机推荐

  1. dwr3实现消息精确推送详细步骤

    最近项目中需要用到推送消息,找了很久终于找到一篇不错的文章,方便以后查看就转载了,也分享给大家,希望能帮到有需要的人. 第一.在项目中引入dwr.jar,然后在web.xml中进行配置,配置如下: & ...

  2. 最近在研究FFmpeg编解码

    好几年没上CNBLOGS了, 最近在研究FFmpeg编解码,一个人研究感到很寂寞,所以想通过博客来和大家分享和交流,呵呵. 最近研究的主题是: ANDROID手机同屏技术: 需要用到ANDROID截屏 ...

  3. Ubuntu12.04 64bit 下安装VNC server

    1. 安装gonme核心包(如果是字符界面的话) apt-get install x-window-system-coreapt-get install gnome-core (下载完成后需要安装dg ...

  4. c# winform编程之多线程ui界面资源修改总结篇【转】

    c# winform编程之多线程ui界面资源修改总结篇 单线程的winfom程序中,设置一个控件的值是很easy的事情,直接 this.TextBox1.value = "Hello Wor ...

  5. Retrofit 从入门到了解【总结】

    源码:https://github.com/baiqiantao/RetrofitDemo.git 参考:http://www.jianshu.com/p/308f3c54abdd Retrofit入 ...

  6. 在C#中使用属性控制 XML 序列化来解析XML

    今天需要解析一个XML,这个XML和一般情况用.NET的序列化出来的格式不太一样. 我就又补习了一下. 分享一下学习成果吧. 示例代码下载: http://download.csdn.net/deta ...

  7. j2ee model1模型完成分页逻辑的实现 详解!

    在显示用户全部信息的页面,在显示全部数据的时候,长长的滚动条,像是没有边界的天空一样, 让用户查看数据很不方便. 于是, 我们要把这些数据分页显示, 就像office的word一样,每页显示一定数量的 ...

  8. 如何在Linux中用命令行工具管理KVM虚拟环境

    第一步: 配置存储池 Virsh命令行工具是一款管理virsh客户域的用户界面,它能在命令行中运行所给的命令以及它的参数,我们要用它给我们的KVM环境创建存储池,想知道关于这个工具的更多信息,用以下这 ...

  9. PHP 使用mysql 与 mysqli 连接Mysql数据库

    代码很简单直接上了 <?php /** * @Author: HTL * @Email: Huangyuan413026@163.com * @DateTime: 2015-05-14 16:0 ...

  10. 【python】如何去掉使用BeautifulSoup读取html出现的警告UserWarning: You provided Unicode markup but also provided a value for from_encoding

    如果我们这样读取html页面 soup= BeautifulSoup(rsp.text,'html.parser',from_encoding='utf-8')  # 粗体部分多余了 就会出现下面的警 ...