B - I Hate It

思路:线段树板子。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 200010
using namespace std;
int n,m;
struct nond{
    int l,r,max;
}tree[MAXN*4];
void up(int now){
    tree[now].max=max(tree[now*2].max,tree[now*2+1].max);
}
void build(int now,int l,int r){
    tree[now].l=l;tree[now].r=r;
    if(tree[now].l==tree[now].r){
        scanf("%d",&tree[now].max);
        return ;
    }
    int mid=(tree[now].l+tree[now].r)/2;
    build(now*2,l,mid);
    build(now*2+1,mid+1,r);
    up(now);
}
void change(int now,int pos,int k){
    if(tree[now].l==tree[now].r){
        tree[now].max=k;
        return ;
    }
    int mid=(tree[now].l+tree[now].r)/2;
    if(pos<=mid)    change(now*2,pos,k);
    else     change(now*2+1,pos,k);
    up(now);
}
int query(int now,int l,int r){
    if(tree[now].l==l&&tree[now].r==r)
        return tree[now].max;
    int mid=(tree[now].l+tree[now].r)/2;
    if(r<=mid)    return query(now*2,l,r);
    else if(l>mid)    return query(now*2+1,l,r);
    else return max(query(now*2,l,mid),query(now*2+1,mid+1,r));
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        build(1,1,n);
        for(int i=1;i<=m;i++){
            char c;int x,y;
            scanf("\n%c%d%d",&c,&x,&y);
            if(c=='Q')    printf("%d\n",query(1,x,y));
            else if(c=='U')    change(1,x,y);
        }
    }
}

 

 
posted @ 2018-02-24 11:15  一蓑烟雨任生平  阅读(165)  评论(0编辑  收藏  举报