zoukankan      html  css  js  c++  java
  • poj 2155 Matrix(二维树状数组)

    链接:http://poj.org/problem?id=2155

    题意:给出一个n*n的矩阵,初始化为0,给出q个操作,C x1 y1 x2 y2表示将(x1,y1)至(x2,y2)的矩阵的数翻转(0变成1,1变成0),Q x y表示求(x,y)位置的点的值。

    分析:这是hdu1556的二维版本。把(1,1)到四个角的矩阵内的数全翻转1次即update(),然后sum(x,y)即可。

    代码如下:

     1 #include<cstdio>
     2 #include<cstring>
     3 const int N=1010;
     4 int c[N][N],n;
     5 int lowbit(int x)
     6 {
     7     return x&(-x);
     8 }
     9 void update(int x,int y,int num)
    10 {
    11     int i,j;
    12     for(i=x;i<=n;i+=lowbit(i))
    13         for(j=y;j<=n;j+=lowbit(j))
    14             c[i][j]^=num;
    15 }
    16 int sum(int x,int y)
    17 {
    18     int i,j,s=0;
    19     for(i=x;i>0;i-=lowbit(i))
    20         for(j=y;j>0;j-=lowbit(j))
    21             s+=c[i][j];
    22     return s%2;
    23 }
    24 int main()
    25 {
    26     int t,q,x1,x2,y1,y2,x,y;
    27     char s[10];
    28     scanf("%d",&t);
    29     while(t--)
    30     {
    31         scanf("%d%d",&n,&q);
    32         memset(c,0,sizeof(c));
    33         while(q--)
    34         {
    35             scanf("%s",s);
    36             if(s[0]=='C')
    37             {
    38                 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    39                 update(x1,y1,1);
    40                 update(x2+1,y1,1);
    41                 update(x1,y2+1,1);
    42                 update(x2+1,y2+1,1);
    43             //    update(x1-1,y1-1,1);      //超时陷进,树状数组的下标不能为0,所以+1变成上面的代码
    44             //    update(x2,y1-1,1);
    45             //    update(x1-1,y2,1);
    46             //    update(x2,y2,1);
    47             }
    48             else
    49             {
    50                 scanf("%d%d",&x,&y);
    51                 printf("%d
    ",sum(x,y));
    52             }
    53         }
    54         printf("
    ");
    55     }
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    window.open()弹出窗口防止被禁
    实用Javascript代码片段
    php字符串常用函数
    PHP基本使用
    php开发环境搭建
    what is php?
    Front-end Developer Interview Questions
    jQuery Questions:Front-end Developer Interview Questions
    JS Questions:Front-end Developer Interview Questions
    HTML Questions:Front-end Developer Interview Questions
  • 原文地址:https://www.cnblogs.com/frog112111/p/3265541.html
Copyright © 2011-2022 走看看