首页 > 其他 > 详细

poj1195 mobile phones 【二维树状数组】

时间:2014-05-21 16:23:17      阅读:366      评论:0      收藏:0      [点我收藏+]

一次AC

二维树状数组,有模版很好办

注意二维树状数组这个下标是[1][1]的

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int Max = 1030;
int row, col, ar[Max][Max];
//  二维的其实下标为[1][1],这个要记得。
int lowbit(int x)
{
    return x & (-x);
}

void add(int i, int j, int w)
{
    int tmpj;
    while(i <= row)
	{
        tmpj = j;
        while(tmpj <= col)
		{
            ar[i][tmpj] += w;
            tmpj += lowbit(tmpj);
        }
        i += lowbit(i);
    }
}

int sum(int i, int j)
{
    int tmpj, ans = 0;
    while(i > 0)
	{
        tmpj = j;
        while(tmpj > 0)
		{
            ans += ar[i][tmpj];
            tmpj -= lowbit(tmpj);
        }
        i -= lowbit(i);
    }
    return ans;
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("G:/1.txt","r",stdin);
		freopen("G:/2.txt","w",stdout);
	#endif
	int tmp,n;
	scanf("%d%d",&tmp,&n);
	row=col=n;
	while(true)
	{
		int kind,x,y,xx,yy,v;
		scanf("%d",&kind);
		if(kind==1)
		{
			scanf("%d%d%d",&x,&y,&v);
			add(x+1,y+1,v);
		}
		else if(kind==2)
		{
			scanf("%d%d%d%d",&x,&y,&xx,&yy);
			x++;y++;xx++;yy++;
			printf("%d\n",sum(xx,yy)-sum(xx,y-1)-sum(x-1,yy)+sum(x-1,y-1));
		}
		else
		{
			break;
		}
	}
	return 0;
}


 

poj1195 mobile phones 【二维树状数组】,布布扣,bubuko.com

poj1195 mobile phones 【二维树状数组】

原文:http://blog.csdn.net/u011775691/article/details/26357107

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!