题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=4031
区间更新,询问点
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN =20010;
int n;
int c[MAXN];
struct Range_Attack
{
int l;
int r;
Range_Attack()
{
l = r = 0;
}
};
Range_Attack Attack[MAXN];
int LowBit(int x)
{
return x & (-x);
}
void UFSet_BIT(int pos, int data)
{
while(pos <= n)
{
c[pos] += data;
pos += LowBit(pos);
}
}
int Query_BIT(int pos, int sum)
{
while(pos > 0)
{
sum += c[pos];
pos -= LowBit(pos);
}
return sum;
}
int main()
{
int T, kcase = 1, i, pos;
char str[6];
int times;//攻击的次数
int coolTime, oper;//盾牌冷却的时间,以及操作数
int used_Time[MAXN], protected_Times[MAXN];//used_Time[pos]表示pos这个位置已经花费了多少时间, protected_Times[pos]表示pos这个位置已经成功防御了多少次
scanf("%d", &T);
while(T--)
{
memset(c, 0, sizeof(c));
memset(used_Time, 0, sizeof(used_Time));
memset(protected_Times, 0, sizeof(protected_Times));
printf("Case %d:\n", kcase++);
scanf("%d %d %d", &n, &oper, &coolTime);
times = 0;
while(oper--)
{
scanf("%s", str);
if(str[0] == 'A')
{
scanf("%d %d", &Attack[times].l, &Attack[times].r);
UFSet_BIT(Attack[times].l, 1);
UFSet_BIT(Attack[times++].r+1, -1);
}
else
{
scanf("%d", &pos);
for(i = used_Time[pos]; i < times; ++i)
{
if(Attack[i].l <= pos && pos <= Attack[i].r)//第i次攻击范围内必须要包含pos
{
protected_Times[pos]++;
used_Time[pos] = i + coolTime;
i = i+coolTime - 1;
}
}
printf("%d\n", Query_BIT(pos, 0)-protected_Times[pos]);
}
}
}
return 0;
}