pku 1195 Mobile phones 树状数组 解题报告

 

pku 1195 Mobile phones 解题报告

刚刚学完树状数组,pku1195来练习就a,第一道二维树状数组的应用,值得纪念一下.算法就是简单的二维树状数组的基本函数应用,详细见我另一篇文章树状数组小结。

题目链接: http://acm.pku.edu.cn/JudgeOnline/problem?id=1195

AC代码:

#include <stdio.h>

#include <string.h>

#define M 1030

 

int Tree[M][M];

int Operand, Size;

 

int Lowbit(int x)

{

       return x & -x;

}

 

void update(int i, int j, int num)

{

    while (i <= Size)

    {

        int temp = j;

        while (temp <= Size)

        {

            Tree[i][temp] += num;

            temp += Lowbit(temp);

        }

        i += Lowbit(i);

    }

}

 

int sum(int x, int y)

{

       int temp = 0;

 

       for (int i = x; i > 0; i -= Lowbit(i))

    {

              for (int j = y; j > 0; j -= Lowbit(j))

        {

                     temp += Tree[i][j];

              }

       }

    return temp;

}

 

int main()

{

       freopen("1.txt", "r", stdin);

 

       int i, j, x, y, Incremental, a, b, c, d, res;

       while (scanf("%d", &Operand))

       {

              if (Operand == 3)

              {

                     break;

              }

              else if (Operand == 0)

              {

                     scanf("%d", &Size);

                     for (i = 0; i <= Size; i++)

                     {

                            for (j = 0; j <= Size; j++)

                            {

                                   Tree[i][j] = 0;

                            }

                     }

              }

              else if (Operand == 1)

              {

                     scanf("%d%d%d", &x, &y, &Incremental);

                     update(x + 1, y + 1, Incremental);

              }

              else if (Operand == 2)

              {

                     scanf("%d%d%d%d", &a, &b, &c, &d);

                     a++; b++; c++; d++;

                     res = sum(c, d) - sum(c, b - 1) - sum(a - 1, d) + sum(a - 1, b - 1);

                     printf("%d/n", res);

              }

       }

 

       return 0;

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值