【树状数组】POJ 2155 Matrix
附一篇经典翻译,学习 树状数组 http://www.hawstein.com/posts/binary-indexed-trees.html
/**
* @author johnsondu
* @time 2015-8-22
* @type 2D Binary Index Tree
* @strategy 如果翻转的是(x1,y1), (x2,y2)区域。则相当于
* 翻转(0, 0)~(x2, y2), 然后再翻转(0,0)~(x1-1, y2)
* (0, 0)~(x2, y1-1), (0, 0)~(x1-1, y1-1);
* 由于翻转两次等于没有翻转。 此处类比容斥原理
* @url http://poj.org/problem?id=2155
*/ #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <vector> using namespace std; const int N = 1005;
int c[N][N], n, q; int lowbit(int x)
{
return (x & (-x));
} void update(int x, int y)
{
for(int i = x; i <= n; i += lowbit(i))
for(int j = y; j <= n; j += lowbit(j))
c[i][j] ++;
} int query(int x, int y)
{
int sum = 0;
for(int i = x; i > 0; i -= lowbit(i))
for(int j = y; j > 0; j -= lowbit(j))
sum += c[i][j];
return sum;
} int main()
{
int tcase;
int x1, y1, x2, y2;
scanf("%d", &tcase) ;
while(tcase --) {
memset(c, 0, sizeof(c));
scanf("%d%d", &n, &q);
for(int i = 0; i < q; i ++)
{
char command[5];
scanf("%s", command);
if(command[0] == 'C')
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
x1 ++; y1 ++; x2 ++; y2 ++;
update(x2, y2);
update(x1 - 1, y1 - 1);
update(x1 - 1, y2);
update(x2, y1 - 1);
}
else
{
scanf("%d%d", &x1, &y1);
printf("%d\n", query(x1, y1) & 1);
}
}
printf("\n"); } return 0;
}
【树状数组】POJ 2155 Matrix的更多相关文章
- 线段树/树状数组 POJ 2182 Lost Cows
题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号: ...
- 树状数组 || POJ 2352 Stars
Astronomers often examine star maps where stars are represented by points on a plane and each star h ...
- 树状数组 || POJ 3321 Apple Tree
一道dfs序+树状数组的题 因为并没有get到dfs序以及对树状数组也不熟练卡了很久orz dfs序: in和out是时间戳 dfs序可以将树转化成为一个序列,满足区间 -> 子树 然后就可以用 ...
- LCA+树状数组 POJ 2763 Housewife Wind
题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...
- 树状数组 POJ 2481 Cows
题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...
- POJ 2155 Matrix【二维树状数组+YY(区间计数)】
题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- poj 2155 Matrix (树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16797 Accepted: 6312 Descripti ...
- POJ 2155 Matrix(树状数组+容斥原理)
[题目链接] http://poj.org/problem?id=2155 [题目大意] 要求维护两个操作,矩阵翻转和单点查询 [题解] 树状数组可以处理前缀和问题,前缀之间进行容斥即可得到答案. [ ...
- POJ 2155 Matrix(二维树状数组,绝对具体)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20599 Accepted: 7673 Descripti ...
随机推荐
- Js打开QQ聊天对话窗口
function openQQ() { var qq = $(this).attr('data-qq');//获取qq号 window.open('http://wpa.qq.com/msgrd?v= ...
- Linux+Apache+PHP+MySQL服务器环境配置(CentOS篇)
1.配置php.ini vi /etc/php.ini 2.配置apache 先给需要配置的文件做个备份 cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/h ...
- javascript DOM编程艺术 第10章问题记录
为什么moveElement函数调用时,必须加字符串的拼接符 var repeat = "moveElement('"+elementID+"',"+final ...
- MonoBehaviour简述
Unity中的脚本都是继承自MonoBehaviour. 一.基础函数: 创建脚本就默认的update.start方法:(这些官方的文档都是有的) Start:Update函数第一次运行前调用,一般用 ...
- JS——标记
continue 语句(带有或不带标签引用)只能用在循环中.break 语句(不带标签引用),只能用在循环或 switch 中.通过标签引用,break 语句可用于跳出任何 JavaScript 代码 ...
- 集合Set、List、Map的遍历方法
package com.shellway.javase; import java.util.ArrayList; import java.util.Collection; import java.ut ...
- [Windows Server 2012] Discuz X3安全设置
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★[护卫神·V课堂]是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:Discu ...
- dotnetnuke 7.x登录时不跳到站点设置中的指定页
查源码发现登录按钮有参数,点击跳到登录页或者弹窗登录,真正登录后会根据传参的url反回.因为皮肤对像没有相应参数,所以只能去掉参数.我是用js去的,偷个懒吧.如下所示: <script type ...
- illumina测序原理
一些常用基本概念的介绍: flowcell流动池 是指Illumina测序时,测序反应发生的位置,1个flowcell含有8条lane lane通道 每一个flowcell上都有8条泳道,用于测序反应 ...
- Oracle 函数总结
<1>=========================返回 String,其中包含有与指定的字符代码相关的字符======================== 函 数:< ...