集训第二十四天(2017/8/23):二维树状数组&三维树状数组

      今上午解决了昨天遗留的问题——一道关于区间修改,区间查询的树状数组的题目,专门写了一篇结题报告。

     今下午主要做了三维树状数组和二维树状数组的题目,感觉还是有模板的,做这类的题目还是有规律可循的。

       要解决POJ2155Matrix(二维树状数组)和hdu 3584 Cube(三维树状数组)这样的题目,这两道题目很相似,只不过从

二维升到三维,其实思路是一样的。

      首先看 POJ2155Matrix(二维树状数组):

      

Problem 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. <br> <br>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. <br>
 

Output
For each querying output one line, which has an integer representing A[x, y]. <br> <br>There is a blank line between every two continuous test cases. <br>
 

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
   AC代码:
   #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 1005
int c[maxn][maxn];
int X,n,t;
int lowbit(int x)
{
    return x&(-x);
}
int sum(int x,int 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;
}
void add(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]+=1;
}
int main()
{   char str;
    int x1,y1,x2,y2;
    scanf("%d",&X);
    while(X--)
    {   memset(c,0,sizeof(c));
        scanf("%d%d",&n,&t);
        while(t--)
        {   getchar();
            scanf("%s",&str);
            if(str=='C')
            {
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                add(x1,y1);
                add(x2+1,y1);
                add(x1,y2+1);
                add(x2+1,y2+1);
            }
            else
            {
                scanf("%d%d",&x1,&y1);
                printf("%d\n",sum(x1,y1)%2);
            }
        }
        if(X) printf("\n");
    }
    return 0;
}

    然后是 hdu 3584 Cube(三维树状数组):
   
Problem Description
Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the number in the i-th row , j-th column and k-th layer. Initially we have A[i, j, k] = 0 (1 <= i, j, k <= N). 
We define two operations, 1: “Not” operation that we change the A[i, j, k]=!A[i, j, k]. that means we change A[i, j, k] from 0->1,or 1->0. (x1<=i<=x2,y1<=j<=y2,z1<=k<=z2).
0: “Query” operation we want to get the value of A[i, j, k].
 

Input
Multi-cases. First line contains N and M, M lines follow indicating the operation below. Each operation contains an X, the type of operation. 1: “Not” operation and 0: “Query” operation. If X is 1, following x1, y1, z1, x2, y2, z2. If X is 0, following x, y, z.
 

Output
For each query output A[x, y, z] in one line. (1<=n<=100 sum of m <=10000)
 

Sample Input
   
   
2 5 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 2 2 2 0 1 1 1 0 2 2 2
 

Sample Output
   
   
1 0 1
 
   /*这题为啥用树状数组呢?


首先1<=N<=100,如果暴力修改的话,最糟糕的时间为O(N*N*N),然后有M个命令,1<=M<=10000,总耗时为O(M*N*N*N),也就是100*100*100*10000,而限时1s,神仙难救~


如果把修改次数看成前缀和,修改次数为奇数,答案则为1,修改次数为偶数,则为0,总的耗时为Q(M*logN*logN*logN),随意可过~
*/


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 105
int c[N][N][N],n,m;
int lowbit(int x)
{
    return x&(-x);
}
int sum(int x,int y,int z)
{
    int res=0;
    for(int i=x;i>0;i-=lowbit(i))
      for(int j=y;j>0;j-=lowbit(j))
        for(int k=z;k>0;k-=lowbit(k))
           res+=c[i][j][k];
    return res;
}
void add(int x,int y,int z,int d)
{
    for(int i=x;i<=n;i+=lowbit(i))
      for(int j=y;j<=n;j+=lowbit(j))
        for(int k=z;k<=n;k+=lowbit(k))
            c[i][j][k]+=d;
}
int main()
{   int q,x1,y1,z1,x2,y2,z2;
    while(~scanf("%d%d",&n,&m))
    {
        memset(c,0,sizeof(c));
        while(m--)
        {
            scanf("%d",&q);
            if(q==1)
            {
                scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
                add(x1,y1,z1,1);
                add(x2+1,y1,z1,1);
                add(x1,y2+1,z1,1);
                add(x1,y1,z2+1,1);
                add(x2+1,y2+1,z1,1);
                add(x2+1,y1,z2+1,1);
                add(x1,y2+1,z2+1,1);
                add(x2+1,y2+1,z2+1,1);
            }
            else
            {
                scanf("%d%d%d",&x1,&y1,&z1);
                printf("%d\n",sum(x1,y1,z1)%2);
            }
        }
    }
    return 0;
}
   要理解这类多维树状数组0,1二进制计数问题,首先要理解一维树状数组,有兴趣的童鞋请看 https://wenku.baidu.com/view/1e51750abb68a98271fefaa8.html  ,写得很详细。
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值