HLG 1161 Leyni【树状数组】

Description

Leyni被人掳走,身在水深火热之中...
小奈叶为了拯救Leyni,独自一人前往森林深处从静竹手中夺回昏迷中的Leyni。
历经千辛万苦,小奈叶救出了Leyni,但是静竹为此极为恼怒,决定对他们发起最强烈的进攻。
不过小奈叶有一个叫做能量保护圈的道具,可以保护他们。
这个保护圈由n个小的小护盾围成一圈,从1到n编号。当某一块小护盾受到攻击的时候,小护盾就会抵消掉这次攻击,也就是说对这一块小护盾的攻击是无效攻击,从而保护圈里的人,不过小护盾在遭到一次攻击后,需要t秒进行冷却,在冷却期间受到的攻击都是有效攻击,此时他们就会遭到攻击, 即假设1秒时受到攻击并成功防御,到1+t秒时冷却才结束并能进行防御,在2到t受到的都是有效攻击。

现在小奈叶专心战斗,Leyni昏迷,他们无法得知小护盾遭受的有效攻击次数,他们需要你的帮助。
只要能帮到他们,Leyni就会赠送出一份小奈叶写真集。
Input

第一行是一个整数T,表示有多少组测试数据。
第一行是三个整数,n,q,t,n表示保护圈的长度,q表示攻击的询问的总次数,t表示能量盾的冷却时间。
接下来的q行,每行表示受到的攻击或者她询问某范围内的能量盾被攻击的次数。
攻击:
Attack   a
表示编号为a的小护盾受到一次攻击, 保证 1 <= a <= n
询问:
Query  a  b
表示询问编号从a到b的小护盾(包括a和b)总共受到了多少次有效攻击。保证 1<=a,b<=n
第k次攻击发生在第k秒,询问不花费时间。
1 <= n,q <=100000
1 <= t <= 50。

Output

每一组测试数据,先输出一行"Case i:",i表示第i组测试数据,从1开始计数。
之后对于每一个询问,输出该范围内的小护盾受到的有效攻击次数,一个询问一行。

Sample Input

1
4 7 3
Attack 1
Attack 1
Attack 1
Attack 2
Attack 2
Query 1 4
Query 1 1

Sample Output

Case 1:
3
2

code:

 

View Code
#include<stdio.h>
#include<string.h>
int tcase,n;
int tree[100001];
int lowbit(int x)
{
return (x)&(-x);
}
void attack(int pos)
{
while(pos<=n)
{
tree[pos]+=1;
pos+=lowbit(pos);
}
}
int getsum(int x)
{
int sum=0;
while(x>0)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
int main()
{
char str[20];
int te[100001];
int i,j,t,ci,k=0;
int x,y,tot;
scanf("%d",&tcase);
{
for(j=1;j<=tcase;j++)
{
tot=0;
memset(tree,0,sizeof(tree));
memset(te,0,sizeof(te));
scanf("%d%d%d",&n,&ci,&t);
printf("Case %d:\n",j);
for(i=1;i<=ci;i++)
{
scanf("%s",str);
if(*str=='A')
{
tot++;
scanf("%d",&y);
if(te[y]==0)
te[y]=tot;
else if(tot-te[y]>=1&&tot-te[y]<t)
attack(y);
else if(tot-te[y]>=t)
te[y]=tot;
}
else if(*str=='Q')
{
scanf("%d%d",&x,&y);
printf("%d\n",getsum(y)-getsum(x-1));
}
}
}
}
return 0;
}

 

posted @ 2012-03-15 00:31  'wind  阅读(262)  评论(0编辑  收藏  举报