1
31
2010
0

SGU 177 四分+线段树

 

const int N = 1024;
struct Qua{
	int x, y, X, Y, len;
	char val;
	Qua *nxt[4];
};
void build(int x, int y, int X, int Y, Qua* cur){
	cur->x = x; 
	cur->y = y; 
	cur->X = X; 
	cur->Y = Y; 
	if( X-x == 1 && Y - y == 1){
		cur->val = -1;
		cur->x = x; cur->y = y; cur->X = X; cur->Y = Y;
		return;
	}
	if( X-x < 1 ) return;
	if( Y-y < 1 ) return;
	int mx = (x+X)/2, my = (y+Y)/2;
	build(x,y,mx,my, cur->nxt[0] = new Qua);
	build(mx,my,X,Y, cur->nxt[1] = new Qua);
	build(mx,y,X,my, cur->nxt[2] = new Qua);
	build(x,my,mx,Y, cur->nxt[3] = new Qua);
}


void update(int x, int y, int X, int Y, Qua* cur, char color){
	if( X <= cur->x || cur->X <= x ||
	    Y <= cur->y || cur->Y <= y ) return;
	x = max(x,cur->x);
	X = min(X,cur->X);
	y = max(y,cur->y);
	Y = min(Y,cur->Y);
	if( x == cur->x && X == cur->X && y == cur->y && Y == cur->Y ){
		cur->val = color;
		return;
	}
	if( cur->val != -1 )Rep(i,4)cur->nxt[i]->val = cur->val;
	cur->val = -1; // 清除懒惰标志,不能忘记
	Rep(i,4)update(x,y,X,Y,cur->nxt[i],color);
}


int get(Qua* cur){
	if( cur->val != -1 )return cur->val == 'w' ? (cur->Y-cur->y)*(cur->X-cur->x) : 0;
	int ans = 0;
	Rep(i,4)ans += get(cur->nxt[i]);
	return ans;
}

 

 

Category: 未分类 | Tags: | Read Count: 1293

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com