hdu1754(线段树)

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

线段树功能:update:单点替换 query:区间最值

模板裸题。。。

#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 200010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int mx[N<<2];
int a[N];
void Pushup(int rt)
{
    mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        mx[rt]=a[l];
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    Pushup(rt);
}
void update(int pos,int num,int l,int r,int rt)
{
    if(l==r)
    {
        mx[rt]=num;
        return;
    }
    int m=(l+r)>>1;
    if(pos<=m)update(pos,num,lson);
    if(m<pos)update(pos,num,rson);
    Pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return mx[rt];
    }
    int m=(l+r)>>1;
    int res=0;
    if(L<=m)res=max(res,query(L,R,lson));
    if(m<R)res=max(res,query(L,R,rson));
    return res;
}
int main()
{
    int n,m;
    char op[10];
    while(scanf("%d%d",&n,&m)>0)
    {
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        build(1,n,1);
        while(m--)
        {
            int a,b;
            scanf("%s%d%d",op,&a,&b);
            if(op[0]=='Q')
                printf("%d
",query(a,b,1,n,1));
            else update(a,b,1,n,1);
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/lienus/p/4240106.html