码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1195 Mobile phones

时间:2016-04-30 23:48:15      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

  二维树状数组

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
int n;
int c[1030][1030];
int lowbit(int k)
{
    return k&(-k);
}
void add(int x,int y,int num)
{
    for(int i = x;i <= n;i += lowbit(i))
    for(int j = y;j <= n;j += lowbit(j))
    c[i][j] += num;
}
void sub(int x,int y,int num)
{
    for(int i = x;i >= 0;i -= lowbit(i))
    for(int j = y;j >= 0;j -= lowbit(j))
    c[i][j] -= num;
}
int cal(int x,int y)
{
    int sum = 0;
    for(int i = x;i > 0;i -= lowbit(i))
    for(int j = y;j > 0;j -= lowbit(j))
     sum += c[i][j];
     return sum;
}
int main()
{
    int first,op,size;
    scanf("%d%d",&first,&n);
    {
        size = n;
        memset(c,0,sizeof(c));
        for(int i = 1;i <= size;i++)
        for(int j = 1;j <= size;j++)
        add(i,j,first);
        int op;
        while(scanf("%d",&op) != EOF)
        {
            if(op == 1)
            {
                int x,y,a;
                scanf("%d%d%d",&x,&y,&a);
                x++,y++;
                add(x,y,a);
            }
            else if(op == 2)
            {
                int x1,y1,x2,y2;
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                x1++,y1++,x2++,y2++;
                int sum = cal(x2,y2) + cal(x1-1,y1-1) - cal(x2,y1-1) - cal(x1-1,y2);
                printf("%d\n",sum);
            }
            else break;
        }

    }
}

 

POJ 1195 Mobile phones

标签:

原文地址:http://www.cnblogs.com/jifahu/p/5449578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!