Matrix

Time Limit: 3000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 2155
64-bit integer IO format: %lld      Java class name: Main

 
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].

 

Input

The 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.

 

Output

For 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 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

 
解题:二维线段树,第一次玩这鬼玩意,调试了N久。。。挫。。。。
 
 #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 ;
}

最新文章

  1. c++笔记整理
  2. vim 长句子中的上下移动
  3. 11个强大的Visual Studio调试小技巧
  4. 使用HelloCharts绘制柱状图
  5. 三、oracle 体系结构
  6. JavaScript_判断浏览器种类IE、FF、Opera、Safari、chrome及版本
  7. linux系统基础(一)
  8. mysql datetime、date、time、timestamp区别
  9. YZOI Easy Round 2_回文串 string
  10. 小猪猪C++笔记基础篇(五)表达式、语句
  11. js中表单的聚焦失焦事件
  12. vue组件通讯方法汇总(在不使用vuex的情况下)
  13. sublime text2 中标签高亮效果BracketHighlighter插件
  14. 设计模式---策略模式Strategy(对象行为型)
  15. 第11课 std::bind和std::function(2)_std::bind绑定器
  16. switch...case... 语句中的类型转换
  17. Java精选笔记_自定义标签
  18. python random 之基础点名器
  19. Linux使用free命令查看实际内存占用
  20. shell编程——保留元字符

热门文章

  1. hdu 4079简单贪心
  2. WebApi传参总动员(一)
  3. NEFU 118
  4. awesome-free-software
  5. hadoop 计数器
  6. SQL 数据库性能优化
  7. 一个基于React整套技术栈+Node.js的前端页面制作工具
  8. JS中innerHTML/outerHTML和innerText/outerText以及value 的区别与使用
  9. 通过obs进行推流
  10. Golden Gate 检查点