【树状数组】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 ...
随机推荐
- JavaScript--认识DOM
文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面代码: 将HTM ...
- hibernate.cfg.xml配置
hibernate.hbm2ddl.auto 配置: create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这 ...
- jQuery文档就绪
很多jQuery代码都有如下片段: $(document).ready(function(){ //代码 }) 作用就是等文档结构加载完成后再去执行function中的代码,功能类似于window.o ...
- drupal 8——在CKEditor中导入video media时添加caption会导致video缩小至消失
在CKEditor中,我点击media browser,选择video型的media,并在caption中输入video的名字.当我保存后发现在前台页面的video消失了,只留下video的名字,点击 ...
- Android项目实战_手机安全卫士流量统计
## 1.抽屉控件SlidingDrawer:一定要配置android:handle(把手)和android:content(内容),并在子View中添加把手和内容的布局```java <Sli ...
- jsp 中包含 一个路径为变量的文件
<head> <base href="<%=basePath%>"> <% String fileroot="MyJsp.jsp ...
- JS——锚点的运用
锚点的两种形式: 1.<a href="#a">点击到锚点</a> 2.window.location.hash = "#a"; 最后都 ...
- C# ADO.NET动态数据的增删改查(第五天)
一.插入登录框中用户输入的动态数据 /// <summary> /// 添加数据 /// </summary> /// <param name="sender& ...
- Python语言之函数
1.函数的定义与调用 def function(x): print("function(%s)"%x) function("hello") #call the ...
- C# SetWindowsHookEx
[DllImport("user32.dll")] static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookPr ...