Live2D

[线段树] (b) hdu1754 单点修改区间求最大

(b) hdu1754 I Hate It

如在阅读本文时遇到不懂的部分,请在评论区询问,或跳转 线段树总介绍

/*hdu1754 */
/*多组数据 woc*/

#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
#define mid (t[rt].l+t[rt].r>>1)
#define ls (rt<<1)
#define rs (ls|1)
#define mid (t[rt].l+t[rt].r>>1)
#define pushup(rt) t[rt].mx=max(t[ls].mx,t[rs].mx)
/*
N M
student : 1 - N
      (1 - M)
one for Q/U A B
Q : Max of [A,B]
U : A -> B
*/
const int N=2e6+2;
struct node{int l,r,mx;}t[N<<2];
int a[N],n,m;
void build(int rt,int l,int r){
    t[rt].l=l,t[rt].r=r,t[rt].mx=0;
    if(l==r){t[rt].mx=a[l];return;}
    build(ls,l,mid);build(rs,mid+1,r);
    pushup(rt);
}
void update(int rt,int x,int delta){
    if(x==t[rt].l&&x==t[rt].r){t[rt].mx=delta;return;}
    if(x<=mid)update(ls,x,delta);
    else update(rs,x,delta);
    pushup(rt);return;
}
int query(int rt,int x,int y){
    if(t[rt].l>=x&&t[rt].r<=y)return t[rt].mx;
    int res=-0x7fffffff;
    if(x<=mid)res=max(res,query(ls,x,y));
    if(y>mid)res=max(res,query(rs,x,y));
    return res;
}
int main()
{
    //freopen("b.in","r",stdin);
    //freopen("b.out","w",stdout);
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=1;i<=n;++i) scanf("%d",&a[i]);
        register char ch;
        register int x,y;
        build(1,1,n);
        while(m--){
            cin>>ws;
            scanf("%c%d%d",&ch,&x,&y);
            if(ch=='Q')printf("%d\n",query(1,x,y));
            else update(1,x,y);
        }
    }
    return 0;
}

 

posted @ 2019-07-22 19:18  lsy263  阅读(100)  评论(0编辑  收藏  举报