UVA:11297-Census(二维线段树)

Census

Time Limit: 8 sec

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.

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.

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


解题心得:

  1. 题意很简单,就是给你一个矩阵,多次询问,每次询问一个子矩阵,输出子矩阵里面的最大值和最小值,也可以改变矩阵中某个点的值。
  2. 一看就是一个线段树,不过是一个矩阵,但是也很简单啊,把线段树的每一行拆出来建一个树,询问的时候就一个树一个树的找,时间给你8s,其实300ms就过了。

/*其实代码差不多就是一个线段树的模板*/
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 510;
const int INF = 0x3f3f3f3f;
struct NODE
{
    int Max,Min;
}node[maxn][maxn<<2];//第一维表示是用矩阵的第几行建立的线段树
int n,m,ans_Max,ans_Min;

void updata(int c,int root)
{
    node[c][root].Max = max(node[c][root<<1|1].Max,node[c][root<<1].Max);
    node[c][root].Min = min(node[c][root<<1|1].Min,node[c][root<<1].Min);
}

void build_tree(int c,int root,int l,int r)
{
    if(l == r)
    {
        int temp;
        scanf("%d",&temp);
        node[c][root].Max = node[c][root].Min = temp;
        return ;
    }
    int mid = (l+r)>>1;
    build_tree(c,root<<1,l,mid);
    build_tree(c,root<<1|1,mid+1,r);
    updata(c,root);
}

void init()
{
    for(int i=1;i<=n;i++)
        build_tree(i,1,1,n);
}

void get_ans(int c,int root,int l,int r,int L,int R)
{
    if(l == L && r == R)
    {
        ans_Max = max(ans_Max,node[c][root].Max);
        ans_Min = min(ans_Min,node[c][root].Min);
        return ;
    }
    int mid = (L+R)>>1;
    if(mid >= r)
        get_ans(c,root<<1,l,r,L,mid);
    else if(mid < l)
        get_ans(c,root<<1|1,l,r,mid+1,R);
    else
    {
        get_ans(c,root<<1,l,mid,L,mid);
        get_ans(c,root<<1|1,mid+1,r,mid+1,R);
    }
}

void change(int c,int va,int root,int pos,int l,int r)
{
    if(l == r && l == pos)
    {
        node[c][root].Max = va;
        node[c][root].Min = va;
        return ;
    }
    int mid = (l+r)>>1;
    if(mid >= pos)
        change(c,va,root<<1,pos,l,mid);
    else if(mid < pos)
        change(c,va,root<<1|1,pos,mid+1,r);
    updata(c,root);
}

void query()
{
    int m;
    scanf("%d",&m);
    while(m--)
    {
        char s[10];
        scanf("%s",s);
        if(s[0] == 'q')
        {
            ans_Max = -INF;
            ans_Min = INF;
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            for(int i=x1;i<=x2;i++)
                get_ans(i,1,y1,y2,1,n);
            printf("%d %d\n",ans_Max,ans_Min);
        }
        if(s[0] == 'c')
        {
            int x,y,va;
            scanf("%d%d%d",&x,&y,&va);
            change(x,va,1,y,1,n);
        }
    }
}

int main()
{
    while(scanf("%d",&n) != EOF)
    {
        init();
        query();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值