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 longniu at 2009-08-17 10:16:13 on Problem 3468
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "string.h"

int n,q;
__int64 c[100001],sum;

int lowbit(int x)
{
	return x&(-x);
}

__int64 getsum(int x)
{
	__int64 sum=0;
	
	while( x>0 )
	{
		sum+=c[x];
		x-=lowbit(x);
	}
	return sum;
}

int updata(int x,int num)
{
	while( x<=n )
	{
		c[x]+=num;
		x+=lowbit(x);
	}
	return 0;
}

int main()
{
	__int64 a[100001];
	int i,j,x,y,d;
	char ch;
	
	//freopen("3243.txt","r",stdin);
	
	memset(c,0,sizeof(c));
	
	scanf("%d%d",&n,&q);
	for(i=1;i<=n;i++)
	{
		scanf("%I64d",&a[i]);
		updata(i,a[i]);
	}
		
	sum=0;
	
	while(q--)
	{
		scanf(" %c",&ch);
		
		if( ch=='C' )
		{
			scanf("%d%d%d",&x,&y,&d);
                        //可能是这里导致超时了。。。帮个忙吧!大牛
			while(x<=y)//每一点处都要加上 d
			{
				updata(x,d);
				x++;
			}
		}
		//计算区间 i - j 的和
		else
		{
			scanf("%d%d",&i,&j);
			sum=getsum(j) - getsum(i-1);
			printf("%I64d\n",sum);
		}
	}
	
    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