●POJ 1195 Mobile phones

题链:

http://poj.org/problem?id=1195

题解:

二维树状数组

#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 1500
using namespace std;
struct BIT{
	int val[MAXN][MAXN],N;
	void Reset(int n){N=n; memset(val,0,sizeof(val));}
	int Lowbit(int x){return x&-x;}
	void Modify(int x,int y,int v){
		x++; y++;
		for(int i=x;i<=N;i+=Lowbit(i))
			for(int j=y;j<=N;j+=Lowbit(j))
				val[i][j]+=v;
	}
	int Sum(int x,int y){
		int ret=0;
		for(int i=x;i;i-=Lowbit(i))
			for(int j=y;j;j-=Lowbit(j))
				ret+=val[i][j];
		return ret;
	}
	int Area(int x1,int y1,int x2,int y2){
		x1++; y1++; x2++; y2++;
		return Sum(x2,y2)-Sum(x1-1,y2)-Sum(x2,y1-1)+Sum(x1-1,y1-1);
	}
}DT;
int main(){
	int cm,a,b,c,d;
	scanf("%d%d",&cm,&a);
	DT.Reset(a);
	while(~scanf("%d",&cm)&&cm!=3){
		if(cm==1) scanf("%d%d%d",&a,&b,&c),DT.Modify(a,b,c);
		else scanf("%d%d%d%d",&a,&b,&c,&d),printf("%d\n",DT.Area(a,b,c,d));
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/zj75211/p/8365567.html

时间: 2024-08-30 04:28:31

●POJ 1195 Mobile phones的相关文章

poj 1195:Mobile phones(二维树状数组,矩阵求和)

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

poj 1195:Mobile phones(二维线段树,矩阵求和,经典题)

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

(简单) POJ 1195 Mobile phones,二维树状数组。

Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contain

POJ 1195 Mobile phones(二维树状数组)

题目链接:POJ 1195 题意: 给出一个S*S的矩阵(行.列号从1开始),每个元素初始值为0,有两种操作:一种是第X行第Y列元素值加A:另一种是查询给定范围矩阵的所有元素之和(L<=X<=R,B<=Y<=T). 分析: 查询给定范围矩阵的所有元素之和是二维区间和,可以转换为二维前缀和求值.类比一维前缀和求法,二维区间和S(L, B, R, T) = S(1, 1, R, T) - S(1 ,1, L-1, T) - S(1, 1, R, B-1) + S(1, 1, L-1,

POJ 1195 Mobile phones (二维树状数组)

Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contain

POJ - 1195 - Mobile phones (二维线段树)

Mobile phones 题目传送:Mobile phones AC代码: /************************************************ > Auther : zzuspy > Mail : [email protected] > Created Time : 2015/8/7 15:24:00 ************************************************/ #include <map> #inclu

POJ 1195——Mobile phones(二维树状数组)

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 15013   Accepted: 6957 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

POJ 1195 Mobile phones

二维树状数组 #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(i

POJ 1195 Mobile phones【 二维树状数组 】

题意:基础的二维数组,注意 0 + lowbit(0)会陷入无限循环----- 之前做一道一维的一直tle,就是因为这个-------------------------- 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<ma