poj 2155 区间更新 单点查询

Matrix

Time Limit: 3000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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 <= i, j <= N).

We can change the matrix in the following way. Given a rectangle
whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2),
we change all the elements in the rectangle by using "not" operation (if
it is a ‘0‘ then change it into ‘1‘ otherwise change it into ‘0‘). To
maintain the information of the matrix, you are asked to write a program
to receive and execute two kinds of instructions.

1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2
<= n) changes the matrix by using the rectangle whose upper-left
corner is (x1, y1) and lower-right corner is (x2, y2).

2. Q x y (1 <= x, y <= n) querys A[x, y].

Input

The first line of the input is an integer X (X
<= 10) representing the number of test cases. The following X blocks
each represents a test case.

The first line of each block contains two numbers N and T (2 <= N
<= 1000, 1 <= T <= 50000) representing the size of the matrix
and the number of the instructions. The following T lines each
represents an instruction having the format "Q x y" or "C x1 y1 x2 y2",
which has been described above.

Output

For each querying output one line, which has an integer representing A[x, y].

There is a blank line between every two continuous test cases.

Sample Input

1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1

Sample Output

1
0
0
1
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
#define max 1005
int c[max][max];
int n;

int lowbit(int x)
{
    return x&-x;
}

void update(int x,int y,int val)
{
    for(int i=x; i<=n; i+=lowbit(i))
    {
        for(int j=y; j<=n; j+=lowbit(j))
        {
            c[i][j]+=val;
        }
    }
}

int get_sum(int x,int y)
{
    int s=0;
    for(int i=x; i>0; i-=lowbit(i))
    {
        for(int j=y; j>0; j-=lowbit(j))
        {
            s+=c[i][j];
        }
    }
    return s;
}

int main()
{
   /// freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
   int cc,t,x1,x2,y1,y2,a,b;
    char ch;
    scanf("%d",&cc);
    for(int i=1;i<=cc;i++)
    {
        memset(c,0,sizeof(c));
        scanf("%d%d",&n,&t);
        getchar();
        for(int j=0;j<t;j++)    ///下标不可以从0开始  会死循环
        {
            scanf("%c",&ch);
            if(ch==‘C‘)
            {
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                getchar();
                update(x1,y1,1);
                update(x1,y2+1,1);
                update(x2+1,y1,1);
                update(x2+1,y2+1,1);
            }
            else
            {
                scanf("%d%d",&a,&b);
                getchar();
                printf("%d\n",get_sum(a,b)%2);
            }
        }
        printf("\n");
    }
    return 0;
}

* 矩阵的00在左上角

http://blog.csdn.net/zxy_snow/article/details/6264135   图示

poj 2155 区间更新 单点查询

时间: 2024-10-25 19:45:56

poj 2155 区间更新 单点查询的相关文章

poj 3321 区间更新单点求和??

Apple Tree Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the

codevs 1081 线段树练习 2 区间更新 单点查询 无lazy

题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]的所有数都增加X 2:询问第i个数是什么? 输入描述 Input Description 第一行一个正整数n,接下来n行n个整数,再接下来一个正整数Q,表示操作的个数. 接下来Q行每行若干个整数.如果第一个数是1,后接3个正整数a,b,X,表示在区间[a,b]内每个数增加X,如果是2,后面跟1个整数i, 表示询问第i个位置的数是多少. 输出描述 Output Description 对于每个询问输出一行一个答案 样例输

Light OJ 1080 - Binary Simulation - (线段树区间更新 单点查询)

Description Given a binary number, we are about to do some operations on the number. Two types of operations can be here. 'I i j'    which means invert the bit from i to j (inclusive) 'Q i'    answer whether the ith bit is 0 or 1 The MSB (most signif

ZOJ 1610 Count the Colors【题意+线段树区间更新&amp;&amp;单点查询】

任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent

POJ 2777 Count Color (线段树区间更新加查询)

Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly d

kuangbin带你飞----线段树专题一(基础操作,单点,区间更新和查询)

A 题意:给出q个询问,单点更新和查询 主要是注意模板的书写 #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<climits> #include<stack> #include<vector> #include<queue> #include<

【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状数组的单点查询:求某点a的值就是求数组中1~a的和. (i-a)%k==0把区间分隔开了,不能直接套用树状数组的区间修改单点查询 这道题的K很小,所以可以枚举k,对于每个k,建立k个树状数组,所以一共建立55棵树 所以就可以多建几棵树..然后就可以转换为成段更新了~~ [AC] 1 #include

Wikilo 1191线段树区间修改单点查询

这题也算比较容易的了. 如果哪个区间已经没有黑色的话,就不用update了,就是因为这个原因WA了2发,唉-- #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #incl

洛谷 P3368 【模板】树状数组 2 如题(区间修改+单点查询)

P3368 [模板]树状数组 2 时空限制1s / 128MB 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. 第二行包含N个用空格分隔的整数,其中第i个数字表示数列第i项的初始值. 接下来M行每行包含2或4个整数,表示一个操作,具体如下: 操作1: 格式:1 x y k 含义:将区间[x,y]内每个数加上k 操作2: 格式:2 x 含义:输出