• 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
    ",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;
    }
  • 相关阅读:
    包装器
    高级new创建
    野性的呼唤 第三章
    SourceTree的基本使用
    SAP的春天回来么?
    dirname命令和basename命令
    一个简单的ETL脚本的内容
    轮子:读取config.ini文件
    sed基础语法
    hivesql之str_to_map函数
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107192.html
Copyright © 2020-2023  润新知