HDU 2155 Matrix
Matrix
This problem will be judged on PKU. Original ID: 2155
64-bit integer IO format: %lld Java class name: Main
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 <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct subtree{
int lt,rt;
bool val;
};
struct node{
int lt,rt;
subtree sb[maxn<<];
};
node tree[maxn<<];
int n,m,ans;
void sub(int lt,int rt,int v,subtree *sb){
sb[v].lt = lt;
sb[v].rt = rt;
sb[v].val = false;
if(lt == rt) return;
int mid = (lt + rt)>>;
sub(lt,mid,v<<,sb);
sub(mid+,rt,v<<|,sb);
}
void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
sub(,n,,tree[v].sb);
if(lt == rt) return;
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
}
void subupdate(int lt,int rt,int v,subtree *sb){
if(sb[v].lt >= lt && sb[v].rt <= rt){
sb[v].val ^= ;
return;
}
if(lt <= tree[v<<].rt) subupdate(lt,rt,v<<,sb);
if(rt >= tree[v<<|].lt) subupdate(lt,rt,v<<|,sb);
}
void update(int lt,int rt,int y1,int y2,int v){
if(tree[v].lt >= lt && tree[v].rt <= rt){
subupdate(y1,y2,,tree[v].sb);
return;
}
if(lt <= tree[v<<].rt) update(lt,rt,y1,y2,v<<);
if(rt >= tree[v<<|].lt) update(lt,rt,y1,y2,v<<|); }
void subquery(int y,int v,subtree *sb){
ans ^= sb[v].val;
if(sb[v].lt == sb[v].rt) return;
if(y <= sb[v<<].rt) subquery(y,v<<,sb);
else subquery(y,v<<|,sb);
}
void query(int x,int y,int v){
subquery(y,,tree[v].sb);
if(tree[v].lt == tree[v].rt) return;
if(x <= tree[v<<].rt) query(x,y,v<<);
else query(x,y,v<<|);
}
int main(){
int t,x1,y1,x2,y2;
char s[];
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
build(,n,);
for(int i = ; i < m; ++i){
scanf("%s",s);
if(s[] == 'Q'){
ans = ;
scanf("%d %d",&x1,&y1);
query(x1,y1,);
printf("%d\n",ans);
}else if(s[] == 'C'){
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
update(x1,x2,y1,y2,);
}
}
puts("");
}
return ;
}
最新文章
- c++笔记整理
- vim 长句子中的上下移动
- 11个强大的Visual Studio调试小技巧
- 使用HelloCharts绘制柱状图
- 三、oracle 体系结构
- JavaScript_判断浏览器种类IE、FF、Opera、Safari、chrome及版本
- linux系统基础(一)
- mysql datetime、date、time、timestamp区别
- YZOI Easy Round 2_回文串 string
- 小猪猪C++笔记基础篇(五)表达式、语句
- js中表单的聚焦失焦事件
- vue组件通讯方法汇总(在不使用vuex的情况下)
- sublime text2 中标签高亮效果BracketHighlighter插件
- 设计模式---策略模式Strategy(对象行为型)
- 第11课 std::bind和std::function(2)_std::bind绑定器
- switch...case... 语句中的类型转换
- Java精选笔记_自定义标签
- python random 之基础点名器
- Linux使用free命令查看实际内存占用
- shell编程——保留元字符