二维树状数组。。。。

                         Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 15575   Accepted: 5854

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

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int tree[][],n,m,t; inline int lowbit(int x)
{
return -x&x;
} void update(int x,int y,int v)
{
int temp=y;
while(x<=n)
{
y=temp;
while(y<=n)
{
tree[x][y]+=v;
y+=lowbit(y);
}
x+=lowbit(x);
}
} int getsum(int x,int y)
{
int sum=;
int temp=y;
while(x>)
{
y=temp;
while(y>)
{
sum+=tree[x][y];
y-=lowbit(y);
}
x-=lowbit(x);
}
return sum;
} int main()
{
scanf("%d",&t);
while(t--)
{
memset(tree,,sizeof(tree));
scanf("%d%d",&n,&m);
n++;char str[];
while(m--)
{
scanf("%s",str);
if(str[]=='C')
{
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1++;y1++;x2++;y2++;
update(x1,y1,);
update(x1,y2+,-);
update(x2+,y1,-);
update(x2+,y2+,);
}
else if(str[]=='Q')
{
int x,y;
scanf("%d%d",&x,&y);
x++;y++;
printf("%d\n",getsum(x,y)&);
}
}
putchar();
}
return ;
}

POJ 2155 Matrix的更多相关文章

  1. POJ poj 2155 Matrix

    题目链接[http://poj.org/problem?id=2155] /* poj 2155 Matrix 题意:矩阵加减,单点求和 二维线段树,矩阵加减,单点求和. */ using names ...

  2. POJ 2155 Matrix (D区段树)

    http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 1 ...

  3. POJ 2155 Matrix【二维树状数组+YY(区间计数)】

    题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

  4. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  5. POJ 2155 Matrix (二维线段树)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226   Accepted: 6461 Descripti ...

  6. POJ 2155 Matrix (矩形)

    date:公元2017年7月19日适逢周三: location:清北集训 杭州 point:二维树状数组/二维差分 Matrix Time Limit: 3000MS   Memory Limit:  ...

  7. poj 2155 Matrix (树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16797   Accepted: 6312 Descripti ...

  8. POJ 2155 Matrix(树状数组+容斥原理)

    [题目链接] http://poj.org/problem?id=2155 [题目大意] 要求维护两个操作,矩阵翻转和单点查询 [题解] 树状数组可以处理前缀和问题,前缀之间进行容斥即可得到答案. [ ...

  9. POJ 2155 Matrix(二维树状数组,绝对具体)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 Descripti ...

随机推荐

  1. hibernate关联映射学习

  2. Mac系统搭建Go语言Sublime Text 2环境配置

    Go语言是谷歌自家的编译型语言,旨在不损失性能的前提下降低代码复杂率.其优势是让软件充分发挥多核心处理器同步多工的优点,并可解决面向对象程序设计的麻烦. 一.安装Golang的SDK 在官网http: ...

  3. ArrayList用法

    ArrayList属于collection->List接口下的一个集合类 针对数组的一些缺陷,集合框架提供了ArrayList集合类,对数组进行封装,实现了长度可变的数组,而且和数组彩用相同的存 ...

  4. python函数基础

    一.基础 函数的定义 def 函数名(参数): ... 函数体 ... 返回值 #如果函数执行完毕也没有return语句时,自动return None. 空函数 如果想定义一个什么事也不做的空函数,可 ...

  5. Raspberry Pi 3 FAQ --- connect automatically to 'mirrors.zju.edu.cn' when downloading and how to accelerate download

    modify the software source: The software source is a place where several free application for linux ...

  6. 从Paxos到ZooKeeper-三、ZooKeeper的典型应用场景

    ZooKeeper是一个典型的发布/订阅模式的分布式数据管理与协调框架,开发人员可以使用它来进行分布式数据的发布与订阅.另一方面,通过对ZooKeeper中丰富的数据节点类型进行交叉使用,配合Watc ...

  7. EasyUi 方法传递多个参数值得方法

    1.项目中需要传递多个参数值 function actionFtt(value, row, index) { //传递查询需要的参数 var customerId = row.customerId;/ ...

  8. HTML meta viewport属性详细说明

    viewport并非只是ios上的独有属性,在android.winphone上同样也有viewport,下面为大家详细介绍下HTML meta viewport 什么是Viewport 手机浏览器是 ...

  9. 删除elasticsearch索引脚本

    只保留七天的索引 shell版 #!/bin/bash #hexm@ #只保留一周es日志 logName=( -nginxaccesslog -nginxerrorlog -phperrorlog ...

  10. 用arp-scan扫描局域网IP地址

    1,在安装之前需要安装yum install -y libpcap libpcap-devel如果没有安装yum工具需要用rpm安装如下软件包[root@oradba arp-scan-1.8]# y ...