【树状数组】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;
}
时间: 2024-12-12 17:16:05

【树状数组】POJ 2155 Matrix的相关文章

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

题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号:为1,倒数第二头牛的编号为除去最后一头牛的编号后的第a[i-1] + 1编号:为3,其他的类推,所以可以维护之前已经选掉的编号,求第k大的数字,sum[rt] 表示该区间已经被选掉的点的个数.另外树状数组也可以做,只不过用二分优化查找第k大的位置. 收获:逆向思维,求动态第K大 代码(线段树): /

树状数组 POJ 2481 Cows

题目传送门 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAX_N = 100000 + 10; 7 int cnt[MAX_N]; 8 int ans[MAX_N]; 9 int maxn = -1; 10 struct node 11 { 12 int s, e; 13 int id; 14 }cow[MAX_

LCA+树状数组 POJ 2763 Housewife Wind

题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储每条边的信息,这样第二种操作也能用BIT快速解决 利用RMQ的写法不知哪里写挫了,改用倍增法 /************************************************ * Author :Running_Time * Created Time :2015/10/6 星期二

( 树状数组) poj 2029

Get Many Persimmon Trees Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3700   Accepted: 2405 Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18th century. In

(树状数组) poj 3067

Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21769   Accepted: 5885 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coas

(树状数组) poj 2481

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13421   Accepted: 4442 Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in hi

[qbzt寒假]线段树和树状数组

树状数组 \(lowbit(x)=x\&(-x)\) 二维树状数组 修改某个点,查询(1,1)到(n,m)的前缀和(树状数组要从1开始) HDU2642 Stars \(YFF\)是个浪漫的人,他喜欢数天上的星星. 为了解决这个问题,我们考虑到天空是一个二维平面,有时星星会很亮,有时星星会很暗.首先,天空中没有明亮的星星,然后一些信息会被给出为"\(B\) \(x\) \(y\)",其中"\(B\)"表示明亮,\(x\)表示\(x\)坐标,\(y\)表示在\

POJ - 2155 Matrix (二维树状数组 + 区间改动 + 单点求值 或者 二维线段树 + 区间更新 + 单点求值)

POJ - 2155 Matrix Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status 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 ha

POJ 2155 Matrix 【二维树状数组】

题目链接:http://poj.org/problem?id=2155 题目大意:给出一个N*N的0矩阵,下面给出两种指令:1. 给出的第一个数据为'C',再给出四个整形数据,x1,y1,y1,y2,对以(x1,y1)(x2,y2)分别为左上角和右下角坐标的矩阵内的元素进行反转(0变1,1变0)         2. 给出的第一个数据为'Q',再给出两个数据,x,y,然后输出此时这个坐标上的元素. 这题用二维树状数组解,树状数组能够对某区间更新所有元素的和,树状数组维护的是c[1][1]到c[i

POJ 2155 Matrix【二维树状数组+YY(区间更新,单点查询)】

题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 32950   Accepted: 11943 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 col