Cod sursa(job #3134522)

Utilizator theodortuduranwqeqwe theodortuduran Data 29 mai 2023 11:31:52
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>

using namespace std;
using ll=long long;

ll p2=1;

void update(ll a,ll b,vector<ll> &v)
{

    ll ax=p2+a-1;
    v.at(ax)=b;
    while(ax>1)
    {
        if(ax%2==0)
        {
            v.at(ax/2)=max(v.at(ax),v.at(ax+1));

        }
        else
        {
            v.at(ax/2)=max(v.at(ax),v.at(ax-1));
        }
        ax=ax/2;
    }

}

ll max_of_part(ll l,ll r,vector<ll>&v)
{

    l=p2+l-1;
    r=p2+r-1;
    ll ans=-1;
    while(l<=r)
    {
        if(l%2==1)
        {
            ans=max(ans,v.at(l));
            l++;
        }
        if(r%2==0)
        {
            ans=max(ans,v.at(r));
            r--;
        }
        l=l/2;
        r=r/2;
    }
    return ans;
}

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

    //~~~~~~~~~~~~~~~~~~~~~~ PART 1
    ll n,t;
    cin>>n>>t;
    while(p2<n)
    {
        p2=p2*2;
    }

    vector<ll> v(p2*2);
    for(int i=p2; i<p2+n; i++)
    {
        cin>>v.at(i);
    }
    //~~~~~~~~~~~~~~~~~~~~~~ PART 1
    for(int i=p2*2-1; i>1; i-=2)
    {
        v.at(i/2)=max(v.at(i),v.at(i-1));
    }

    while(t-->0)
    {
        ll c,a,b;
        cin>>c>>a>>b;
        if(c==0)
        {
            cout<<max_of_part(a,b,v)<<endl;
        }
        else
        {

            update(a,b,v);
        }

    }
    return 0;
}