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

POJ 1195 Mobile phones

时间:2015-01-20 20:21:18      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

一. 题意:

  裸二维树状数组,求(L, B) (R, T) 形成的矩形内所有点的和。

  ans = getSum(R + 1, T + 1) - getSum(R + 1, B) - getSum(L, T + 1) + getSum(L, B)

  

#include <cstdio>
#include <cstring>

#define lowbit(x) x&(-x)
#define MAXN 1030

int c[MAXN][MAXN];
int n;

void update(int x, int y, int val)
{
	for (int i = x; i <= n; i += lowbit(i)) {
		for (int j = y; j <= n; j += lowbit(j)) {
			c[i][j] += val;
		}
	}
}

int getSum(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 cmd, x, y, val;
	int x1, y1, x2, y2;
	scanf("%d%d", &cmd, &n);
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= n; j++) {
			c[i][j] = 0;
		}
	}
	while (scanf("%d", &cmd) && cmd < 3) {
		if (cmd == 1) {
			scanf("%d %d %d", &x, &y, &val);
			update(x + 1, y + 1, val);
		} else if (cmd == 2) {
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			int ans = getSum(x2 + 1, y2 + 1) - getSum(x2 + 1, y1) - getSum(x1, y2 + 1) + getSum(x1, y1);
			printf("%d\n", ans);
		}
	}
	return 0;
}

  

POJ 1195 Mobile phones

标签:

原文地址:http://www.cnblogs.com/subrshk/p/4236938.html

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