POJ 2155

楼教主的题,很不错的二维树状数组

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <complex>
#include <cstring>
#include <iostream>
#include <algorithm>

#define REP(i,N) for (int i = 0;i < (N);i++)
#define REP_1(i,N) for (int i = 1;i < (N);i++)
#define REP_2(i,be,en) for (int i = (be);i < (en);i++)
#define DWN(i,N) for (int i = (N);i >= 0;i--)
#define DWN_1(i,N) for (int i = (N);i >= 1;i--)
#define DWN_2(i,en,be) for (int i = (en);i >= (be);i--)
#define FR(N) freopen((N),"r",stdin)
#define FW(N) freopen((N),"w",stdout)
#define MAXN 1010
#define GETS(ch) fgets((ch),MAXN,stdin)
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;

typedef long long LL;

typedef map<int,LL> MINT;
typedef map<char,int> MCH;
typedef map<string,int> MSTR;

typedef vector<int> VINT;
typedef set<LL> SINT;
typedef pair<int,int> PINT;

LL read(){
    LL ret=0,f=1;
    char x=getchar();
    while(!(x>=‘0‘ && x<=‘9‘)){if(x==‘-‘)f=-1;x=getchar();}
    while(x>=‘0‘ && x<=‘9‘) ret=ret*10+x-‘0‘, x=getchar();
    return ret*f;
}

int c[MAXN][MAXN];
int N;

int lowbit(int x) {
	return x & (-x);
}

int Query(int x,int y) {
	int ans = 0;
	for (int i = x;i > 0;i -= lowbit(i)) {
		for (int j = y;j > 0;j -= lowbit(j)) {
			ans += c[i][j];
		}
	}
	return ans % 2 == 0 ?
			0
			: 1;
}

void Updata(int x,int y,int value) {
	for (int i = x;i <= N;i += lowbit(i)) {
		for (int j = y;j <= N;j += lowbit(j)) {
			c[i][j] += value;
		}
	}
}

int main() {
	//FR("1.txt");
	int T;
	cin >> T;
	while (T--) {
		memset(c,0,sizeof(c));
		int X;
		cin >> N >> X;
		getchar();
		REP(i,X) {
			char c = getchar();
			if (c == ‘C‘) {
				int x1,y1,x2,y2;
				cin >> x1 >> y1 >> x2 >> y2;
				getchar();
				Updata(x1,y1,1);
				Updata(x1,y2 + 1,1);
				Updata(x2 + 1,y1,1);
				Updata(x2 + 1,y2 + 1,1);
			}
			else if (c == ‘Q‘) {
				int x,y;
				cin >> x >> y;
				getchar();
				cout << Query(x,y) << endl;
			}
		}
		cout << endl;
	}
}

  

时间: 2024-08-28 03:12:16

POJ 2155的相关文章

POJ 2155 二维线段树

POJ 2155  二维线段树 思路:二维线段树就是每个节点套一棵线段树的树. 刚开始因为题目是求A[I,J],然后在y查询那直接ans^=Map[i][j]的时候没看懂,后面自己把图画出来了才理解. 因为只有0和1,所以可以用异或来搞,而不需要每次都需要修改. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #incl

POJ 2155 二维线段树 经典的记录所有修改再统一遍历 单点查询

本来是想找一个二维线段树涉及懒惰标记的,一看这个题,区间修改,单点查询,以为是懒惰标记,敲到一半发现这二维线段树就不适合懒惰标记,你更新了某段的某列,但其实其他段的相应列也要打标记,但因为区间不一样,又不好打...也可能是我这是在套用一维线段树的思想,还有更好的二维线段树懒惰标记方法 反正到现在我还没搞定二维线段树的懒惰标记,因为这道题不用懒惰标记,因为是二进制序列,区间修改仅限于翻转操作,那就只要记录每次操作,最后查询的时候从上往下把所有修改都来上一遍,就可以了.就类似于树状数组的第二种用法,

POJ - 2155 Matrix (二维树状数组 + 区间改动 + 单点求值 或者 二维线段树 + 区间更新 + 单点求值)

POJ - 2155 Matrix Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description 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 ha

POJ 2155 Matrix (二维线段树)

http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18143   Accepted: 6813 Description 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. I

POJ poj 2155 Matrix

题目链接[http://poj.org/problem?id=2155] /* poj 2155 Matrix 题意:矩阵加减,单点求和 二维线段树,矩阵加减,单点求和. */ using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1010; int N, Q; struct Nodey { int l, r; int val; }; int locx[MAXN], locy[MAXN]; struct Nodex {

poj 2155 二进制0 1反转---二维树状数组

http://poj.org/problem?id=2155 上午自己搞了很久胡思乱想了很久,然后没思路-----看了论文<浅谈信息学竞赛中的"0"和"1"--二进制思想在信息学竞赛中的应用>,豁然开朗啊,,马上A掉---PE了一次o(╯□╰)o 通过论文学到的两点: 1.多维不会的时候,从一维尝试类比: 2.想法的证明,情况数不多的时候,分类讨论证明很好 #include <cstdio> #include <cstring>

poj 2155 二维树状数组

http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17721   Accepted: 6653 Description 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. I

POJ 2155 Matrix 【二维树状数组】

题目链接:http://poj.org/problem?id=2155 题目大意:给出一个N*N的0矩阵,下面给出两种指令:1. 给出的第一个数据为'C',再给出四个整形数据,x1,y1,y1,y2,对以(x1,y1)(x2,y2)分别为左上角和右下角坐标的矩阵内的元素进行反转(0变1,1变0)         2. 给出的第一个数据为'Q',再给出两个数据,x,y,然后输出此时这个坐标上的元素. 这题用二维树状数组解,树状数组能够对某区间更新所有元素的和,树状数组维护的是c[1][1]到c[i

poj 2155 区间更新 单点查询

Matrix Time Limit: 3000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row

POJ 2155 Matrix (D区段树)

http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18143   Accepted: 6813 Description 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. I