POJ 2155 Matrix

  与以往不同的是,这个树状数组是二维的,仅此而已

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
int n,c[1100][1100];
int lowbit(int k)
{
    return k&(-k);
}
void add(int x,int y,int num)
{
    for(int i = x; i <= n; i += lowbit(i))
        for(int j = y; j <= n; j += lowbit(j))
            c[i][j] += num;

}
int cal(int x,int y)
{
    int sum = 0;
    for(int i = x; i >= 1; i -= lowbit(i))
        for(int j = y; j >= 1; j -= lowbit(j))
            sum += c[i][j];
    return sum;
}
int  main()
{
    int x;
    scanf("%d",&x);
    while(x--)
    {
        int t;
        memset(c,0,sizeof(c));
        scanf("%d%d",&n,&t);
        char op;
        while(t--)
        {
            cin>>op;
            if(op == ‘C‘)
            {
                int x1,y1,x2,y2;
                scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
                x1++,x2++,y1++,y2++;
                add(x2,y2,1);
                add(x1-1,y1-1,1);
                add(x1-1,y2,1);
                add(x2,y1-1,1);
            }
            else
            {
                int x,y;
                scanf("%d%d",&x,&y);
                //++x,++y
                int ans = cal(x,y);// - cal(x-1,y-1) - cal(x-1,y) - cal(x,y-1);
                ans %= 2;
                printf("%d\n",ans);
            }
        }
        puts("");
    }
    return 0;
}
时间: 2024-10-12 06:48:38

POJ 2155 Matrix的相关文章

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 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18143   Accepted: 6813 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. I

POJ poj 2155 Matrix

题目链接[http://poj.org/problem?id=2155] /* poj 2155 Matrix 题意:矩阵加减,单点求和 二维线段树,矩阵加减,单点求和. */ using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1010; int N, Q; struct Nodey { int l, r; int val; }; int locx[MAXN], locy[MAXN]; struct Nodex {

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 (D区段树)

http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18143   Accepted: 6813 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. 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

POJ 2155 Matrix(二维树状数组,绝对具体)

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 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

POJ 2155 Matrix(树状数组)

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20138   Accepted: 7522 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

poj 2155 Matrix(树状数组的应用)

http://poj.org/problem?id=2155 对于一个n*n(n <= 1000)的01矩阵,初始全为0,有两种操作. C x1 y1 x2 y2 ,分别代表矩阵的左上角和右下角,将这个矩阵中的01互换,原为0的变为1,原为1的变为0. Q x y询问A[x,y]现在是几. 因为只有01的互换,且原始为0,那么只需计算[x,y]处被换了几次就能确定现在这个格子是几.重点就是怎样快速计算[x,y]格子被换了几次.操作方法是将[x1,y1][x1,y2+1][x2+1,y1][x2+

POJ 2155 Matrix (矩形)

date:公元2017年7月19日适逢周三: location:清北集训 杭州 point:二维树状数组/二维差分 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28325   Accepted: 10341 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the