• spoj 1029 Matrix Summation


    题意:

    对一个矩阵有2种操作:

    1.把某个元素设为x。

    2.查询以(x1,y1)为左上角 以(x2,y2)为右上角的矩阵中的数字的和。

    思路:

    二维树状数组入门题,同时对横坐标和纵坐标做前缀和就行了。

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 const int N = 1030;
     6 int a[N][N];
     7 int c[N][N];
     8 int n;
     9 int lowbit(int x)
    10 {
    11     return x&(-x);
    12 }
    13 void add(int x,int y,int ch)
    14 {
    15     for (int i = x;i <= n;i += lowbit(i))
    16     {
    17         for (int j = y;j <= n;j += lowbit(j))
    18         {
    19             c[i][j] += ch;
    20         }
    21     }
    22 }
    23 int getsum(int x,int y)
    24 {
    25     int ans = 0;
    26     for (int i = x;i > 0;i -= lowbit(i))
    27     {
    28         for (int j = y;j > 0;j -= lowbit(j))
    29         {
    30             ans += c[i][j];
    31         }
    32     }
    33     return ans;
    34 }
    35 int main()
    36 {
    37     int T;
    38     scanf("%d",&T);
    39     while (T--)
    40     {
    41         memset(c,0,sizeof(c));
    42         memset(a,0,sizeof(a));
    43         scanf("%d",&n);
    44         char s[5];
    45         while (scanf("%s",s) != EOF)
    46         {
    47             if (s[1] == 'N') break;
    48             if (s[1] == 'E')
    49             {
    50                 int x,y,num;
    51                 scanf("%d%d%d",&x,&y,&num);
    52                 x++,y++;
    53                 int ch = num - a[x][y];
    54                 a[x][y] = num;
    55                 add(x,y,ch);
    56             }
    57             if (s[1] == 'U')
    58             {
    59                 int x,y,p,q;
    60                 scanf("%d%d%d%d",&x,&y,&p,&q);
    61                 x++,y++,p++,q++;
    62                 int ans = 0;
    63                 ans += getsum(p,q);
    64                 ans -= getsum(x-1,q);
    65                 ans -= getsum(p,y-1);
    66                 ans += getsum(x-1,y-1);
    67                 printf("%d
    ",ans);
    68             }
    69         }
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    Directory文档目录操作
    文件序列化和反序列化
    COOKIE
    转载深入ASP.NET MVC之二:路由模块如何工作
    Running 32Bit RDP on 64 Bit Windows OS
    MS CRM Isv Web.config设置
    QualifyLead PlugIn(转)
    MS CRM帮助访问不正确,
    (转)CrmTrace encountered an error. Additional Info:Error in GetFileName MS CRM
    MS CRM 2011储存电子邮件凭证
  • 原文地址:https://www.cnblogs.com/kickit/p/9073721.html
Copyright © 2020-2023  润新知