Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我的怎么超时了?求大牛看看!!!

Posted by chenxuan123456789 at 2012-09-14 00:23:56 on Problem 3468
#include <stdio.h>
#define __int64 int
#define len 100000
typedef struct node
{
	int lchild;
	int rchild;
	int sum;
}Node;
Node a[len*100];
int num[len];
void bulit(int s,int e,int step)
{
	int i,sum=0,mid;
	for(i=s;i<=e;i++)
	sum+=num[i];
	a[step].lchild=s;
	a[step].rchild=e;
	if(s==e)
	{
		a[step].sum=sum;
		return ;
	}
	else
	{
		mid=(s+e)>>1;
		if(s<e)
		{
			bulit(s,mid,2*step);
			bulit(mid+1,e,2*step+1);
			a[step].sum=sum;
		}
	}
}
void print(int s,int e,int step)
{
	int mid;
	if(s==e)
	{
		printf("%d %d %d %d\n",step,a[step].lchild,a[step].rchild,a[step].sum);
		return ;
	}
	else
	{
		mid=(s+e)>>1;
		print(s,mid,step*2);
		print(mid+1,e,2*step+1);
		printf("%d %d %d %d\n",step,a[step].lchild,a[step].rchild,a[step].sum);
	}
}
int query(int s,int t,int step)
{
	int mid;
	if(s==a[step].lchild&&t==a[step].rchild)
		return a[step].sum;
	else
	{
		mid=(a[step].lchild+a[step].rchild)>>1;
		if(t<=mid)
			return query(s,t,2*step);
		   else
			if(mid<s)
			 return query(s,t,2*step+1);
			else
				return query(s,mid,2*step)+query(mid+1,t,2*step+1);
	}
}
void update(int s,int e,int step,int value)
{
	int sum=0,i,mid;
	for(i=s;i<=e;i++)
	sum+=value;
	a[step].sum+=sum;
    if(a[step].lchild==a[step].rchild)
		return;
	else
	{
		mid=(a[step].lchild+a[step].rchild)>>1;
		if(mid<s)
			update(s,e,2*step+1,value);
		else
		if(mid>=e)
			update(s,e,2*step,value);
		else
		{
			update(s,mid,2*step,value);
			update(mid+1,e,2*step+1,value);
		}
	}
}
int main()
{
	int n,m,i,s,e,value;
	char op;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		getchar();
		for(i=1;i<=n;i++)
		scanf("%d",&num[i]);
		getchar();
		bulit(1,n,1);
		while(m--)
		{
		    scanf("%c",&op);
			if(op=='Q')
			{
				scanf("%d %d",&s,&e);
				//print(1,n,1);
				printf("%d\n",query(s,e,1));
			}
	        else
			if(op=='C')
			{
			scanf("%d %d %d",&s,&e,&value);
			update(s,e,1,value);
		//	print(1,n,1);
			}
			if(m)
			getchar();
		}	
	}
	return 1;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator