POJ2155(二维树状数组)
Matrix
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 17226 | Accepted: 6461 |
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
//2017-10-25
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ; int bt[N][N], n, q; int lowbit(int x){
return x&(-x);
} void add(int x, int y, int v){
while(x <= n){
int j = y;
while(j <= n){
bt[x][j] += v;
j += lowbit(j);
}
x += lowbit(x);
}
} int sum(int x, int y){
int sm = ;
while(x > ){
int j = y;
while(j > ){
sm += bt[x][j];
j -= lowbit(j);
}
x -= lowbit(x);
}
return sm;
} int main()
{
int T;
cin>>T;
while(T--){
scanf("%d%d", &n, &q);
memset(bt, , sizeof(bt));
char op;
int x, y, x1, y1;
while(q--){
getchar();
scanf("%c%d%d", &op, &x, &y);
if(op == 'C'){
scanf("%d%d", &x1, &y1);
add(x, y, );
add(x, y1+, -);
add(x1+, y, -);
add(x1+, y1+, );
}else{
printf("%d\n", sum(x, y)%);
}
}if(T)printf("\n");
} return ;
}
POJ2155(二维树状数组)的更多相关文章
- poj2155二维树状数组
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row an ...
- poj2155二维树状数组区间更新
垃圾poj又交不上题了,也不知道自己写的对不对 /* 给定一个矩阵,初始化为0:两种操作 第一种把一块子矩阵里的值翻转:0->1,1->0 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
- 【POJ2155】【二维树状数组】Matrix
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- poj2155一个二维树状数组
...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- POJ2155 Matrix(二维树状数组||区间修改单点查询)
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row an ...
- POJ2155/LNSYOJ113 Matrix【二维树状数组+差分】【做题报告】
这道题是一个二维树状数组,思路十分神奇,其实还是挺水的 题目描述 给定一个N∗NN∗N的矩阵AA,其中矩阵中的元素只有0或者1,其中A[i,j]A[i,j]表示矩阵的第i行和第j列(1≤i,j≤N)( ...
随机推荐
- GitHubPopular运行记录
运行前准备: Android SDK 23.0.3 2.gradle-2.14.1-all 网盘地址 开始运行 按照项目描述中所说 npm i ------- ok react-native run- ...
- Android 流媒体技术见解
一.技术关键点 1. 音频采集:设置合理的采样参数,包括采样率.通道数.采样深度. 2. 视频采集:手机摄像头采集输出的格式(NV12/NV21/YV12),要注意相互间的转换,以及采集出来图像的方向 ...
- Android常用第三方支付
移动支付 用户使用移动的终端完成对所购买商品或者服务的支付功能;分为近场支付(蓝牙支付,刷卡,滴卡),和远程支付(网上支付,短信支付) app支付模块 常见的支付厂商-->常见的支付方式 支付宝 ...
- Docker学习笔记-简单运行.netcore
前言: 环境:centos7.5 64 位 正文: 拉取 microsoft/dotnet, 安装完毕后执行 docker images 可以看到本地已经包含 microsoft/dotnet #包含 ...
- Day4:html和css
Day4:html和css 规范注意 链接里面不能再放链接. a里面可以放入块级元素. 空格规范 选择器与{之间必须包含空格. 如: .class {} 属性名与之后的:符号之间不允许包含空格, 而: ...
- 几种归一化方法的概念及python实现
一 (0,1)标准化: 这是最简单也是最容易想到的方法,通过遍历feature vector里的每一个数据,将Max和Min的记录下来,并通过Max-Min作为基数(即Min=0,Max=1)进行数据 ...
- 安卓之必须了解的实时通信(Socket)
Socket: 有服务器和客户端之分,其是对TCP/IP的封装,使用IP地址加端口,确定一个唯一的点.在Internet上的主机一般运行了多个服务软件,同时提供几种服务.每种服务都打开一个Socket ...
- PHP计算连续签到天数以及累计签到天数
代码如下: /** * 统计连续签到天数以及累计签到天数 * @param string $user_long_id 用户ID * @return array 一维数组 */ function sig ...
- sql server 性能调优之 资源等待 CXPACKET
一.概述 CXPACKET是指:线程正在等待彼此完成并行处理.什么意思呢? 当sql server发现一条指令复杂时,会决定用多个线程并行来执行,由于某些并行线程已完成工作,在等待其它并行线程来同步 ...
- 【原】使用vue2+vue-router+vuex写一个cnode的脚手架
最近喜欢上了markdown的书写方式,所以博客直接写在github上来,点击查看