HDU 2155 Matrix

Matrix

Time Limit: 3000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2155
64-bit integer IO format: %lld      Java class name: Main
 
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <= i, j <= N). 

We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2), we change all the elements in the rectangle by using "not" operation (if it is a '0' then change it into '1' otherwise change it into '0'). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions. 

1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2). 
2. Q x y (1 <= x, y <= n) querys A[x, y]. 
 

Input

The first line of the input is an integer X (X <= 10) representing the number of test cases. The following X blocks each represents a test case. 

The first line of each block contains two numbers N and T (2 <= N <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. The following T lines each represents an instruction having the format "Q x y" or "C x1 y1 x2 y2", which has been described above. 
 

Output

For each querying output one line, which has an integer representing A[x, y]. 

There is a blank line between every two continuous test cases. 
 

Sample Input

1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1

Sample Output

1
0
0
1

Source

 
解题:二维线段树,第一次玩这鬼玩意,调试了N久。。。挫。。。。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 1001;
18 struct subtree{
19     int lt,rt;
20     bool val;
21 };
22 struct node{
23     int lt,rt;
24     subtree sb[maxn<<2];
25 };
26 node tree[maxn<<2];
27 int n,m,ans;
28 void sub(int lt,int rt,int v,subtree *sb){
29     sb[v].lt = lt;
30     sb[v].rt = rt;
31     sb[v].val = false;
32     if(lt == rt) return;
33     int mid = (lt + rt)>>1;
34     sub(lt,mid,v<<1,sb);
35     sub(mid+1,rt,v<<1|1,sb);
36 }
37 void build(int lt,int rt,int v){
38     tree[v].lt = lt;
39     tree[v].rt = rt;
40     sub(1,n,1,tree[v].sb);
41     if(lt == rt) return;
42     int mid = (lt + rt)>>1;
43     build(lt,mid,v<<1);
44     build(mid+1,rt,v<<1|1);
45 }
46 void subupdate(int lt,int rt,int v,subtree *sb){
47     if(sb[v].lt >= lt && sb[v].rt <= rt){
48         sb[v].val ^= 1;
49         return;
50     }
51     if(lt <= tree[v<<1].rt) subupdate(lt,rt,v<<1,sb);
52     if(rt >= tree[v<<1|1].lt) subupdate(lt,rt,v<<1|1,sb);
53 }
54 void update(int lt,int rt,int y1,int y2,int v){
55     if(tree[v].lt >= lt && tree[v].rt <= rt){
56         subupdate(y1,y2,1,tree[v].sb);
57         return;
58     }
59     if(lt <= tree[v<<1].rt) update(lt,rt,y1,y2,v<<1);
60     if(rt >= tree[v<<1|1].lt) update(lt,rt,y1,y2,v<<1|1);
61 
62 }
63 void subquery(int y,int v,subtree *sb){
64     ans ^= sb[v].val;
65     if(sb[v].lt == sb[v].rt) return;
66     if(y <= sb[v<<1].rt) subquery(y,v<<1,sb);
67     else subquery(y,v<<1|1,sb);
68 }
69 void query(int x,int y,int v){
70     subquery(y,1,tree[v].sb);
71     if(tree[v].lt == tree[v].rt) return;
72     if(x <= tree[v<<1].rt) query(x,y,v<<1);
73     else query(x,y,v<<1|1);
74 }
75 int main(){
76     int t,x1,y1,x2,y2;
77     char s[5];
78     scanf("%d",&t);
79     while(t--){
80         scanf("%d %d",&n,&m);
81         build(1,n,1);
82         for(int i = 0; i < m; ++i){
83             scanf("%s",s);
84             if(s[0] == 'Q'){
85                 ans = 0;
86                 scanf("%d %d",&x1,&y1);
87                 query(x1,y1,1);
88                 printf("%d\n",ans);
89             }else if(s[0] == 'C'){
90                 scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
91                 update(x1,x2,y1,y2,1);
92             }
93         }
94         puts("");
95     }
96     return 0;
97 }
View Code

 

posted @ 2014-10-09 21:33  狂徒归来  阅读(156)  评论(0编辑  收藏  举报