您的位置:首页 > 其它

POJ 2155 解题报告

2015-09-12 02:17 417 查看
这道题最简单的方法是用二维树状数组。

一维树状数组比较常见,资料也比较多:

TopCoder: https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-%20trees/ http://dongxicheng.org/structure/binary_indexed_tree/
看完后大致了解了。

但是这道题需要用到二维树状数组,并且是“区域更新,单点查询”,这也与常见用法不一致。所以我也不清楚为什么需要这样更新和查询(重点在于查询出来的不是区域么?),尽管博客讲解很详细:
http://www.cnblogs.com/chenxiwenruo/p/3388783.html
thestoryofsnow2155Accepted4088K422MSC++1520B
/*
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 > 0; i -= lowbit(i))
{
for (int j = y; j > 0; j -= lowbit(j))
{
C[i][j] += delta;
}
}
}

int queryBIT(int x, int y)
{
int sum = 0;
for (int i = x; i <= N; i += lowbit(i))
{
for (int j = y; j <= N; 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: