UVA11297 Census(二维线段树)

Description


This year, there have been many problems with population calculations, since in some cities, there are many emigrants, or the population growth is very high. Every year the ACM (for Association for Counting Members) conducts a census in each region. The country is divided into N^2 regions, consisting of an N x N grid of regions. Your task is to find the least, and the greatest population in some set of regions. Since in a single year there is no significant change in the populations, the ACM modifies the population counts by some number of inhabitants.

The Input


In the first line you will find N (0 <= N <= 500), in following the N lines you will be given N numbers, which represent, the initial population of city C [i, j]. In the following line is the number Q (Q <= 40000), followed by Q lines with queries: 

There are two possible queries: 

- "x1 y1 x2 y2" which represent the coordinates of the upper left and lower right of where you must calculate the maximum and minimum change in population. 

- "x y v" indicating a change of the population of city C [x, y] by value v.

The Output


For each query, "x1 y1 x2 y2" print in a single line the greatest and least amount of current population. Separated each output by a space. 

Notice: There is only a single test case.

Sample Input  


5 5
1 2 3 4 5
0 9 2 1 3
0 2 3 4 1
0 1 2 4 5
8 5 3 1 4
4
q 1 1 2 3
c 2 3 10
q 1 1 5 5
q 1 2 2 2 

 Sample Output


9 0
10 0
9 2

给你一个n*n的矩阵,有m个询问,q x1 y1 x2 y2 查询这个子矩阵的最大值和最小值,c x y v是将x,y点的值改成c。

二维线段树的基本操作。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
#define INF 0x3f3f3f3f
const int maxn=1500;
int n,_max[maxn][maxn],_min[maxn][maxn];
int flag,xo,maxans,minans;
void update2(int l,int r,int o,int k,int v)
{
	if(l==r)
	{
		if(flag)
		_max[xo][o]=v,_min[xo][o]=v;
		else
		{
			_max[xo][o]=max(_max[xo*2][o],_max[xo*2+1][o]);
			_min[xo][o]=min(_min[xo*2][o],_min[xo*2+1][o]);
		}
		return;
	}
	int ls=o*2,rs=o*2+1,m=(l+r)/2;
	if(k<=m)
	update2(l,m,ls,k,v);
	else
	update2(m+1,r,rs,k,v);
	_max[xo][o]=max(_max[xo][ls],_max[xo][rs]);
	_min[xo][o]=min(_min[xo][ls],_min[xo][rs]);
}
void update1(int l,int r,int o,int x,int y,int v)
{
	if(l==r)
	{
		flag=1,xo=o;
		update2(1,n,1,y,v);
		return;
	}
	int ls=o*2,rs=o*2+1,m=(l+r)/2;
	if(x<=m)
	update1(l,m,ls,x,y,v);
	else
	update1(m+1,r,rs,x,y,v);
	flag=0,xo=o;
	update2(1,n,1,y,v);
}
void query2(int l,int r,int pre,int o,int ql,int qr)
{
	if(l>=ql&&r<=qr)
	{
		maxans=max(maxans,_max[pre][o]);
		minans=min(minans,_min[pre][o]);
		return;
	}
	int ls=o*2,rs=o*2+1,m=(l+r)/2;
	if(ql<=m)
	query2(l,m,pre,ls,ql,qr);
	if(qr>m)
	query2(m+1,r,pre,rs,ql,qr);
}
void query1(int l,int r,int o,int ql,int qr,int x,int y)
{
	if(l>=ql&&r<=qr)
	{
		query2(1,n,o,1,x,y);
		return;
	}
	int ls=o*2,rs=o*2+1,m=(l+r)/2;
	if(ql<=m)
	query1(l,m,ls,ql,qr,x,y);
	if(qr>m)
	query1(m+1,r,rs,ql,qr,x,y);
}
int main()
{
	int x,y,x1,y1,m;
	char op[2];
	scanf("%d",&n);
	memset(_max,0,sizeof(_max));
	memset(_min,0,sizeof(_min));
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	{
		scanf("%d",&x);
		update1(1,n,1,i,j,x);
	}
	scanf("%d",&m);
	while(m--)
	{
		scanf("%s",op);
		if(op[0]=='q')
		{
			scanf("%d%d%d%d",&x,&x1,&y,&y1);
			maxans=0;
			minans=INF;
			query1(1,n,1,x,y,x1,y1);
			printf("%d %d\n",maxans,minans);
		}
		else
		{
			scanf("%d%d%d",&x,&y,&x1);
			update1(1,n,1,x,y,x1);
		}
	}
	return 0;
}

另外一种模板

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define maxn 1025
#define inf 0x3f3f3f3f
ll sum[maxn*4][maxn*4];
ll s;
int n;
void pushup(int rt,int x)
{
    sum[x][rt]=sum[x][rt*2]+sum[x][rt*2+1];
}
void buildy(int rt,int left,int right,int xrt)//y坐标
{
    if(left==right)
        sum[xrt][rt]=0;
    else
    {
        int mid=(left+right)/2;
        buildy(rt*2,left,mid,xrt);
        buildy(rt*2+1,mid+1,right,xrt);
        pushup(rt,xrt);
    }
}
void build(int rt,int left,int right)//确定x坐标
{
    buildy(1,1,n,rt);
    if(left!=right)
    {
        int mid=(left+right)/2;
        build(rt*2,left,mid);
        build(rt*2+1,mid+1,right);
    }
}
void updatey(int rt,int left,int right,int y,int xrt,ll add)
{
    if(left==right&&left==y)
    {
        sum[xrt][rt]+=add;
        return ;
    }
    int mid=(left+right)/2;
    if(y<=mid)
        updatey(rt*2,left,mid,y,xrt,add);
    if(y>mid)
        updatey(rt*2+1,mid+1,right,y,xrt,add);
    pushup(rt,xrt);
}
void update(int rt,int left,int right,int x,int y,ll add)
{
    updatey(1,1,n,y,rt,add);
    if(left!=right)
    {
        int mid=(left+right)/2;
        if(x<=mid)
            update(rt*2,left,mid,x,y,add);
        if(x>mid)
            update(rt*2+1,mid+1,right,x,y,add);
    }
}
void queryY(int rt,int left,int right,int y1,int y2,int xrt)
{
    if(left>=y1&&right<=y2)
    {
        s+=sum[xrt][rt];
        return ;
    }
    int mid=(left+right)/2;
    if(y1<=mid)
        queryY(rt*2,left,mid,y1,y2,xrt);
    if(y2>mid)
        queryY(rt*2+1,mid+1,right,y1,y2,xrt);
}
void query(int rt,int left,int right,int x1,int y1,int x2,int y2)
{
    if(left>=x1&&right<=x2)
    {
        queryY(1,1,n,y1,y2,rt);
        return ;
    }
    int mid=(left+right)/2;
    if(x1<=mid)
        query(rt*2,left,mid,x1,y1,x2,y2);
    if(x2>mid)
        query(rt*2+1,mid+1,right,x1,y1,x2,y2);

}
int main()
{
    int op;
    int x,y;
    ll w;
    int x1,y1,x2,y2;
    while(scanf("%d",&op))
    {
        if(op==0)
        {

            scanf("%d",&n);
            n++;
            build(1,1,n);
        }
        else if(op==1)
        {
            scanf("%d%d%lld",&x,&y,&w);
            x++,y++;
            update(1,1,n,x,y,w);
        }
        else if(op==2)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            s=0;
            x1++,y1++,x2++,y2++;
            query(1,1,n,x1,y1,x2,y2);
            printf("%lld\n",s);
        }
        else
            break;
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值