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

各位大牛们,看看我的代码咋就是wa呢?感激不尽....(错的快让我发飙了)

Posted by hutu_2000 at 2009-07-14 14:49:32 on Problem 3468
#include <iostream>
using namespace std;
#define N 100005
struct point{
	int a,b,lr,rr;                 //a,b表示区间的两端
	long long value,ed;
};
point st[N*4];
int val[N],tot=0;
long long init(int a,int b);
long long getsum(int root,int a,int b);
long long add(int root,int a,int b,int c);
int main()
{
	int n,m,i,x,y,z;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++)
		scanf("%d",&val[i]);
	init(0,n-1);
	for(i=0;i<m;i++)
	{
		char ch;
		getchar();
		scanf("%c",&ch);
		if(ch=='Q')
		{
			scanf("%d %d",&x,&y);
			printf("%lld\n",getsum(0,x-1,y-1));
		}
		else if(ch=='C')
		{
			scanf("%d %d %d",&x,&y,&z);
			add(0,x-1,y-1,z);
		}
		else ;
	}
	return 0;
}

long long init(int a,int b)                //返回a到b的sum值,对st进行初始化
{
	int cur=tot;
	long long g,h;
	st[cur].a=a; st[cur].b=b; st[cur].ed=0;
	if(a>b) return 0;
	else if(a==b)
	{
		st[cur].lr=-1;
		st[cur].rr=-1;
		st[cur].value=val[a];
		return st[cur].value;
	}
	else
	{
		tot++;
		st[cur].lr=tot;
		g=init(a,(a+b)/2);
		tot++;
		st[cur].rr=tot;
		h=init((a+b)/2+1,b);
		st[cur].value=g+h;
		return st[cur].value;
	}
}

long long getsum(int root,int a,int b)                   //返回a到b区间的sum值
{
	if(root==-1) return 0;
	if(st[root].a>a) a=st[root].a;
	if(st[root].b<b) b=st[root].b;
	if(a>b) return 0;
	else if(a==st[root].a&&b==st[root].b)
		return st[root].value+st[root].ed*(b-a+1);
	else
		return getsum(st[root].lr,a,b)+getsum(st[root].rr,a,b)+st[root].ed*(long long)(b-a+1);
}

long long add(int root,int a,int b,int c)          //返回a到b区间的sum值,更新st的值
{
	if(root==-1) return 0;
	if(st[root].a>a) a=st[root].a;
	if(st[root].b<b) b=st[root].b;
	if(a>b) return 0;
	else if(a==st[root].a&&b==st[root].b)
	{
		st[root].ed+=c;
		return st[root].value+st[root].ed*(long long)(b-a+1);
	}
	else
	{
		st[root].value=add(st[root].lr,a,b,c)+add(st[root].rr,a,b,c);
		return st[root].value+st[root].ed*(long long)(b-a+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