hdu1754 单点更新

http://acm.hdu.edu.cn/showproblem.php?pid=1754
中文题 不解释题意

线段树 单点更新,维护区间的最大值

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <math.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <functional>
#define mem(a) memset(a,0,sizeof(a));
#define mem_1(a) memset(a,-1,sizeof(a));
#define sf(a) scanf("%d",&a)
#define sff(a,b) scanf("%d%d",&a,&b)
#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define LL long long
#define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
const int INF = 0x7FFFFFFF;
const int MAXN = 200100;
const double PI = acos(-1.0);
const double esp = 1e-10;
using namespace std;
struct node
{
    int l,r,Max;
}Tree[MAXN<<2];
void build_tree(int l,int r,int root)
{
    Tree[root].l = l;
    Tree[root].r = r;
    if(Tree[root].l == Tree[root].r)
    {
        sf(Tree[root].Max);
        return ;
    }
    int mid = (l + r)>>1;
    build_tree(lson);
    build_tree(rson);
    Tree[root].Max = max(Tree[root << 1].Max , Tree[root << 1|1].Max);
}
void updata(int l,int r,int root,int k)
{
    if(Tree[root].l == l && Tree[root].r == r)
    {
        Tree[root].Max = k;
        return ;
    }
    int mid = (Tree[root].l + Tree[root].r)>>1;
    if(r <= mid) updata(l,r,root<<1,k);
    else if(mid < l) updata(l,r,root<<1|1,k);
    else
    {
        updata(lson,k);
        updata(rson,k);
    }
    Tree[root].Max = max(Tree[root<<1].Max, Tree[root<<1|1].Max);
}
int Query(int l,int r,int root)
{
    if(Tree[root].l == l && Tree[root].r == r)
    {
        return Tree[root].Max;
    }
    int ans = -1;
    int mid = (Tree[root].l + Tree[root].r) >> 1;
    if(r <= mid) ans = max(ans,Query(l,r,root<<1));
    else if(mid < l) ans = max(ans,Query(l,r,root<<1|1));
    else
    {
        ans = max(ans,Query(l,mid,root<<1));
        ans = max(ans,Query(mid+1,r,root<<1|1));
    }
    return ans;
}
int main()
{
    int n,m,x,y;
    char c;
    while(~sff(n,m))
    {
        build_tree(1,n,1);
        while(m--)
        {
            cin >> c;
            sff(x,y);
            if(c=='Q')
            {
                printf("%d\n",Query(x,y,1));
            }
            else if(c=='U')
            {
                updata(x,x,1,y);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值