poj 2155 Matrix 二维树状数组

简介:

   经典题,把一个-=写成=-了查了半天都没查出来……

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
int n;
int sum[1005][1005];
inline int low(int x)
{
    return x&-x;
}
void update(int x,int y,int num)
{
    int Y=y;
    while(x<=n)
    {
        y=Y;
        while(y<=n)
        {
            sum[x][y]+=num;
            y+=low(y);
        }
        x+=low(x);
    }
}
int query(int x,int y)
{
    int Y=y,ans=0;
    while(x>0)
    {
        y=Y;
        while(y>0)
        {
            ans+=sum[x][y];
            y-=low(y);
        }
        x-=low(x);
    }
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    int q;
    while(T--)
    {
        memset(sum,0,sizeof(sum));
        scanf("%d%d",&n,&q);
        int a,b,c,d;
        while(q--)
        {
            getchar();
            switch(getchar())
            {
                case 'C':
                    scanf("%d%d%d%d",&a,&b,&c,&d);
                    c++;d++;
                    update(a,b,1);
                    update(a,d,-1);
                    update(c,b,-1);
                    update(c,d,1);
                    break;
                case 'Q':
                    scanf("%d%d",&a,&b);
                    printf("%d\n",query(a,b)&1);
                    break;
            }
        }
        puts("");
    }
}


目录
相关文章
|
6月前
|
机器学习/深度学习
poj 2155 Matrix (二维树状数组)
这是楼教主出的二维线段树或者是二维树状数组的题,题意很简单,就是有个n*n的矩阵,初始值都是0,然后给你两个操作,一个是给你左上角和右下角的坐标,把这个长方形的区间所有元素反取反(0变1 1变0),另一个是求某个具体坐标的值。 这里我用了二维的线树状数组,一维树状数组可以解决区间更新和点查询的问题,这里只需要加一维就可以了,代码比较好写,不过开始犯了很多低级的错误。
18 0
|
6月前
codeforces 289 B. Polo the Penguin and Matrix
题目意思是在n*m的矩阵中,你可以对矩阵中的每个数加或者减d,求最少的操作次数,使得矩阵中所有的元素相同。 虽然在condeforces中被分到了dp一类,但完全可以通过排序,暴力的方法解决。
25 0
|
索引
LeetCode 54. Spiral Matrix
给定m×n个元素的矩阵(m行,n列),以螺旋顺序[顺时针]返回矩阵的所有元素
67 0
LeetCode 54. Spiral Matrix
LeetCode 59. Spiral Matrix II
给定正整数n,以螺旋顺序生成填充有从1到n2的元素的方阵。
66 0
【1105】Spiral Matrix (25分)【螺旋矩阵】
【1105】Spiral Matrix (25分)【螺旋矩阵】 【1105】Spiral Matrix (25分)【螺旋矩阵】
99 0
|
Java Go
POJ 1163 The Triangle
POJ 1163 The Triangle
84 0
|
索引 Python Java
Leetcode 542:01 矩阵 01 Matrix
题目: 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。 两个相邻元素间的距离为 1 。 Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
755 0
|
Java 索引 Python
Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
811 0

热门文章

最新文章