题目链接

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 ;
}

最新文章

  1. PHPCMS开启伪静态和织梦开启伪静态的优缺点比较
  2. mysql关键字与自己设置的字段冲突
  3. iOS五种本地缓存数据方式
  4. leetcode 374
  5. BW常用事务码Tcode
  6. ArcGIS API for Silverlight开发入门准备
  7. jackson使用示例
  8. 控件WebView网页的加载
  9. UBUNTU 下设置全局 path变量
  10. 一类斜率优化的dp(特有性质:只能连续,不能交叉)
  11. json与xml的比较
  12. Java基础---Java---IO流-----LineNumberReader方法及原理、自定义一个LineNumberReader、字节流、图片复制、mp3复制、
  13. Python Web 框架:Tornado
  14. 通讯录管理系统(C语言)
  15. C-Linux_定时器示例使用
  16. Vue(七)发送Ajax请求
  17. [题目] Luogu P5038 [SCOI2012]奇怪的游戏
  18. globals() 和 locals() 函数
  19. 《DSP using MATLAB》Problem 5.7
  20. Python: 如何写一个异常

热门文章

  1. EC读书笔记系列之16:条款35、36、37、38、39、40
  2. Python核心编程读笔 1
  3. MYSQL区分大小写
  4. [转载]CTreeCtrl 和 CListCtrl 控件(VC_MFC)
  5. crontab Linux定时器工具
  6. 页面类跳转Demo
  7. Linux(CentOS6.5) 开放端口,配置防火墙
  8. ENVISAT卫星及ASAR数据介绍
  9. 跟我一起学extjs5(16--各种Grid列的自己定义渲染)
  10. 游戏开场镜头拉近(Unity3D开发之四)