Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 17880 | Accepted: 6709 |
Description
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].
Input
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.
Output
There is a blank line between every two continuous test cases.
Sample Input
1
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 Output
1
0
0
1
Source
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; #define MAXN 1100 int tree[MAXN*][MAXN*],s; void Negate_y(int d,int dy,int L,int R,int y1,int y2) //取反操作
{
if(L==y1 && R==y2){ //将这个矩阵的所有元素记录为取反
tree[d][dy] = (tree[d][dy]+) % ;
return ;
} int mid = (L+R)>>;
if(mid>=y2)
Negate_y(d,dy<<,L,mid,y1,y2);
else if(mid<y1)
Negate_y(d,dy<<|,mid+,R,y1,y2);
else{
Negate_y(d,dy<<,L,mid,y1,mid);
Negate_y(d,dy<<|,mid+,R,mid+,y2);
}
} void Negate_x(int d,int L,int R,int x1,int y1,int x2,int y2) //取反操作
{
if(L==x1 && R==x2){ //找到行块
Negate_y(d,,,s,y1,y2);
return ;
} int mid = (L+R)>>;
if(mid>=x2)
Negate_x(d<<,L,mid,x1,y1,x2,y2);
else if(mid<x1)
Negate_x(d<<|,mid+,R,x1,y1,x2,y2);
else{
Negate_x(d<<,L,mid,x1,y1,mid,y2);
Negate_x(d<<|,mid+,R,mid+,y1,x2,y2);
}
} int Query_y(int d,int dy,int L,int R,int r) //查询
{
if(L==R) //找到要找的坐标,输出这个坐标对应的值
return tree[d][dy]; //没找到
int mid = (L+R)>>;
if(mid >= r)
return (Query_y(d,dy<<,L,mid,r)+tree[d][dy]) % ;
else
return (Query_y(d,dy<<|,mid+,R,r)+tree[d][dy]) % ;
} int Query_x(int d,int L,int R,int l,int r) //查询
{
if(L==R){ //找到要找的行块,继续查找列块
return Query_y(d,,,s,r);
} //没找到
int mid = (L+R)>>;
if(mid >= l)
return (Query_x(d<<,L,mid,l,r) + Query_y(d,,,s,r)) % ;
else
return (Query_x(d<<|,mid+,R,l,r) + Query_y(d,,,s,r)) % ;
} int main()
{
int X,T,x,y,x1,y1,x2,y2;
scanf("%d",&X);
while(X--){
memset(tree,,sizeof(tree));
scanf("%d%d",&s,&T);
while(T--){
char c[];
scanf("%s",c);
if(c[]=='C'){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
Negate_x(,,s,x1,y1,x2,y2);
}
else if(c[]=='Q'){
scanf("%d%d",&x,&y);
printf("%d\n",Query_x(,,s,x,y));
}
}
if(X!=)
printf("\n");
} return ;
}
Freecode : www.cnblogs.com/yym2013