Cod sursa(job #3032655)

Utilizator ReBeGhElRebegea Stefan ReBeGhEl Data 22 martie 2023 16:07:07
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream cin("arbint.in");
ofstream cout("arbint.out");

vector < int > ST;

void update(int node, int from, int to , int poz, int val)
{
    if(from==to)
    {
        ST[node]=val;
        return ;
    }
    int mid=(from+to)/2;
    if(poz<=mid)
        update(node*2,from,mid,poz,val);
    else
        update(node*2+1,mid+1,to,poz,val);
    ST[node]=max(ST[node*2],ST[node*2+1]);
}

int query(int node, int from , int to, int qst, int qdr)
{
    int smax=0;
    if(qst<=from && to<=qdr)
        return ST[node];
    int mid = (from + to)/2;
    if(qst<=mid)
    {
        int s=query(node*2,from,mid,qst,qdr);
        smax=max(smax,s);
    }
        if(mid+1<=qdr)
    {
        int s=query(node*2+1,mid+1,to,qst,qdr);
        smax=max(smax,s);
    }
    return smax;
}

int main()
{
    int n,m,a,x,y;
    cin>>n>>m;
    ST.resize(4*n+5);
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        update(1,1,n,i,a);
    }
    for(int i=1;i<=m;i++)
    {
        cin>>a>>x>>y;
        if(a==1)
            update(1,1,n,x,y);
        else
            cout<<query(1,1,n,x,y)<<'\n';
    }
    return 0;
}