Poj--2155(二维线段树)

2014-09-26 02:21:14

思路:初写二维线段树,把点查询当成区间查询也是智压了QAQ。参考:http://www.cnblogs.com/gj-Acit/p/3258880.html

 1 /*************************************************************************
 2     > File Name: p2155.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Fri 26 Sep 2014 01:09:19 AM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <queue>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 typedef long long ll;
18 #define lpos pos << 1
19 #define rpos pos << 1|1
20 #define getmid(l,r) (l + (r - l) / 2)
21 const int INF = 1 << 29;
22 const int maxn = 1010;
23 
24 bool tree[maxn << 2][maxn << 2];
25 int X1,Y1,X2,Y2,cnt;
26 int N,T,Case;
27 
28 void Update_y(int xpos,int ypos,int l,int r){
29     if(Y1 <= l && r <= Y2){
30         tree[xpos][ypos] = !tree[xpos][ypos];
31         return;
32     }
33     int mid = getmid(l,r);
34     if(Y1 <= mid) Update_y(xpos,ypos << 1,l,mid);
35     if(Y2 > mid) Update_y(xpos,ypos << 1|1,mid + 1,r);
36 }
37 
38 void Update_x(int pos,int l,int r){
39     if(X1 <= l && r <= X2){
40         Update_y(pos,1,1,N);
41         return;
42     }
43     int mid = getmid(l,r);
44     if(X1 <= mid) Update_x(lpos,l,mid);
45     if(X2 > mid) Update_x(rpos,mid + 1,r);
46 }
47 
48 void Query_y(int xpos,int ypos,int l,int r){
49     if(tree[xpos][ypos])
50         ++cnt;
51     if(l == r)
52         return;
53     int mid = getmid(l,r);
54     if(Y1 <= mid) Query_y(xpos,ypos << 1,l,mid);
55     else Query_y(xpos,ypos << 1|1,mid + 1,r);
56 }    
57 
58 void Query_x(int pos,int l,int r){
59     Query_y(pos,1,1,N);
60     if(l == r)
61         return;
62     int mid = getmid(l,r);
63     if(X1 <= mid) Query_x(pos << 1,l,mid);
64     else Query_x(pos << 1|1,mid + 1,r);
65 }
66 
67 int main(){
68     char s[5];
69     scanf("%d",&Case);
70     while(Case--){
71         memset(tree,false,sizeof(tree));
72         scanf("%d%d",&N,&T);
73         while(T--){
74             scanf("%s",s);
75             if(s[0] == 'C'){
76                 scanf("%d%d%d%d",&X1,&Y1,&X2,&Y2);
77                 Update_x(1,1,N);
78             }
79             else{
80                 scanf("%d%d",&X1,&Y1);
81                 cnt = 0;
82                  Query_x(1,1,N);
83                 if(cnt & 1) printf("1\n");
84                 else printf("0\n");
85             }
86         }
87         if(Case) puts("");
88     }
89     return 0;
90 }

 

posted @ 2014-09-26 02:22  Naturain  阅读(130)  评论(0)    收藏  举报