HDU 1754 I Hate It

线段树,单点更新,求区间最值

/* ***********************************************
Author        :Zhou Zhentao
Email         :774388357@qq.com
Created Time  :2015/11/20 17:21:35
File Name     :acm.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
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]);  
}  
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);  
            //求[a,b]区间最大值
            if (op[0] == 'Q') printf("%d\n",query(a , b , 1 , n , 1));  
            //下标为a的改为b
            else update(a , b , 1 , n , 1);  
        }  
    }  
    return 0;  
}  

 

posted @ 2015-11-21 08:52  Fighting_Heart  阅读(170)  评论(0编辑  收藏  举报