题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031
代码风格:http://www.notonlysuccess.com/index.php/segment-tree-complete/
题目大意:Attack——攻击区间【a,b】, 每个位置的防御盾用过一次在 t 时间内不能再次防御,没有启动防御盾表示successful attack,W——该位置有多少次successful attack
算法:线段树
思路:统计该点所有被攻击的次数 减去- 该点启动了多少次防御盾
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1
#define mid int m = (r+l) >> 1
struct nod
{
int l, r;
}p[565656];
int cnt[565656];
void update(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R)
{
cnt[rt] ++;
return ;
}
mid ;
if(L <= m)
update(L, R, lson);
if(m < R)
update(L, R, rson);
}
int query(int d, int l, int r, int rt)
{
if(l == r)
return cnt[rt];
mid ;
if(d <= m)
return cnt[rt] + query(d, lson);
else return cnt[rt] + query(d, rson);
}
int main()
{
int n, T, m, t;
char op[15];
int a, b;
int ica = 1;
scanf("%d", &T);
while(T --)
{
printf("Case %d:\n", ica ++);
scanf("%d%d%d", &n, &m, &t);
memset(cnt, 0, sizeof(cnt));
int tot = 0;
while(m --)
{
scanf("%s%d", op, &a);
if(op[0] == 'A')
{
scanf("%d", &b);
p[tot].l = a;
p[tot].r = b;
tot ++;
update(a, b, 1, n, 1);
}
else
{
int sum = query(a, 1, n, 1);//一共被攻打了多少次
int temp = 0;
for(int i = 0; i < tot; i ++)
{
// printf("l = %d, r = %d\n", p[i].l, p[i].r);
if(p[i].l <= a && a <= p[i].r)
{
temp ++;
i += t-1;
}
}
// printf("sum = %d, temp = %d\n", sum, temp);
printf("%d\n", sum - temp);
}
}
}
return 0;
}