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 anghoo at 2008-07-24 22:16:29 on Problem 3468
我觉得得这题用树状数组更好解呀,为什么我就T了呢?
望大牛指点!
#include <iostream>
using namespace std;
#define temp 100005
int cc[temp];
int n;

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

__int64 sum(int pos)
{
	__int64 num=0;
	while(pos>0)
	{
		num+=cc[pos];
		pos-=lowbit(pos);
	}
	return num;
}
void update(int pos,int d)
{
	while(pos<=n)
	{
		cc[pos]+=d;
		pos+=lowbit(pos);
	}
}

int main()
{
	int i,j,k,q;
	scanf("%d%d",&n,&q);
	for(i=0;i<n;i++)
	{
		scanf("%d",&k);
		update(i+1,k);
	}
	while(q--)
	{
		char ch;
		int a,b,c;
		getchar();
		scanf("%c",&ch);
		if(ch=='Q')
		{
			scanf("%d%d",&a,&b);
			printf("%I64d\n",sum(b)-sum(a-1));
		}
		if(ch=='C')
		{
			scanf("%d%d%d",&a,&b,&c);
			for(i=a;i<=b;i++)
				update(i,c);
		}
	}
	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