HDU 1754 I Hate It (线段树 单点替换, 区间最值)

题目大意:

中文题面

就是可以每次替换一个数, 或者查询区间最大值


大致思路:

线段树专题速度刷....

没什么好说的....练练手..


代码如下:

Result  :  Accepted     Memory  :  3680 KB     Time  :  1092 ms

/*
 * Author: Gatevin
 * Created Time:  2015/8/14 21:43:48
 * File Name: Sakura_Chiyo.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 200020

struct Segment_Tree
{
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    int val[maxn << 2];
    void pushUp(int rt)
    {
        val[rt] = max(val[rt << 1], val[rt << 1 | 1]);
        return;
    }
    void build(int l, int r, int rt)
    {
        if(l == r)
        {
            scanf("%d", &val[rt]);
            return;
        }
        int mid = (l + r) >> 1;
        build(lson);
        build(rson);
        pushUp(rt);
        return;
    }
    void update(int l, int r, int rt, int pos, int value)
    {
        if(l == r)
        {
            val[rt] = value;
            return;
        }
        int mid = (l + r) >> 1;
        if(mid >= pos) update(lson, pos, value);
        else update(rson, pos, value);
        pushUp(rt);
        return;
    }
    int query(int l, int r, int rt, int L, int R)
    {
        if(l >= L && r <= R)
            return val[rt];
        int mid = (l + r) >> 1;
        int ret = -1;
        if(mid >= L) ret = max(ret, query(lson, L, R));
        if(mid + 1 <= R) ret = max(ret, query(rson, L, R));
        return ret;
    }
};

Segment_Tree ST;

int main()
{
    int n, m;
    while(~scanf("%d %d", &n, &m))
    {
        ST.build(1, n, 1);
        while(m--)
        {
            char op[4];
            int l, r;
            scanf("%s", op);
            switch(op[0])
            {
                case 'U': scanf("%d %d", &l, &r);
                          ST.update(1, n, 1, l, r);
                          break;
                case 'Q': scanf("%d %d", &l, &r);
                          printf("%d\n", ST.query(1, n, 1, l, r));
                          break;
            }
        }
    }
    return 0;
}


你可能感兴趣的:(线段树,it,1754,HDU,I,hate,区间最值,单点替换)