POJ1195 Mobile phones 二维线段树 区间求和

Mobile phones
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 9263 Accepted: 4154

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 contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. 

Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area. 

Input

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 

The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3. 

Table size: 1 * 1 <= S * S <= 1024 * 1024 
Cell value V at any time: 0 <= V <= 32767 
Update amount: -32768 <= A <= 32767 
No of instructions in input: 3 <= U <= 60002 
Maximum number of phones in the whole table: M= 2^30 

Output

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

0 4
1 1 2 3
2 0 0 2 2 
1 1 1 2
1 1 2 -1
2 1 1 2 3 
3

Sample Output

3
4
  该题就是一个普通的二维线段树,前面一直纠结于如何更新父亲节点的Y轴,其实就是在寻找的过程中,更新包含该区间的所有区间(一直更新到区间长度为 1 为止),由于是第一个区间求和的二维线段树,就没有写Lazy操作了。以后再写个优化版的。
  PS:网上大部分人都是二维树状数组写的,那个确实又快又好。
代码如下:
#include <cstdlib>
#include <cstdio>
#include <cstring>
#define LSON p << 1
#define RSON p << 1 | 1
#define MID( x, y ) (x) + (y) >> 1
#define TWO( x, y ) (x) + (y) << 1
using namespace std;

void getint( int &t )
{
	char c;
	while( c = getchar(), c < '0' || c > '9' );
	t = c - '0';
	while( c = getchar(), c >= '0' && c <= '9' )
		t = t * 10 + c - '0';
}

struct node
{
	int l, r, sum;
}n[2600][2600];

struct Node
{
	int l ,r;
}N[2600];

void y_build( node *rt, int p, int l, int r )
{
	rt[p].l = l, rt[p].r = r, rt[p].sum = 0;
	if( r - l == 1 ) return;
	int m = MID( l, r );
	y_build( rt, LSON, l, m );
	y_build( rt, RSON, m, r );
}

void build( int p, int l, int r, int l2, int r2 )
{
	N[p].l = l, N[p].r = r;
	y_build( n[p], 1, l2, r2 ); // 该步操作与上部极其相似了
	if( r - l == 1 ) return;
	int m = MID( l, r );
	build( LSON, l, m, l2, r2 );
	build( RSON, m, r, l2, r2 );
}

void y_modify( node *rt, int p, int add, int l, int r )
{
	rt[p].sum += add;
	if( rt[p].r - rt[p].l == 1 ) return;  // 更新到最下面,没有用到Lzay操作
	int m = MID( rt[p].l, rt[p].r );
	if( m >= r )
		y_modify( rt, LSON, add, l, r );
	else if( m <= l )
		y_modify( rt, RSON, add, l, r );
}

void modify( int p, int add, int l, int r, int l2, int r2 )
{
	y_modify( n[p], 1, add, l2, r2 );
	if( N[p].r - N[p].l == 1 ) return;  // 一维是无法Lzay的,因为Y轴是变化的
	int m = MID( N[p].l, N[p].r );
	if( m >= r )
		modify( LSON, add, l, r, l2, r2 );
	else if( m <= l )
		modify( RSON, add, l, r, l2, r2 );
}

int y_cal( node *rt, int p, int l, int r )
{
	if( rt[p].l == l && rt[p].r == r )
	{
		return rt[p].sum;
	}
	int m = MID( rt[p].l, rt[p].r );
	if( m >= r )
		return y_cal( rt, LSON, l, r );
	else if( m <= l )
		return y_cal( rt, RSON, l, r );
	else
	{
		return y_cal( rt, LSON, l, m ) + y_cal( rt, RSON, m, r );
	}
}

int cal( int p, int l, int r, int l2, int r2 )
{
	if( N[p].l == l && N[p].r == r )
	{
		return y_cal( n[p], 1, l2, r2 );
	}
	int m = MID( N[p].l, N[p].r );
	if( m >= r )
		return cal( LSON, l, r, l2, r2 );
	else if( m <= l )
		return cal( RSON, l, r, l2, r2 );
	else
	{
		return cal( LSON, l, m, l2, r2 ) + cal( RSON, m, r, l2, r2 );
	}
}

int main()
{
	int op;
	while( getint( op ), op != 3 )
	{
		int M, x1, y1, x2, y2, add;
		switch( op )
		{
			case 0 :
			{
				scanf( "%d", &M );
				build( 1, 1, M + 1, 1, M + 1 );
				break;
			}
			case 1 :
			{
				scanf( "%d %d %d", &x1, &y1, &add );
				modify( 1, add, x1 + 1, x1 + 2, y1 + 1, y1 + 2 );
				break;
			}
			case 2:
			{
				scanf( "%d %d %d %d", &x1, &y1, &x2, &y2 );
				printf( "%d\n", cal( 1, x1 + 1, x2 +  2, y1 + 1, y2 + 2 ) );
				break;
			}
			case 3:
				break;
		}
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/Lyush/p/2173706.html