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 aibing at 2013-05-17 18:27:34 on Problem 2155
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1 
#define rson m + 1 , r , rt << 1 | 1

bool seg[4010][4010];
int n,m,T,ans;

void buildy(int i,int l,int r,int rt)
{
	seg[i][rt] = 0;
	if(l == r) return ;
	int m = (l + r) >> 1;
	buildy(i,lson);
	buildy(i,rson);
}

void buildx(int l,int r,int rt)
{
	buildy(rt,1,n,1);
	if(l == r) return ;
	int m = (l + r) >> 1;
	buildx(lson);
	buildx(rson);
}
void updatey(int i,int L,int R,int l,int r,int rt)
{
	if(L <= l && r <= R)
	{
		seg[i][rt]^= 1;
		return ;
	}
	int m = (l + r) >> 1;
	if(m >= L)
	updatey(i,L,R,lson);
	if(m < R)
	updatey(i,L,R,rson);	
}

void updatex(int L,int R,int y1,int y2,int l,int r,int rt)
{
	if(L <= l && r <= R)
	{
		updatey(rt,y1,y2,1,n,1);
		return ;
	}
	int m = (l + r) >> 1;
	if(L <= m) updatex(L,R,y1,y2,lson);
	if(R > m)  updatex(L,R,y1,y2,rson);
}

void queryy(int i,int y,int l,int r,int rt)
{
	ans^= seg[i][rt];
	if(l == r)
	return ; 
	int m = (l + r) >> 1;
	if(y <= m) queryy(i,y,lson);
	else  queryy(i,y,rson);
}

void queryx(int x,int y,int l,int r,int rt)
{
	queryy(rt,y,1,n,1);
	if(l == r)
	return ;
	int m = (l + r) >> 1;
	if(x <= m) queryx(x,y,lson);
	else queryx(x,y,rson);
}

int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		buildx(1,n,1);
		for(int i = 0 ; i < m ; i++)
		{
			char s[2];
			scanf("%s",s);
			if(s[0] == 'C')
			{
				int x1,x2,y1,y2;
				scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
				updatex(x1,x2,y1,y2,1,n,1);
			}
			else
			{
				ans = 0;
				int x,y;
				scanf("%d%d",&x,&y);
				queryx(x,y,1,n,1);
				printf("%d\n",ans);
			}
		}
		if(T)
		printf("\n");
	}
	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