这道题是二维树状数组(BIT)的基本应用。之前已经写过2155,那道题是区域更新,单点输出,比较异常。这道题是基本的单点更新,区域输出,所以比较好理解,一次就过了。
thestoryofsnow | 1195 | Accepted | 4276K | 594MS | C++ |
/*
ID: thestor1
LANG: C++
TASK: poj2155
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>
using namespace std;
const int MAXN = 1000;
int N, T;
int C[MAXN + 1][MAXN + 1];
inline int lowbit(int x)
{
return x & (-x);
}
void updateBIT(int x, int y, int delta)
{
for (int i = x; i <= N; i += lowbit(i))
{
for (int j = y; j <= N; j += lowbit(j))
{
C[i][j] += delta;
}
}
}
int queryBIT(int x, int y)
{
int sum = 0;
for (int i = x; i > 0; i -= lowbit(i))
{
for (int j = y; j > 0; j -= lowbit(j))
{
sum += C[i][j];
}
}
return sum;
}
int main()
{
int X;
scanf("%d", &X);
char type;
int x1, y1, x2, y2;
for (int x = 0; x < X; ++x)
{
scanf("%d%d", &N, &T);
for (int i = 1; i <= N; ++i)
{
for (int j = 1; j <= N; ++j)
{
C[i][j] = 0;
}
}
for (int t = 0; t < T; ++t)
{
scanf(" %c ", &type);
if (type == 'C')
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
updateBIT(x2, y2, 1);
updateBIT(x1 - 1, y2, -1);
updateBIT(x2, y1 - 1, -1);
updateBIT(x1 - 1, y1 - 1, 1);
}
else if (type == 'Q')
{
scanf("%d%d", &x1, &y1);
printf("%d\n", queryBIT(x1, y1) % 2);
}
else
{
assert(false);
}
}
// There is a blank line between every two continuous test cases.
printf("\n");
}
return 0;
}