且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

hdu 1754 I Hate It

更新时间:2022-08-12 16:17:42

点击打开链接hdu 1754

思路: 线段树+单点更新

分析:

1 线段树的水题


代码:

/************************************************
 * By: chenguolin                               * 
 * Date: 2013-09-01                             *
 * Address: http://blog.csdn.net/chenguolinblog *
 ************************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

#define Lson(x) (x<<1)
#define Rson(x) (Lson(x)|1)
#define Mid(x,y) ((x+y)>>1)
#define Sum(x,y) (x+y)

const int MAXN = 200010;

int n , m;
int num[MAXN];
struct Node{
    int left;
    int right;
    int maxScore; 
};
Node node[4*MAXN];

void push_up(int pos){
    node[pos].maxScore = max(node[Lson(pos)].maxScore , node[Rson(pos)].maxScore);
}

void init(int left , int right , int pos){
    node[pos].left = left;
    node[pos].right = right;
    if(node[pos].left == node[pos].right){
        node[pos].maxScore = num[left];
        return;
    }
    int mid = Mid(left , right);
    init(left , mid , Lson(pos));
    init(mid+1 , right , Rson(pos));
    push_up(pos); 
}

void update(int index , int val , int pos){
    if(node[pos].left == node[pos].right){
        node[pos].maxScore = val;
        return;
    }
    int mid = Mid(node[pos].left , node[pos].right);
    if(index <= mid)
        update(index , val , Lson(pos));
    else
        update(index , val , Rson(pos));
    push_up(pos); 
}

int query(int left , int right , int pos){
    if(node[pos].left == left && node[pos].right == right) 
        return node[pos].maxScore;
    int mid = Mid(node[pos].left , node[pos].right);
    if(right <= mid)
        return query(left , right , Lson(pos));
    else if(left > mid)
        return query(left , right , Rson(pos));
    else
        return max(query(left , mid , Lson(pos)),query(mid+1 , right , Rson(pos)));
}

void input(){
    char c;
    int x , y;
    for(int i = 1 ; i <= n ; i++) 
        scanf("%d%*c" , &num[i]);
    init(1 , n , 1);
    while(m--){
        scanf("%c%d%d%*c" , &c , &x , &y);
        if(c == 'Q'){
            printf("%d\n" , query(x , y , 1));
        }
        else
            update(x , y , 1);
    }
}

int main(){
    while(scanf("%d%d" , &n , &m) != EOF)
        input();
    return 0;
}