poj-2155 Matrix

呂振麒 bio photo By 呂振麒 Comment

傳送門:

Matrix

題意:

請支援

  1. 矩形 NOT 運算
  2. 詢問某點數值

思路:

二維的 BIT 直接下去就可以了。

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
////////  template end   //////////

int N, Q, sz, x_1, x_2, y_1, y_2;
int bit[2050][2050];

inline int lowerbit(const int &x) { return x&(-x); }

inline void insert(const int &x, const int &y, const int &val){
  for(int xx=x ; xx<=sz ; xx+=lowerbit(xx)){
    for(int yy=y ; yy<=sz ; yy+=lowerbit(yy))
      bit[xx][yy] += val;
  }
}

inline int query(const int& x, const int &y){
  int res = 0;
  for(int xx=x ; xx ; xx-=lowerbit(xx))
    for(int yy=y ; yy ; yy-=lowerbit(yy))
      res += bit[xx][yy];
  return res&1;
}

void init(){
  cin >> N >> Q;
  sz = 1<<(int)ceil(log2(N+1));
  for(int i=1;i<=sz;i++)
    memset(bit[i], 0, sizeof(int)*(sz+1));
}
char ch;
void sol(){
  while(Q--){
    cin >> ch;
    if(ch == 'C'){
      cin >> x_1 >> y_1 >> x_2 >> y_2;
      insert(x_1, y_1, 1);      // 使用差分矩陣
      insert(x_1, y_2+1, -1);
      insert(x_2+1, y_1, -1);
      insert(x_2+1, y_2+1, 1);
    }else{
      cin >> x_1 >> y_1;
      cout << query(x_1, y_1) << endl;
    }
  }
}



int main(){
  ios::sync_with_stdio(false);cin.tie(0);
  int ncase = 1;
  cin >> ncase;
  while(ncase--){
    init();
    sol();
    if(ncase) cout << endl;
  }
}
comments powered by Disqus