hdu 1166(线段树)

点击打开链接


都说是模版级别的。。

但是还是RE了两次啊(是因为输入的原因)。。。。


 

#include"stdio.h"
#include"string.h"
#define N 50011
struct tree
{
	int x,y,mid;
	int count;
}A[4*N];

void creat(int x,int y,int k)
{
	A[k].x=x;
	A[k].y=y;
	A[k].mid=(x+y)/2;
	A[k].count=0;
	if(x==y)return ;
	creat(x,A[k].mid,2*k);
	creat(A[k].mid+1,y,2*k+1);
	return;
}

void insert(int x,int y,int k)
{
	if(A[k].x==A[k].y&&A[k].x==x)
	{
		A[k].count+=y;
		return ;
	}
	if(x<=A[k].mid)insert(x,y,2*k);
	else insert(x,y,2*k+1);
	A[k].count=A[2*k].count+A[2*k+1].count;
	return ;
}

int search(int x,int y,int k)
{
	if(A[k].x==x&&A[k].y==y)
		return A[k].count;
	int ans=0;
	if(y<=A[k].mid)
		ans+=search(x,y,2*k);
	else if(x>A[k].mid)
		ans+=search(x,y,2*k+1);
	else
	{
		ans+=search(x,A[k].mid,2*k);
		ans+=search(A[k].mid+1,y,2*k+1);
	}
	return ans;
}
int main()
{
	int T,t;
	int n;
	int x,y,a;
	int i;
	char s[15];
	scanf("%d",&T);
	t=1;
	while(T--)
	{
		printf("Case %d:\n",t++);
		scanf("%d",&n);
		creat(1,n,1);
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a);
			insert(i,a,1);
		}
		getchar();
		while(scanf("%s",s),s[0]!='E')
		{
			scanf("%d%d",&x,&y);
			if(s[0]=='A')insert(x,y,1);
			else if(s[0]=='S')insert(x,-y,1);
			else if(s[0]=='Q')printf("%d\n",search(x,y,1));
		}
	}
	return 0;
}

				


 

 

posted @ 2013-05-23 20:00  javawebsoa  Views(134)  Comments(0Edit  收藏  举报