题目链接

Attack

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2330    Accepted Submission(s): 695

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
 
基础的是线段树区间更新+单点查询, 因为有那个保护的时间, 所以开一个辅助的数组来记录状态, 具体的看代码。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e4+;
int sum[maxn<<], add[maxn<<];
void pushUp(int rt) {
sum[rt] = sum[rt<<]+sum[rt<<|];
}
void pushDown(int rt, int m) {
if(add[rt]) {
sum[rt<<] += (m-(m>>))*add[rt];
sum[rt<<|] += (m>>)*add[rt];
add[rt<<] += add[rt];
add[rt<<|] += add[rt];
add[rt] = ;
}
}
void update(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
sum[rt] += r-l+;
add[rt]++;
return ;
}
pushDown(rt, r-l+);
int m = l+r>>;
if(L<=m)
update(L, R, lson);
if(R>m)
update(L, R, rson);
pushUp(rt);
}
int query(int p, int l, int r, int rt) {
if(l == r) {
return sum[rt];
}
pushDown(rt, r-l+);
int m = l+r>>;
if(p<=m)
return query(p, lson);
else
return query(p, rson);
}
int time[maxn], num[maxn];
pll att[maxn];
int main()
{
int t, n, q, k, x, y;
cin>>t;
char s[];
for(int casee = ; casee<=t; casee++) {
printf("Case %d:\n", casee);
mem(add);
mem(num);
mem(sum);
mem(time);
int cnt = ;
cin>>n>>q>>k;
while(q--) {
scanf("%s%d", s, &x);
if(s[] == 'A') {
scanf("%d", &y);
update(x, y, , n, );
att[cnt++] = mk(x, y);
} else {
if(k == ) {
puts("");
continue;
}
int tmp = query(x, , n, );
for(int i = time[x]; i<cnt; i++) {
if(x<=att[i].se&&x>=att[i].fi) {
num[x]++;
time[x] = i+k;
i += k-;
}
}
printf("%d\n", tmp-num[x]);
}
}
}
return ;
}

hdu 4031 Attack 线段树的更多相关文章

  1. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  2. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  3. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  4. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

  5. HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...

  6. HDU 4031 Attack (线段树)

    成功袭击次数=所有袭击次数-成功防守次数 需要一个辅助pre来记录上一次袭击成功什么时候,对于每个查询,从上一次袭击成功开始,每隔t更新一次. 感觉这样做最坏时间复杂度是O(n^2),这里 说是O(q ...

  7. hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值

    Conturbatio Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...

  8. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  9. HDU 3308 LCIS (线段树区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...

随机推荐

  1. NOIP2012模拟试题【圆圈舞蹈( circle)

    2.圆圈舞蹈( circle) [问题描述] 熊大妈的奶牛在时针的带领下,围成了一个圆圈跳舞.由于没有严格的教育,奶牛们之间的间隔不一致. 奶牛想知道两只最远的奶牛到底隔了多远.奶牛A到B的距离为A顺 ...

  2. EC读书笔记系列之9:条款16、17

    条款16 成对使用new和delete时要采取相同形式 记住: ★若你在new表达式中使用[ ],必须在相应的delete中也使用[ ],反之亦然 -------------------------- ...

  3. EC读书笔记系列之6:条款11 在operator=中处理自我赋值

    记住: ★确保当对象自我赋值时operator=有良好行为.有三种方法:比较“来源对象”和“目标对象”的地址.精心周到的语句顺序.以及copy-and-swap技术 ★确定任何函数若操作一个以上对象, ...

  4. php7 install script

    ./configure --prefix=/home/admin/local/php7 --with-gd=/home/admin/local/libgd-2.1.1/ --with-jpeg-dir ...

  5. php curl函数实例

    <?php function login(){ $url = 'http://jspatch.qq.com/offline/check?qver=6.2.0.427&hver=0& ...

  6. hdu 5046 Airport 二分+重复覆盖

    题目链接 给n个点, 定义两点之间距离为|x1-x2|+|y1-y2|. 然后要选出k个城市建机场, 每个机场可以覆盖一个半径的距离. 求在选出点数不大于k的情况下, 这个半径距离的最大值. 二分半径 ...

  7. 【转】IOS开发小技巧

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...

  8. Confluent

    Confluent介绍(一)   最开始接触confluent是通过这篇博客,How to Build a Scalable ETL Pipeline with Kafka Connect,对于做大数 ...

  9. IBM X System ServerGuide 8.41 服务器 系统安装 引导盘

    IBM X System ServerGuide 8.41 支持操作系统: 32位: Microsoft Windows 2003/2003 R2 (Enterprise, Standard, Web ...

  10. Oracle EBS-SQL (GL-4):从接收追溯到接收事务

    SELECT row_id, creation_date, created_by, last_update_date, last_updated_by,last_update_login, note_ ...