HDU 2155 Matrix

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

POJ Monthly,Lou Tiancheng

解题:二维线段树,第一次玩这鬼玩意,调试了N久。。。挫。。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 1001;
18 struct subtree{
19     int lt,rt;
20     bool val;
21 };
22 struct node{
23     int lt,rt;
24     subtree sb[maxn<<2];
25 };
26 node tree[maxn<<2];
27 int n,m,ans;
28 void sub(int lt,int rt,int v,subtree *sb){
29     sb[v].lt = lt;
30     sb[v].rt = rt;
31     sb[v].val = false;
32     if(lt == rt) return;
33     int mid = (lt + rt)>>1;
34     sub(lt,mid,v<<1,sb);
35     sub(mid+1,rt,v<<1|1,sb);
36 }
37 void build(int lt,int rt,int v){
38     tree[v].lt = lt;
39     tree[v].rt = rt;
40     sub(1,n,1,tree[v].sb);
41     if(lt == rt) return;
42     int mid = (lt + rt)>>1;
43     build(lt,mid,v<<1);
44     build(mid+1,rt,v<<1|1);
45 }
46 void subupdate(int lt,int rt,int v,subtree *sb){
47     if(sb[v].lt >= lt && sb[v].rt <= rt){
48         sb[v].val ^= 1;
49         return;
50     }
51     if(lt <= tree[v<<1].rt) subupdate(lt,rt,v<<1,sb);
52     if(rt >= tree[v<<1|1].lt) subupdate(lt,rt,v<<1|1,sb);
53 }
54 void update(int lt,int rt,int y1,int y2,int v){
55     if(tree[v].lt >= lt && tree[v].rt <= rt){
56         subupdate(y1,y2,1,tree[v].sb);
57         return;
58     }
59     if(lt <= tree[v<<1].rt) update(lt,rt,y1,y2,v<<1);
60     if(rt >= tree[v<<1|1].lt) update(lt,rt,y1,y2,v<<1|1);
61
62 }
63 void subquery(int y,int v,subtree *sb){
64     ans ^= sb[v].val;
65     if(sb[v].lt == sb[v].rt) return;
66     if(y <= sb[v<<1].rt) subquery(y,v<<1,sb);
67     else subquery(y,v<<1|1,sb);
68 }
69 void query(int x,int y,int v){
70     subquery(y,1,tree[v].sb);
71     if(tree[v].lt == tree[v].rt) return;
72     if(x <= tree[v<<1].rt) query(x,y,v<<1);
73     else query(x,y,v<<1|1);
74 }
75 int main(){
76     int t,x1,y1,x2,y2;
77     char s[5];
78     scanf("%d",&t);
79     while(t--){
80         scanf("%d %d",&n,&m);
81         build(1,n,1);
82         for(int i = 0; i < m; ++i){
83             scanf("%s",s);
84             if(s[0] == ‘Q‘){
85                 ans = 0;
86                 scanf("%d %d",&x1,&y1);
87                 query(x1,y1,1);
88                 printf("%d\n",ans);
89             }else if(s[0] == ‘C‘){
90                 scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
91                 update(x1,x2,y1,y2,1);
92             }
93         }
94         puts("");
95     }
96     return 0;
97 }

时间: 2024-10-06 00:12:31

HDU 2155 Matrix的相关文章

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

POJ 2155 Matrix (D区段树)

http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18143   Accepted: 6813 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. I

HDU 4902 Matrix multiplication

点击打开链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2113    Accepted Submission(s): 956 Problem Description Given two matrices A and B of size n×n, find the product o

HDU 2686 Matrix(最大费用最大流+拆点)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 和POJ3422一样 删掉K把汇点与源点的容量改为2(因为有两个方向的选择)即可 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int ma

HDU多校赛第9场 HDU 4965Fast Matrix Calculation【矩阵运算+数学小知识】

难度上,,,确实,,,不算难 问题是有个矩阵运算的优化 题目是说给个N*K的矩阵A给个K*N的矩阵B(1<=N<=1000 && 1=<K<=6),先把他们乘起来乘为C矩阵,然后算C^(N*N) 相当于 ABABABABABABAB...=(AB)^(N*N) 不如 A(BA)^(N*N-1)B 因为BA乘得K*K的矩阵,K是比较小的 #include <cstdio> #include <cstdlib> #include <cstr

HDU 2830 Matrix Swapping II (DP,最大全1矩阵)

题意  给你一个n*m矩阵  每列都可以随便交换位置   求最优交换后最大的全1子矩阵 又是HDU 1505 1506的变种  但这个更容易了  因为每列都可以交换位置了   那么这一行中所有比i高的都可以与i相邻了  只需要统计这一行有多少个比i高就行了   可以在算出每一行后  把高度大的放前面去  用num[i]记录排序后的列原来的数  这样就有j列比h[i][num[j]]高了  最后的答案也就是max(j*h[i][num[j]]) #include<cstdio> #include

HDU 4920 Matrix multiplication(矩阵相乘)

各种TEL,233啊.没想到是处理掉0的情况就可以过啊.一直以为会有极端数据.没想到竟然是这样的啊..在网上看到了一个AC的神奇的代码,经典的矩阵乘法,只不过把最内层的枚举,移到外面就过了啊...有点不理解啊,复杂度不是一样的吗.. Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 640 

hdu 4920 Matrix multiplication(矩阵乘法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 989    Accepted Submission(s): 396 Problem Description Given two matr

HDU 4920 Matrix multiplication

Problem Description Given two matrices A and B of size n×n, find the product of them. bobo hates big integers. So you are only asked to find the result modulo 3. Input The input consists of several tests. For each tests: The first line contains n (1≤