【二维树状数组--模板】poj 2155 Matrix、poj 1195 Mobile phones

来源:互联网 发布:淘宝无忧退货流程 编辑:程序博客网 时间:2024/06/01 14:31

一维树状数组很容易扩展到二维,但本王不会

这两个题就是个模板题,参考文章:点击打开链接



Matrix
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 28177 Accepted: 10285

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

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

Sample Output

1001

题意:給一个N*N的矩阵,有两种操作,一种是查询(0~x,0~y)的区间和,另一种是改变摸个区间(0~x,0~y)的值;

本题类型:区间更新,区间求和;

代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=1005;int C[N][N];int n;int lowbit(int x){    return x&(-x);}void add(int x,int y,int d)              //修改{    for(int i=x;i<=n;i+=lowbit(i))    for(int j=y;j<=n;j+=lowbit(j))        C[i][j]+=d;}int query(int x,int y)                   //查询(0~x,0~y)的区间和;{    int res=0;    for(int i=x;i>0;i-=lowbit(i))    for(int j=y;j>0;j-=lowbit(j))        res+=C[i][j];    return res;}int sum(int x,int y,int xx,int yy)       //返回(x~xx,y~yy)的区间和;{    x--,y--;    return query(xx,yy)-query(xx,y)-query(x,yy)+query(x,y);}int main(){    int T,t;    scanf("%d",&T);    while(T--)    {        memset(C,0,sizeof(C));        scanf("%d%d",&n,&t);        while(t--)        {            char s[10];            scanf("%s",s);            if(s[0]=='C')            {                int x1,y1,x2,y2;                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);                add(x1,y1,1);                           //给出左上,右下的坐标,修改这四个点的值;                add(x1,y2+1,1);                add(x2+1,y1,1);                add(x2+1,y2+1,1);            }            else if(s[0]=='Q')            {                int x,y;                scanf("%d%d",&x,&y);//                printf("%d\n",query(x,y)&1);                printf("%d\n",sum(1,1,x,y)&1);            }        }        puts("");    }    return 0;}



Mobile phones
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 19907 Accepted: 9205

Description

Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. 
Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area. 

Input

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 
The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3. 
Table size: 1 * 1 <= S * S <= 1024 * 1024 
Cell value V at any time: 0 <= V <= 32767 
Update amount: -32768 <= A <= 32767 
No of instructions in input: 3 <= U <= 60002 
Maximum number of phones in the whole table: M= 2^30 

Output

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

0 41 1 2 32 0 0 2 2 1 1 1 21 1 2 -12 1 1 2 3 3

Sample Output

34

Source

题意:注意坑点:坐标是从0开始的;

给出指示0,输入S表示给定一个S*S的矩阵、

给出指示1,输入X,Y,A表示(X,Y)位置增加了A个、

给出指示2,输入L,B,R,T表示求(x:L~R  y:B~T)区间和、

给出指示3,表示程序终止;

本题类型:单点更新,区间求和;

代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int maxn=1100;ll C[maxn][maxn];int N;ll lowbit(ll x){    return x&(-x);}void add(ll x,ll y,ll v){    for(int i=x;i<=N;i+=lowbit(i))    for(int j=y;j<=N;j+=lowbit(j))        C[i][j]+=v;}ll query(ll x,ll y){    ll 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 sum(ll x,ll y,ll xx,ll yy){    x--,y--;    return query(xx,yy)-query(xx,y)-query(x,yy)+query(x,y);}int main(){    int a;    memset(C,0,sizeof(C));    while(~scanf("%d",&a))    {        if(a==0)        {            scanf("%d",&N);        }        else if(a==1)        {            ll x,y,v;            scanf("%lld%lld%lld",&x,&y,&v);            x+=1,y+=1;            add(x,y,v);        }        else if(a==2)        {            ll x1,y1,x2,y2;            scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);            x1+=1,y1+=1,x2+=1,y2+=1;            ll ans=sum(x1,y1,x2,y2);            if(ans>=0)                printf("%lld\n",ans);            else                printf("0\n");        }        else if(a==3)            break;    }    return 0;}





原创粉丝点击