Cod sursa(job #3033161)

Utilizator tonealexandruTone Alexandru tonealexandru Data 23 martie 2023 14:20:54
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int st[400005];
void update(int node, int from, int to, int pos, int val)
{
    if(from==to)
    {
        st[node]=val;
        return;
    }
    int mid=(from+to)/2;
    if(pos<=mid)
        update(node*2, from, mid, pos, val);
    else
        update(node*2+1, mid+1, to, pos, val);

    st[node]=max(st[node*2], st[node*2+1]);
}

int query(int node, int from, int to, int qleft, int qright)
{
    int smax=0;
    if(qleft<=from && to<=qright)
        return st[node];

    int mid=(from+to)/2;
    if(qleft<=mid)
    {
        int s=query(node*2, from, mid, qleft, qright);
        smax=max(smax, s);
    }
    if(mid+1<=qright)
    {
        int s=query(node*2+1, mid+1, to, qleft, qright);
        smax=max(smax, s);
    }
    return smax;
}

int main()
{
    ifstream cin("arbint.in");
    ofstream cout("arbint.out");
    int n,k,x,op,a,b;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        update(1,1,n,i,x);
    }
    for(int i=0;i<k;i++)
    {
        cin>>op>>a>>b;
        if(op==0)
            cout<<query(1,1,n,a,b)<<'\n';
        else if(op==1)
            update(1,1,n,a,b);
    }

    return 0;
}