Hdu 1754 I Hate It

来源:互联网 发布:许晴航母 知乎 编辑:程序博客网 时间:2024/05/16 06:36

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754


线段树处理单点更新,区间最值问题模型。。

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <map>#include <queue>#include <algorithm>using namespace std;#define Maxn 200005#define lx (x<<1)#define rx (x<<1 | 1)#define MID ((l + r)>>1)int A[Maxn];int S[Maxn<<2];void pushUp(int x){    S[x] = max(S[lx],S[rx]);}void build(int l,int r,int x){    if(l == r)    {        S[x] = A[l];        return;    }    build(l,MID,lx);    build(MID+1,r,rx);    pushUp(x);}int query(int L,int R,int l,int r,int x){    if(L<=l && r<=R) return S[x];    int ans = 0;    if(L<=MID) ans = max(ans,query(L,R,l,MID,lx));    if(MID<R) ans = max(ans,query(L,R,MID+1,r,rx));    return ans;}void update(int p,int d,int l,int r,int x){    if(l == r)    {        S[x] = d;        return;    }    if(p <= MID) update(p,d,l,MID,lx);    else update(p,d,MID+1,r,rx);    pushUp(x);}int main(){#ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);#endif    int m,n;    char cmd[10];    int a,b;    while(scanf(" %d %d",&n,&m)!=EOF)    {        for(int i=1;i<=n;i++) scanf(" %d",&A[i]);        build(1,n,1);        while(m--)        {            scanf(" %s",cmd);            scanf(" %d %d",&a,&b);            if(cmd[0] == 'Q') printf("%d\n",query(a,b,1,n,1));            else if(cmd[0] == 'U') update(a,b,1,n,1);        }    }    return 0;}