zoukankan      html  css  js  c++  java
  • HDU 1754 I Hate It (段树 & 树阵)

    I Hate It
    Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 39959 Accepted Submission(s): 15863


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754


    Problem Description
    非常多学校流行一种比較的习惯。老师们非常喜欢询问,从某某到某某其中。分数最高的是多少。
    这让非常多学生非常反感。


    无论你喜不喜欢。如今须要你做的是,就是依照老师的要求,写一个程序。模拟老师的询问。当然。
    老师有时候须要更新某位同学的成绩。
     
    Input
    本题目包括多组測试。请处理到文件结束。
    在每一个測试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操
    作的数目。
    学生ID编号分别从1编到N。
    第二行包括N个整数。代表这N个学生的初始成绩,当中第i个数代表ID为i的学生的成绩。
    接下来有M行。

    每一行有一个字符 C (仅仅取'Q'或'U') ,和两个正整数A,B。
    当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包含A,B)的学生其中。成绩最高的是多
    少。
    当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。


     
    Output
    对于每一次询问操作。在一行里面输出最高成绩。
     
    Sample Input
    5 6
    1 2 3 4 5
    Q 1 5
    U 3 6
    Q 3 4
    Q 4 5
    U 2 9
    Q 1 5
     
    Sample Output
    5
    6
    5
    92014年10月6日 Problem - 1754
    http://acm.hdu.edu.cn/showproblem.php?

    pid=1754 2/2
    Hint
    Huge input,the C function scanf() will work better than cin
     
     
    Author
    linle
     
    Source
    2007省赛集训队练习赛(6)_linle专场





    题意:单点改动, 求区间最值。



    解题思路:非常明显。这题树状数组和线段树都能做。这里先用线段树搞吧。树状数组的也不难,仅仅要把update和sum改一下即可了,就不给详细代码了。这题也是主要的线段树的应用。仅仅要把单点改动。区间求和的update和query略微改一下即可了。

    详见代码


    线段树基础知识详见:线段树之入门篇



    AC代码:

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    #define lson l , m , rt << 1
    #define rson m + 1 , r , rt << 1 | 1
    const int maxn = 222222;
    int MAX[maxn<<2];
    void PushUP(int rt) {                    //这个也变成了两者的最大值,而不是求和了
        MAX[rt] = max(MAX[rt<<1] , MAX[rt<<1|1]);         // rt<<1|1 是 rt*2+1 的意思
    }
    void build(int l,int r,int rt) {
        if (l == r) {
            scanf("%d",&MAX[rt]);
            return ;
        }
        int m = (l + r) >> 1;
        build(lson);
        build(rson);
        PushUP(rt);
    }
    void update(int p,int sc,int l,int r,int rt) {
        if (l == r) {
            MAX[rt] = sc;                        
            return ;
        }
        int m = (l + r) >> 1;
        if (p <= m) update(p , sc , lson);
        else update(p , sc , rson);
        PushUP(rt);
    }
    int query(int L,int R,int l,int r,int rt) {
        if (L <= l && r <= R) {
            return MAX[rt];
        }
        int m = (l + r) >> 1;
        int ret = 0;
        if (L <= m) ret = max(ret , query(L , R , lson));             //这里变成了求两者的最大值
        if (R > m) ret = max(ret , query(L , R , rson));
        return ret;
    }
    int main() {
        int n , m;
        while (~scanf("%d%d",&n,&m)) {
            build(1 , n , 1);
            while (m --) {
                char op[2];
                int a , b;
                scanf("%s%d%d",op,&a,&b);
                if (op[0] == 'Q') printf("%d
    ",query(a , b , 1 , n , 1));
                else update(a , b , 1 , n , 1);
            }
        }
        return 0;
    }



    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    C# 图片与Base64的相互转化
    LeetCode 303. Range Sum Query – Immutable
    LeetCode 300. Longest Increasing Subsequence
    LeetCode 292. Nim Game
    LeetCode 283. Move Zeroes
    LeetCode 279. Perfect Squares
    LeetCode 268. Missing Number
    LeetCode 264. Ugly Number II
    LeetCode 258. Add Digits
    LeetCode 257. Binary Tree Paths
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4618313.html
Copyright © 2011-2022 走看看