| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
用线段树TLE的同学进来看看..不要记录每个结点的change的次数,这样必TLE。
记录每个结点表示的矩形的状态即可。
query的时候如果要查找的点被当前矩形覆盖则 res ^= 当前矩形的状态。
参考代码如下:
#include <cstdio>
struct sub_node {
short seg_left;
short seg_right;
bool value;
};
struct seg_node {
short seg_left;
short seg_right;
sub_node sub_nodes[2100];
};
seg_node seg_nodes[2100];
int range, op_num, res;
void build_sub(short p_slot, short slot, short left, short right) {
sub_node *p_cur_node = &seg_nodes[p_slot].sub_nodes[slot];
p_cur_node->seg_left = left;
p_cur_node->seg_right = right;
p_cur_node->value = false;
if(left == right) {
return;
}
short mid = (left + right) >> 1;
build_sub(p_slot, slot * 2, left, mid);
build_sub(p_slot, slot * 2 + 1, mid + 1, right);
}
void build(short slot, short left, short right) {
seg_nodes[slot].seg_left = left;
seg_nodes[slot].seg_right = right;
build_sub(slot, 1, 1, range);
if(left == right) {
return;
}
short mid = (left + right) >> 1;
build(slot * 2, left, mid);
build(slot * 2 + 1, mid + 1, right);
}
void change_sub(short p_slot, short slot, short left, short right) {
sub_node *p_cur_node = &seg_nodes[p_slot].sub_nodes[slot];
if(p_cur_node->seg_left == left && p_cur_node->seg_right == right) {
p_cur_node->value = !p_cur_node->value;
return;
}
short mid = (p_cur_node->seg_left + p_cur_node->seg_right) >> 1;
if(right <= mid) {
change_sub(p_slot, slot * 2, left, right);
} else if(left >= mid + 1) {
change_sub(p_slot, slot * 2 + 1, left, right);
} else {
change_sub(p_slot, slot * 2, left, mid);
change_sub(p_slot, slot * 2 + 1, mid + 1, right);
}
}
void change(short slot, short left, short right, short sub_left, short sub_right) {
if(seg_nodes[slot].seg_left == left && seg_nodes[slot].seg_right == right) {
change_sub(slot, 1, sub_left, sub_right);
return;
}
short mid = (seg_nodes[slot].seg_left + seg_nodes[slot].seg_right) >> 1;
if(right <= mid) {
change(slot * 2, left, right, sub_left, sub_right);
} else if(left >= mid + 1) {
change(slot * 2 + 1, left, right, sub_left, sub_right);
} else {
change(slot * 2, left, mid, sub_left, sub_right);
change(slot * 2 + 1, mid + 1, right, sub_left, sub_right);
}
}
void query_sub(short p_slot, short slot, short y) {
sub_node *p_cur_node = &seg_nodes[p_slot].sub_nodes[slot];
res ^= p_cur_node->value;
if(p_cur_node->seg_left == p_cur_node->seg_right) {
return;
}
short mid = (p_cur_node->seg_left + p_cur_node->seg_right) >> 1;
if(y <= mid) {
query_sub(p_slot, slot * 2, y);
} else {
query_sub(p_slot, slot * 2 + 1, y);
}
}
void query(short slot, short x, short y) {
query_sub(slot, 1, y);
if(seg_nodes[slot].seg_left == seg_nodes[slot].seg_right) {
return;
}
short mid = (seg_nodes[slot].seg_left + seg_nodes[slot].seg_right) >> 1;
if(x <= mid) {
query(slot * 2, x, y);
} else {
query(slot * 2 + 1, x, y);
}
}
int main() {
//freopen("e:/data.txt", "r", stdin);
int cases;
scanf("%d", &cases);
for(int c = 0; c < cases; ++c) {
scanf("%d %d", &range, &op_num);
build(1, 1, range);
char op[2];
int x1, y1, x2, y2;
for(int i = 0; i < op_num; ++i) {
scanf("%s", op);
if(op[0] == 'Q') {
scanf("%d %d", &x1, &y1);
res = 0;
query(1, x1, y1);
printf("%d\n", res);
} else if(op[0] == 'C') {
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
change(1, x1, x2, y1, y2);
}
}
putchar('\n');
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator