POJ2155 Matrix 【二维线段树】

题目链接

POJ2155

题解

二维线段树水题,蒟蒻本想拿来养生一下
数据结构真的是有毒啊,,
TM这题卡常
动态开点线段树会TLE【也不知道为什么】
直接开个二维数组反倒能过

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 4005,maxm = 100005,INF = 1000000000;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
	return out * flag;
}
int val[maxn][maxn],n,m;
inline void modify(int fa,int u,int l,int r,int L,int R){
	if (l >= L && r <= R){val[fa][u]++; return;}
	int mid = l + r >> 1;
	if (mid >= L) modify(fa,u << 1,l,mid,L,R);
	if (mid < R) modify(fa,u << 1 | 1,mid + 1,r,L,R);
}
inline int query(int fa,int u,int l,int r,int pos){
	int ans = val[fa][u];
	if (l == r) return ans;
	int mid = l + r >> 1;
	if (mid >= pos) ans += query(fa,u << 1,l,mid,pos);
	else ans += query(fa,u << 1 | 1,mid + 1,r,pos);
	return ans;
}
inline void Modify(int u,int l,int r,int L,int R,int ll,int rr){
	if (l >= L && r <= R){modify(u,1,1,n,ll,rr); return;}
	int mid = l + r >> 1;
	if (mid >= L) Modify(u << 1,l,mid,L,R,ll,rr);
	if (mid < R) Modify(u << 1 | 1,mid + 1,r,L,R,ll,rr);
}
inline int Query(int u,int l,int r,int Pos,int pos){
	int ans = query(u,1,1,n,pos);
	if (l == r) return ans;
	int mid = l + r >> 1;
	if (mid >= Pos) ans += Query(u << 1,l,mid,Pos,pos);
	else ans += Query(u << 1 | 1,mid + 1,r,Pos,pos);
	return ans;
}
int main(){
	int T = read();
	while (T--){
		n = read(); m = read(); cls(val);
		char opt; int a,b,c,d,ans;
		while (m--){
			scanf("%c",&opt);
			if (opt == 'C'){
				a = read(); c = read(); b = read(); d = read();
				Modify(1,1,n,a,b,c,d);
			}
			else {
				a = read(); b = read();
				ans = Query(1,1,n,a,b);
				printf("%d\n",ans & 1);
			}
		}
		if (T) puts("");
	}
	return 0;
}

posted @ 2018-05-16 21:22  Mychael  阅读(152)  评论(0编辑  收藏  举报