HDU 1754 I Hate It(线段树-水题,模板题)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f
#define maxn 200000 + 50
#define juzheng 300

#define ll long long

using namespace std;

//Date:2017-9-4
//Author:HarryBlackCat

struct xx{
    int left;
    int right;
    int val;
}tree[maxn + maxn + maxn + maxn];

int arr[maxn],n,ans,m;

void bulid_tree(int node,int left,int right){
    tree[node].left = left;
    tree[node].right = right;

    if(left == right)
        tree[node].val = arr[left];
    else{
        int mid = (left + right) / 2;
        bulid_tree(node * 2,left,mid);
        bulid_tree(node * 2 + 1,mid + 1,right);

        tree[node].val = max(tree[node * 2].val,tree[node * 2 + 1].val);
    }
}

void add(int node,int index,int val){
    tree[node].val = max(tree[node].val,val);

    if(tree[node].right == tree[node].left)
        return;

    int mid = (tree[node].left + tree[node].right) / 2;
    if(index > mid)
        add(node * 2 + 1,index,val);
    else
        add(node * 2,index,val);
}

void get_max(int node,int left,int right){
    if(tree[node].left >= left && tree[node].right <= right)
        ans = max(ans,tree[node].val);
    else{
        int mid = (tree[node].left + tree[node].right) / 2;
        if(left > mid)
            get_max(node * 2 + 1,left,right);
        else if(right <= mid)
            get_max(node * 2,left,right);
        else{
            get_max(node * 2,left,right);
            get_max(node * 2 + 1,left,right);
        } 
    }
}

int main() {
    char c;
    int a,b;
    while(~scanf("%d %d",&n,&m)) {
        for(int i = 1; i <= n; i++)
            scanf("%d",&arr[i]);

        bulid_tree(1,1,n);

        while(m--) {
            cin >> c;
            scanf("%d %d",&a,&b);
            if(c == 'Q'){
                ans = -INF;
                get_max(1,a,b);
                printf("%d\n",ans);
            }
            else
                add(1,a,b);
        }
    }
    return 0;
}

你可能感兴趣的:(HDU 1754 I Hate It(线段树-水题,模板题))