附一篇经典翻译,学习 树状数组  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的更多相关文章

  1. 线段树/树状数组 POJ 2182 Lost Cows

    题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号: ...

  2. 树状数组 || POJ 2352 Stars

    Astronomers often examine star maps where stars are represented by points on a plane and each star h ...

  3. 树状数组 || POJ 3321 Apple Tree

    一道dfs序+树状数组的题 因为并没有get到dfs序以及对树状数组也不熟练卡了很久orz dfs序: in和out是时间戳 dfs序可以将树转化成为一个序列,满足区间 -> 子树 然后就可以用 ...

  4. LCA+树状数组 POJ 2763 Housewife Wind

    题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...

  5. 树状数组 POJ 2481 Cows

    题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...

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

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

  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. Django总结四

    0.ORM操作 1.必会的13条 返回对象列表的 all filter exclude order_by reverse distinct 特殊的对象列表 values values_list 返回对 ...

  2. DataFrame入门案例(集团公司对人事信息处理场景)

    我用一个集团公司对人事信息处理场景的简单案例,来作为入门,详细分析DataFrame上的各种常用操作,包括集团子公司的职工人事信息的合并,职工的部门相关信息查询.职工信息的统计.关联职工与部门信息的统 ...

  3. SpringAop--系统日志简例

    通过Spring的Aop我们可以声明式的配置事务管理,那么同样可以通过SpringAop来进行处理的系统日志该如何实现呢? 一.数据表和实体类的准备 我们要管理系统日志,那么数据表和实体类是必不可少的 ...

  4. python框架之Flask基础篇(二)-------- 数据库的操作

    1.flask连接数据库的四步: 倒入第三方数据库扩展包:from flask_sqlalchemy import SQLAlchemy 配置config属性,连接数据库: app.config[&q ...

  5. 使用Jupter Notebook实现简单的神经网络

    参考:http://python.jobbole.com/82208/ 注:1)# %matplotlib inline 注解可以使Jupyter中显示图片 2)注意包的导入方式 一.使用的Pytho ...

  6. maven——项目构建和依赖管理工具

    apache maven是一个用于项目构建和依赖管理的工具. 添加archetype https://repo1.maven.org/maven2/archetype-catalog.xml 更改本地 ...

  7. js indexOf 列表筛选

    先来一堆效果图:  代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  8. SQLServer 异常捕获,回滚,再抛出

    一个存储过程中多个更新操作,后面的更新操作出现异常,如果不手动回滚前面修改的数据是不会自动撤销的! BEGIN TRY BEGIN TRAN -- ..... COMMIT TRAN END TRY ...

  9. Python_多线程1(创建线程,简单线程同步)

    threading 模块除了包含 _thread 模块中的所有方法外,还提供的其他方法: threading.currentThread(): 返回当前的线程变量. threading.enumera ...

  10. 微信小程序获取二维码并把logo替换为自己的头像

    $avatarUrl = 'http://cms-bucket.nosdn.127.net/2018/05/28/a1a44ffdc2d24f928c1860d4fbf54703.jpeg?image ...