题目链接: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. Gradle在Windows环境与Linux上配置有哪些不同?

    我的开发环境:Windows + Android Studio + Gradle 2.8 all + Jenkins 公司CI 服务器环境: Linux + Gradle 2.10 bin + Jen ...

  2. EF实体框架处理实体之间关联关系与EF延迟机制(下)

    在数据库中,表与表之间可能存在多种联系,比如,一对多,多对多的关系.当我们使用逻辑外键在数据库建立两张表之间的关系的时候,我们使用EF实体框架 必然也会将这种关系映射到我们的实体关系中来.所以,在我们 ...

  3. tinyxml使用笔记与总结

    在TinyXML中,根据XML的各种元素来定义了一些类:        TiXmlBase:整个TinyXML模型的基类.        TiXmlAttribute:对应于XML中的元素的属性.   ...

  4. C标准库 二

    http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html现在C语言(C99)标准库函数的24个头文件列表如下: assert.h ...

  5. java操作mongodb时,对象bean和DBObject相互转换的方法

    package com.iqbon.spider.util; import java.lang.reflect.Field; import java.lang.reflect.InvocationTa ...

  6. 《Windows核心编程》第十一章——线程池

    隐式使用工作项 #include <iostream> #include <windows.h> ; VOID NTAPI SimpleCallback(PTP_CALLBAC ...

  7. android中一些特殊字符(如:←↑→↓等箭头符号)的Unicode码值

    在项目中,有时候在一些控件(如Button.TextView)中要添加一些符号,如下图所示:                          这个时候可以使用图片的方式来显示,不过这些可以直接使用U ...

  8. [JQuery插件系列]-强烈推荐10个非常不错的jQuery工具提示插件

    个非常酷的 jQuery 工具提示(Tooltip)插件,希望大家能喜欢. 1. Pop! 使用 Pop!可以创建简单的下拉菜单!这是一个并不引人注目的 jQuery 插件. 2. BetterTip ...

  9. CF 463D Gargari and Permutations [dp]

    给出一个长为n的数列的k个排列(1 ≤ n ≤ 1000; 2 ≤ k ≤ 5).求这个k个数列的最长公共子序列的长度 dp[i]=max{dp[j]+1,where j<i 且j,i相应的字符 ...

  10. IOS NSString 用法详解

    [cpp]  view plain copy   //NSString 操作均不改变自身值 //构建字符串 NSString *szTmp = @"A string";       ...