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

2维树状数组居然TLE了。。。求解啊。

Posted by Mrw at 2010-09-25 16:02:24 on Problem 2155
# include <cstdio>
# include <cstdlib>
# include <cstring>
# include <iostream>
# include <algorithm>
# define MAXN 1007
using namespace std;
int tre[MAXN][MAXN];
int n_tre, n, m, tre_size;

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

inline void Modify( int x, int y, const int& delta )
{
	for( int i = x ; i <= n_tre; i += lowbit(i))
	    for( int j = y ; j <= n_tre; j += lowbit(j))
	        tre[i][j] += delta;
}
inline int get_sum( int x, int y )
{
	int sum = 0;
	for( int i = x ; i != 0; i -= lowbit(x))
	    for( int j = y ; j != 0; j -= lowbit(y))
	        sum += tre[i][j];
	return sum;
}
int Init()
{
	memset( tre, 0, tre_size);
	scanf( "%d%d", &n, &m);
	return 0;
}
int Work()
{
	n_tre = n;
	int i, j, x1, y1, x2, y2;
	char Q;
	while( m-- )
	{
		cin >> Q;
		if( Q == 'C' )
		{
			scanf( "%d%d%d%d", &x1, &y1, &x2, &y2);
			Modify( x1, y1, 1);
			Modify( x2 + 1, y2 + 1, 1);
			Modify(	x1, y2 + 1, 1);
			Modify( x2 + 1, y1, 1);
		}
		else
		{
			scanf( "%d%d", &x1, &y1);
			printf( "%d\n", get_sum( x1, y1) & 1);
		}
	}
	return 0;
}

int main()
{
	int tt;
	scanf( "%d", &tt);
	tre_size = sizeof( tre );
	while( tt-- )
	{
		Init();
		Work();
	}
	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