您的位置:首页 > 其它

poj2155二维线段树,二维树状数组

2016-03-15 21:21 357 查看
Matrix

Time Limit: 3000MS Memory
Limit:
 65536K
Total Submissions: 23048 Accepted: 8560
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 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
一道二维线段树的题,写了好久,题目大意是给一个N*N大小的矩阵,初始赋值为0,根据要求翻转给定矩阵中的数,最后查询,一道区间修改,单点查询的题,树套树就可以了。#include <iostream>
#include <stdio.h>
#include <string.h>
#define mem(a,b) memset((a),(b),sizeof(a))
#define middle int mid=(l+r)/2
#define lsonx l,mid,x*2
#define rsonx mid+1,r,x*2+1
#define lsony l,mid,y*2
#define rsony mid+1,r,y*2+1
using namespace std;
const int M=1005;
int n;
struct treey
{
int l,r,c;
};
struct treex
{
struct treey ntr[M<<2];
int l,r;
} tr[M<<2];
void buildy(int l,int r,int y,int x)
{
tr[x].ntr[y].l=l;
tr[x].ntr[y].r=r;
tr[x].ntr[y].c=0;
if(l==r) return;
middle;
buildy(lsony,x);
buildy(rsony,x);
}
void buildx(int l,int r,int x)
{
tr[x].l=l;
tr[x].r=r;
buildy(1,n,1,x);
if(l==r) return;
middle;
buildx(lsonx);
buildx(rsonx);
}
void updatey(int x1,int y1,int x2,int y2,int x,int y)
{
if(tr[x].ntr[y].l==y1&&tr[x].ntr[y].r==y2)
{
tr[x].ntr[y].c=!tr[x].ntr[y].c;
return;
}
int mid=(tr[x].ntr[y].l+tr[x].ntr[y].r)/2;
if(y2<=mid)
{
updatey(x1,y1,x2,y2,x,y*2);
}
else if(y1>mid)
{
updatey(x1,y1,x2,y2,x,y*2+1);
}
else
{
updatey(x1,y1,x2,mid,x,y*2);
updatey(x1,mid+1,x2,y2,x,y*2+1);
}
}
void updatex(int x1,int y1,int x2,int y2,int x)
{
if(tr[x].l==x1&&tr[x].r==x2)
{
updatey(x1,y1,x2,y2,x,1);
return;
}
int mid=(tr[x].l+tr[x].r)/2;
if(x2<=mid)
updatex(x1,y1,x2,y2,x*2);
else if(x1>mid)
updatex(x1,y1,x2,y2,x*2+1);
else
{
updatex(x1,y1,mid,y2,x*2);
updatex(mid+1,y1,x2,y2,x*2+1);
}
}
int ans;
void get_ansy(int i,int x,int y)
{
ans^=tr[x].ntr[y].c;
if(tr[x].ntr[y].l==tr[x].ntr[y].r)
return;
int mid=(tr[x].ntr[y].l+tr[x].ntr[y].r)/2;
if(i<=mid)
get_ansy(i,x,y*2);
else
get_ansy(i,x,y*2+1);
}
void get_ansx(int x,int y,int i)
{
get_ansy(y,i,1);
if(tr[i].l==tr[i].r)
return ;
int mid=(tr[i].l+tr[i].r)/2;
if(x<=mid)
get_ansx(x,y,i*2);
else
get_ansx(x,y,i*2+1);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int t;
scanf("%d%d",&n,&t);
buildx(1,n,1);
while(t--)
{
char str[5];
scanf("%s",str);
if(str[0]=='C')
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
updatex(x1,y1,x2,y2,1);
}
else if(str[0]=='Q')
{
int x,y;
ans=0;
scanf("%d%d",&x,&y);
get_ansx(x,y,1);
printf("%d\n",ans);
}
}
printf("\n");
}
return 0;
}这道题的另外一种办法,就是树状数组,这里用给定坐标与最大边界间的矩阵,因为要求翻转,所以只需要求翻转次数就可以了。奇数就是翻成了1,偶数还是0,之所以不用最小边界,是因为容易产生给定坐标值为0 0的情况。导致超出数组下届,代码如下。
#include <iostream><pre name="code" class="cpp">#include <iostream>
#include <stdio.h>
#include <string.h>
#define mem(a,b) memset((a),(b),sizeof(a))
using namespace std;
const int M=1005;
int tr[M][M];
int n;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y)
{
for(int i=x; i<=n; i+=lowbit(i))
for(int j=y; j<=n; j+=lowbit(j))
{
tr[i][j]++;
}
return ;
}
int get_ans(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+=tr[i][j];
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int t;
scanf("%d%d",&n,&t);
mem(tr,0);
while(t--)
{
char str[5];
scanf("%s",str);
if(str[0]=='C')
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
update(x2+1,y2+1);
update(x1,y2+1);
update(x2+1,y1);
update(x1,y1);
}
else if(str[0]=='Q')
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",get_ans(x,y)%2);
}
}
printf("\n");
}
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: