zoukankan      html  css  js  c++  java
  • POJ2155 Matrix

      原题传送:http://poj.org/problem?id=2155

      楼教神题,二维树状数组。

      弄懂一维树状数组,二维的也不难懂。可以参考NOCOW

      需要注意的是,修改的四个点分别是(x1, y1), (x2+1, y1), (x1, y2+1), (x2+1, y2+1),而不是(x1, y1), (x2, y1), (x1, y2), (x2, y2),因为如果是后者的话,(x2, y2)会被4次覆盖(也就是点(x2, y2)不会被改变)。

    View Code
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define N 1001
     4 typedef long long LL;
     5 
     6 LL c[N][N];
     7 
     8 LL lowbit(LL x)
     9 {
    10     return x & (-x);
    11 }
    12 
    13 void modify(int x, int y)
    14 {
    15     for(int i = x; i < N; i += lowbit(i))
    16     for(int j = y; j < N; j += lowbit(j))
    17         c[i][j] ++;
    18     return ;
    19 }
    20 
    21 LL query(int x, int y)
    22 {
    23     LL ans = 0;
    24     for(int i = x; i > 0; i -= lowbit(i))
    25     for(int j = y; j > 0; j -= lowbit(j))
    26         ans += c[i][j];
    27     return ans;
    28 }
    29 
    30 int main()
    31 {
    32     int cas, n, tt, t, x1, y1, x2, y2;
    33     char op[3];
    34     scanf("%d", &tt);
    35     for(cas = 1; cas <= tt; cas ++)
    36     {
    37         if(cas != 1)    
    38             putchar('\n');
    39         memset(c, 0, sizeof c);
    40         scanf("%d%d", &n, &t);
    41         while(t --)
    42         {
    43             scanf("%s", op);
    44             if(op[0] == 'C')
    45             {
    46                 scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    47                 modify(x1, y1);
    48                 modify(x2 + 1, y1);
    49                 modify(x1, y2 + 1);
    50                 modify(x2 + 1, y2 + 1);
    51             }
    52             else if(op[0] == 'Q')
    53             {
    54                 scanf("%d%d", &x1, &y1);
    55                 printf("%I64d\n", query(x1, y1) & 1);
    56             }
    57         }
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    elementui组件库eldialog弹出框被遮罩层挡住
    python常规基础操作
    python中的字典排序
    python列表面试题
    python logging日志模块
    python序列之列表
    jmeter中csv连接数据库
    python必会的知识基础
    jmeter tcp 压力测试
    python模块基础知识练习
  • 原文地址:https://www.cnblogs.com/huangfeihome/p/2729502.html
Copyright © 2011-2022 走看看