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 Heng_Xing at 2011-07-24 22:48:23 on Problem 1195
#include<stdio.h>
int d[1025][1025];
int n;
int lowbit(int i)
{
	return i&(i*(-1));
}
void add(int i,int j,int w)
{
	int mj=j;
	while(i<=n)
	{
		for(j=mj;j<=n;j+=lowbit(j))
		{
			d[i][j]+=w;
//			j+=lowbit(j);
		}
		i+=lowbit(i);
	}
}

int sum(int i,int j)
{
	int mj=j;
	int s=0;
	while(i>0)
	{
		for(j=mj;j>0;j-=lowbit(j))
			s+=d[i][j];
		i-=lowbit(i);
	}
	return s;
}
int main (void)
{
	int m;
	int ins;
	int a,b,c;
	int r;
	scanf("%d %d",&m,&n);
	while(scanf("%d",&ins),ins!=3)
	{
		if(ins==1)
		{
			scanf("%d %d %d",&a,&b,&c);
			add(a+1,b+1,c);
		}
		else if(ins==2)
		{
			scanf("%d %d %d %d",&a,&b,&c,&r);
			printf("%d\n",sum(c+1,r+1)+sum(a,b)-sum(c+1,b)-sum(a,r+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