這題其實是一個樹狀數組的思維轉換題,一般來說我們是該一個點,求一段和(對于一維的)。但是如果出現下面情況,你會如何處理?(借用TC里介紹樹狀數組的那個大哥的,鏈接在我上一篇樹狀數組文章里)
There is an array of n cards. Each card is putted face down on table. You have two queries:
1. T i j (turn cards from index i to index j, include i-th and j-th card - card which was face down will be face up; card which was face up will be face down)
2. Q i (answer 0 if i-th card is face down else answer 1)
這里是更新一段,但是只求一點,和我們一般的思路不一樣,但是不打緊,我們可以改變思維,我們可以這樣想(引用)
This has solution for each query (and 1 and 2) has time complexity O(log n). In array f (of length n + 1)we will
store each query T (i , j) - we set f[i]++ and f[j + 1]--. For each card k between i and j (include i and j)
sum f[1] + f[2] + ... + f[k] will be increased for 1, for all others will be same as before
(look at the image 2.0 for clarification), so our solution will be described sum
(which is same as cumulative frequency) module 2.

看了這之后,是不是發現原來還可以這樣啊,呵呵,這就是思維轉換了,如果你已經知道這中思路了,那么這篇文章基本不用看了,因為你已經會
好了,接下來我們說說POJ這題吧,你是不是發現這題和上面那個英文描述的題很像呢,只不過這個是二維的,恩,確實,其實上面那個就是
POJ_2155的一維版本,好了這樣說,你應該懂了吧。下面看看代碼吧(建議先自己想哦),再提供篇集訓論文吧

CODE
1
/**//*
2
ID:Klion
3
PROG:POJ_2155
4
LANG:C++
5
*/
6
#include <iostream>
7
using namespace std;
8
int n;
9
int num[1002][1002];
10
void update(int x,int y)
11

{//這里確實很帥啊
12
int y1;
13
while(x <= n)
14
{
15
y1 = y;
16
while(y1 <= n)
17
{
18
num[x][y1] ^= 1;
19
y1 += (y1 & -y1);
20
}
21
x += (x & -x);
22
}
23
}
24
void work(int x1,int y1,int x2,int y2)
25

{//主要是這里,好好想清楚
26
update(x1,y1);
27
update(x2+1,y1);
28
update(x1,y2+1);
29
update(x2+1,y2+1);
30
}
31
int read(int x,int y)
32

{
33
int ret=0;
34
int y1;
35
while(x > 0)
36
{
37
y1 = y;
38
while(y1 > 0)
39
{
40
ret = ((ret + num[x][y1]) & 1);
41
y1 -= (y1 & -y1);
42
}
43
x -= (x & -x);
44
}
45
ret &= 1;
46
return ret;
47
}
48
int main(void)
49

{
50
freopen("POJ_2155.in","r",stdin);
51
freopen("POJ_2155.out","w",stdout);
52
int t;
53
int x1,y1,x2,y2;
54
int x,y;
55
int l;
56
char str[2];
57
scanf("%d",&t);
58
while(t--)
59
{
60
memset(num,0,sizeof(num));
61
scanf("%d%d%*c",&n,&l);
62
for(int i = 0;i < l;i++)
63
{
64
scanf("%s",str);
65
if('Q' == str[0])
66
{
67
scanf("%d%d%*c",&x,&y);
68
printf("%d\n",read(x,y));
69
}
70
else
71
{
72
scanf("%d%d%d%d%*c",&x1,&y1,&x2,&y2);
73
work(x1,y1,x2,y2);
74
}
75
}
76
printf("\n");
77
}
78
return 0;
79
}
80