洛谷P1531 I Hate It(线段树)

I Hate It

题目背景

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。这让很多学生很反感。

题目描述

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩

输入输出格式

输入格式:
第一行,有两个正整数 N 和 M ( 0

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#define maxn 5000000
using namespace std;

struct arr
{
    int l,r,w;
}tree[maxn];

int a[maxn],n,m,ans;
char ch;

int max(int x,int y)
{
    if (x>y) return x;
    return y;
}

void build(int p)
{
    if (tree[p].l==tree[p].r)
    {
        tree[p].w=a[tree[p].l];
        return;
    }
    int mid=(tree[p].l+tree[p].r)/2;
    tree[p*2].l=tree[p].l;
    tree[p*2].r=mid;
    tree[p*2+1].l=mid+1;
    tree[p*2+1].r=tree[p].r;
    build(p*2);
    build(p*2+1);
    tree[p].w=max(tree[p*2].w,tree[p*2+1].w);
}

void change(int p,int x,int y)
{
    if (tree[p].l==tree[p].r)
    {
        if (tree[p].w<y) tree[p].w=y;
        return;
    }
    int mid=(tree[p].l+tree[p].r)/2;
    if (x<=mid) change(p*2,x,y);
        else change(p*2+1,x,y);
    tree[p].w=max(tree[p*2].w,tree[p*2+1].w);
}

void find(int p,int x,int y)
{
    if (x==tree[p].l&&y==tree[p].r)
    {
        ans=max(ans,tree[p].w);
        return;
    }
    int mid=(tree[p].l+tree[p].r)/2;
    if (y<=mid) find(p*2,x,y);
        else if (x>mid) find(p*2+1,x,y);
            else 
            {
                find(p*2,x,mid);
                find(p*2+1,mid+1,y);
            }
}

int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    tree[1].l=1;
    tree[1].r=n;
    build(1);
    for (int i=1;i<=m;i++)
    {
        int x,y;
        ans=0;
        cin>>ch;
        scanf("%d%d",&x,&y);
        if (ch=='U') change(1,x,y);
            else 
            {
                find(1,x,y);
                printf("%d\n",ans);
            }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值