二维树状数组。。。。

                         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. HttpClient + Jsoup模拟登录教务处并获取课表

    1.概述 最近想做一个校园助手类的APP,由于第一次做,所以打算先把每个功能单独实现,防止乱了阵脚.利用教务处登录获取课表和成绩等是一个基本功能,所以以获取课表为例实现了这个功能.完整代码点这里,尝试 ...

  2. 详解使用icomoon生成字体图标的方法并应用

    原文:http://blog.csdn.net/u013938465/article/details/50680468 最近项目大量用到字体图标,大家也知道,字体图标任意缩放不会失真,也大大减少请求数 ...

  3. CSS,font-family,好看常用的中文字体

    例1(小米米官网):font-family: "Arial","Microsoft YaHei","黑体","宋体",s ...

  4. POJ 2484 A Funny Game(博弈论)

    题目链接: 传送门 A Funny Game Time Limit: 1000MS     Memory Limit: 10000K Description Alice and Bob decide ...

  5. CF 208A Dubstep(简单字符串处理)

    题目链接: 传送门 Dubstep Time Limit: 1000MS     Memory Limit: 32768 KB Description Vasya works as a DJ in t ...

  6. HD2059龟兔赛跑(DP)

    题目链接 直接拿来当贪心做了=_=,然后就懵逼了 动态规划,本弱真没想到=_= #include <iostream> #include <cstdio> #include & ...

  7. const 和 readonly 修饰符的用法

    1. 只有C#内置类型(int,double,long等)可以声明为const;结果.类和数组不能声明为const. 2. readonly 是在字段上使用的修饰符,直接以类名.字段访问. 3. co ...

  8. 过滤字符串的Html标记 c#函数 .

    .public static string StripHTML(string strHtml) . { . string[] aryReg ={ . @"<script[^>]* ...

  9. MySQL缺失mysql_config文件

    打算爬虫,安装mysqldb 结果使用pip安装出错 在centos-6.4上pip install mysql-python,报错如下[sentry@kjtest111 mysql-python]$ ...

  10. cmd chcp命令切换字符格式UTF8

    cmd chcp命令切换字符格式   命令介绍:   chcp 65001   #换成utf-8代码页   chcp 936       #换成默认的gbk   chcp 437       #美国英 ...