hdu 1754 线段树单点更新求最大值

来源:互联网 发布:linux suspend 命令 编辑:程序博客网 时间:2024/06/05 15:35

没啥好说的版子题

#include <stdio.h>#include <cstdlib>#include <iostream>#include <cmath>#include <algorithm>#include <cstring>#include <map>#include <cstring>using namespace std;struct node{    int l,r,max1;}tree[800004];void update(int rt){    tree[rt].max1=max(tree[2*rt+1].max1,tree[2*rt].max1);}void build(int rt,int l,int r){    tree[rt].l=l,tree[rt].r=r;    if(l==r)    { //printf("ok");        scanf("%d",&tree[rt].max1);        return;    }    int mid=(r+l)/2;    build(rt*2,l,mid);    build(rt*2+1,mid+1,r);    update(rt);}int getmax(int rt,int l,int r){    //printf("ok");    if(tree[rt].l==l&&tree[rt].r==r)        return tree[rt].max1;    int mid=(tree[rt].l+tree[rt].r)/2;    if(mid<l)       return getmax(rt*2+1,l,r);    if(mid>=r)        return getmax(rt*2,l,r);    else        return max(getmax(rt*2+1,mid+1,r),getmax(rt*2,l,mid));}void add(int rt,int a,int b){    //printf("ok");    if(tree[rt].l==a&&tree[rt].r==a)    {        tree[rt].max1=b;        return ;    }    int mid=(tree[rt].l+tree[rt].r)/2;    if(a<=mid)    {        add(rt*2,a,b);    }    else add(rt*2+1,a,b);    update(rt);}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        build(1,1,n);        char ss;        for(int i=0;i<m;i++)        {            scanf(" %c",&ss);            int a,b;            if(ss=='Q')            {               scanf("%d%d",&a,&b);                printf("%d\n",getmax(1,a,b));            }else            { //printf("ok");                scanf("%d%d",&a,&b);                add(1,a,b);            }        }    }}

0 0
原创粉丝点击