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 ...why?

Posted by zxw090234 at 2010-08-14 11:26:53 on Problem 3468
#include<stdio.h>
typedef struct 
{
	int l,r;
	int num;
	__int64 sum;
}node;
node tree[4*100050];
__int64 sum;

int build_tree(int a,int b,int p)
{
	tree[p].l = a;
	tree[p].r = b;
	tree[p].num = 0;
	tree[p].sum = 0;
	if(a == b)
		return 1;
	if(a < b)
	{
		int t = (a + b) >> 1;
		build_tree( a, t, 2*p);
		build_tree( t+1, b, 2*p+1);
	}
	return 0;
}

int insert(int a,int b,int h,int p)
{
	if(tree[p].l == a && tree[p].r == b)
	{
		tree[p].num += h;
		return 1;
	}
	else
	{
		tree[p].sum +=(b - a + 1)* h;
		int t = (tree[p].l + tree[p].r) >> 1;
    	if(b <= t)
	    	insert( a, b, h, 2*p);
    	else if(a > t)
	    	insert( a, b, h, 2*p+1);
    	else
		{
	     	insert( a, t, h, 2*p);
	    	insert( t+1, b, h, 2*p+1);
		}
	}
	return 0;
}

__int64 search_Q(int a,int b,int p)
{
	if(tree[p].l == a && tree[p].r == b)
	{
		sum += tree[p].sum + tree[p].num * ( b - a + 1);
		return 0;
	}
	else
	{
		int t = (tree[p].l + tree[p].r) >> 1;
		if(tree[p].num)
		{
			tree[2*p].num += tree[p].num;
			tree[2*p+1].num += tree[p].num;
			tree[p].sum += (tree[p].r - tree[p].l + 1) * tree[p].num;
			tree[p].num = 0;
		}
		if(b <= t)
			 search_Q( a, b, 2*p);
		else if(a > t)
		     search_Q( a, b, 2*p+1);
		else
		{
		     search_Q( a, t, 2*p);
		     search_Q( t+1, b, 2*p+1);
		}
	}
	return 0;
}


int main()
{
	int N,M;
	int i,k,h;
	int d1,d2,c1,c2,c3;
	char aa;
	scanf("%d%d",&N,&M);
	build_tree( 1, N, 1);
	for(i = 1;i <= N;i ++)
	{
		scanf("%d",&h);
		insert( i, i, h,1);
	}

	for(k = 1;k <= M;k ++)
	{
		scanf(" %c",&aa);
		if(aa == 'Q')
		{
			sum = 0;
			scanf("%d%d",&d1,&d2);
			search_Q( d1, d2, 1);
			printf("%I64d\n",sum);
		}
		if(aa == 'C')
		{
			scanf("%d%d%d",&c1,&c2,&c3);
				insert( c1, c2, c3, 1);
		}
	}
	return 0;
}

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