码迷,mamicode.com
首页 > 编程语言 > 详细

POJ_1195 Mobile phones 【二维树状数组】

时间:2017-04-30 18:33:10      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:数组   one   一点   ble   target   二维树状数组   scan   turn   while   

题目链接:http://poj.org/problem?id=1195

纯纯的二维树状数组,不解释,仅仅须要注意一点,由于题目中的数组从0開始计算,所以维护的时候须要加1。由于树状数组的下标是不能为1的

代码:

#include <iostream>
#include <cstdio>
#define N 1030
using namespace std;
int c[N][N];
int cas,n,x,y,a,l,b,r,t;
int Lowbit(int x)
{
    return x & (-x);
}
void Updata(int x,int y,int a)
{
    int i,k;
    for(i=x; i<=n; i+=Lowbit(i))
        for(k=y; k<=n; k+=Lowbit(k))
           c[i][k]+=a;
}
int Getsum(int x,int y)
{
    int i,k,sum = 0;
    for(i=x; i>0; i-=Lowbit(i))
        for(k=y; k>0; k-=Lowbit(k))
            sum += c[i][k];
    return sum;
}
int main()
{
    scanf("%d%d",&cas,&n);
    while(~scanf("%d",&cas))
    {
        if(cas==1)
        {
            scanf("%d%d%d",&x,&y,&a);
            Updata(x+1,y+1,a);
        }
        if(cas==2)
        {
            scanf("%d%d%d%d",&l,&b,&r,&t);
            int a=Getsum(r+1,t+1)-Getsum(l,t+1)-Getsum(r+1,b)+Getsum(l,b);
            printf("%d\n",a);
        }
        if(cas==3)
            return 0;
    }
    return 0;
}

POJ_1195 Mobile phones 【二维树状数组】

标签:数组   one   一点   ble   target   二维树状数组   scan   turn   while   

原文地址:http://www.cnblogs.com/blfbuaa/p/6789940.html

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