您的位置:首页 > 其它

POJ 2155 Matrix(二维线段树)

2016-04-21 18:58 330 查看
http://poj.org/problem?id=2155Matrix
Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 19556Accepted: 7340
DescriptionGiven 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 have A[i, j] = 0 (1 <= i, j <= N). 

We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2), we change all the elements in the rectangle by using "not" operation (if it is a '0' then change it into '1' otherwise change it into '0'). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions. 

1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2). 
2. Q x y (1 <= x, y <= n) querys A[x, y]. 
InputThe first line of the input is an integer X (X <= 10) representing the number of test cases. The following X blocks each represents a test case. 

The first line of each block contains two numbers N and T (2 <= N <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. The following T lines each represents an instruction having the format "Q x y" or "C x1 y1 x2 y2", which has been described above. 
OutputFor each querying output one line, which has an integer representing A[x, y]. 

There is a blank line between every two continuous test cases. 
Sample Input1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1
Sample Output1
0
0
1
SourcePOJ Monthly,Lou Tiancheng二维线段树,区间修改 单点查询 用树套树的思想
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 1000+10
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b)
{
return a>b;
}
struct sub_node
{
int l,r,val;
int get_submid()
{
return (l+r)/2;
}
};
struct node
{
int l,r;
sub_node sub_nod[4*maxn];
int get_mid()
{
return (l+r)/2;
}
}nod[4*maxn];
int n,ans,m;
void build_y(int xi,int yi,int l,int r)//xi yi为标号
{
nod[xi].sub_nod[yi].l=l;
nod[xi].sub_nod[yi].r=r;
nod[xi].sub_nod[yi].val=0;
if(l==r) return ;
int mid=nod[xi].sub_nod[yi].get_submid();
build_y(xi,yi<<1,l,mid);
build_y(xi,yi<<1|1,mid+1,r);
}

void build_x(int xi,int l,int r)
{
nod[xi].l=l;
nod[xi].r=r;
build_y(xi,1,1,n);
if(l==r)return ;
int mid=nod[xi].get_mid();
build_x(xi<<1,l,mid);
build_x(xi<<1|1,mid+1,r);
}

void update_y(int xi,int yi,int y1,int y2)
{
if(nod[xi].sub_nod[yi].l==y1&&nod[xi].sub_nod[yi].r==y2)
{
nod[xi].sub_nod[yi].val^=1;
return ;
}
int mid=nod[xi].sub_nod[yi].get_submid();
if(y2<=mid)update_y(xi,yi<<1,y1,y2);
else if(y1>mid)update_y(xi,yi<<1|1,y1,y2);
else
{
update_y(xi,yi<<1,y1,mid);
update_y(xi,yi<<1|1,mid+1,y2);
}
}

void update_x(int xi,int x1,int x2,int y1,int y2)
{
if(nod[xi].l==x1&&nod[xi].r==x2)
{
update_y(xi,1,y1,y2);
return ;
}
int mid=nod[xi].get_mid();
if(x2<=mid)update_x(xi<<1,x1,x2,y1,y2);
else if(x1>mid)update_x(xi<<1|1,x1,x2,y1,y2);
else
{
update_x(xi<<1,x1,mid,y1,y2);
update_x(xi<<1|1,mid+1,x2,y1,y2);
}
}
void query_y(int xi,int yi,int y)
{
ans^=nod[xi].sub_nod[yi].val;
if(nod[xi].sub_nod[yi].l==nod[xi].sub_nod[yi].r)return ;
int mid=nod[xi].sub_nod[yi].get_submid();
if(y<=mid)query_y(xi,yi<<1,y);
else query_y(xi,yi<<1|1,y);
}

void query_x(int i,int x,int y)
{
query_y(i,1,y);
if(nod[i].l==nod[i].r)return ;
int mid=nod[i].get_mid();
if(x<=mid)query_x(i<<1,x,y);
else query_x(i<<1|1,x,y);
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t;
char s[3];
int x1,y1,x2,y2,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
build_x(1,1,n);
for(int i=1;i<=m;i++)
{
scanf("%s",&s);
if(s[0]=='C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
update_x(1,x1,x2,y1,y2);
}
else
{
ans=0;
scanf("%d%d",&x,&y);
query_x(1,x,y);
printf("%d\n",ans);
}
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: