P1531 I Hate It 线段树解法

P1531 I Hate It
区间最大值,区间查询最大值,单点更新

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//P1531 I Hate It
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=222222;
int Max[maxn<<2];
void PushUp(int rt)
{
    Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    if (l==r)
    {
        cin>>Max[rt];
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUp(rt);
}
void update(int p,int sc,int l,int r,int rt)
{
    if (l==r)
    {
        if (Max[rt]<sc)
        {
            Max[rt]=sc;
        }
        return ;
    }
    int m=(l+r)>>1;
    if (p<=m) update(p,sc,lson);
    else update(p,sc,rson);
    PushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if (L<=l&&r<=R)
    {
        return Max[rt];
    }
    int m=(l+r)>>1;
    int ret=0;
    if (L<=m) ret=max(ret,query(L,R,lson));
    if (R>m) ret=max(ret,query(L,R,rson));
    return ret;
}
int main()
{
    int n,m;
    cin>>n>>m;
    build(1,n,1);
    for (int i=1;i<=m;i++)
    {
        char c;
        int x,y;
        cin>>c>>x>>y;
        if (c=='Q')
        {
            cout<<query(x,y,1,n,1)<<endl;
        }
        else
        {
            update(x,y,1,n,1);
        }
    }
}

  

posted @   心悟&&星际  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示