hdu4031(树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=4031

思路:将防御和攻击分开,用树状数组统计攻击次数,再开一个数组统计成功防御次数。

#include<iostream>
using namespace std;
struct 
{
	int num,count;
}d[20005];
int c[20005],n,str[20005][2];
int lowbit(int x)
{
	return x&(-x);
}
void updatac(int i,int j)
{
	while(i<=n)
	{
		c[i]+=j;
		i+=lowbit(i);
	}
}
int getsumc(int x)
{
	int sum=0;
	while(x>0)
	{
		sum+=c[x];
		x-=lowbit(x);
	}
	return sum;
}
int main()
{
	int t,f=0;
	scanf("%d",&t);
	while(t--)
	{
		memset(c,0,sizeof(c));
		int i=0;
		for(i=0;i<20005;i++)
		{
			d[i].num=0;
			d[i].count=1;
		}
		int m,t;
		scanf("%d%d%d",&n,&m,&t);
		char s[5];
		 i=0;
		printf("Case %d:\n",++f);
		while(m--)
		{
			scanf("%s",s);
			if(s[0]=='A')
			{
				i++;
				int tmp1,tmp2;
				scanf("%d%d",&tmp1,&tmp2);
				str[i][0]=tmp1;
				str[i][1]=tmp2;
				updatac(tmp1,1);
				updatac(tmp2+1,-1);
			}
			else
			{
				int tmp;
				scanf("%d",&tmp);
				for(int j=d[tmp].count;j<=i;)
				{
					if(tmp<=str[j][1]&&tmp>=str[j][0])
					{
						j=j+t;
						d[tmp].count=j;
						d[tmp].num++;	
					}
					else
					{
						j++;
						d[tmp].count=j;
					}
				}
				printf("%d\n",getsumc(tmp)-d[tmp].num);
			}
		}
	}
	return 0;
}

 

posted @ 2012-12-24 13:12  紫忆  阅读(357)  评论(0编辑  收藏  举报