Cod sursa(job #3128285)

Utilizator theodortuduranwqeqwe theodortuduran Data 9 mai 2023 09:20:09
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.75 kb
#include <bits/stdc++.h>

using namespace std;
using ll=long long;

void rep(vector<ll> &v,ll a,ll b,ll p2)
{
    v.at(p2/2+a)=b;
    for(int i=p2-1; i>1; i-=2)
    {
        v.at(i/2-1)=max(v.at(i),v.at(i-1));
    }
}
void mn(vector<ll> &v,ll a,ll b,ll p2)
{
    a+=p2/2;
    b+=p2/2;
    ll ax1=-1e9,ax2=-1e9;
    if(a%2==0)
    {
        ax1=v.at(a);
        a++;
    }
    if(b%2==1)
    {
        ax2=v.at(b);
        b--;
    }

    while(  true)
    {
        if ((a-2)/2<=0 || ((a-2)/2)%2==0)
        {
            break;
        }
        a=(a-1)/2;
    }
    while( true  )
    {
        if ((b-2)/2<=0 || ((b-2)/2)%2==1)
        {
            break;
        }
        b=(b-2)/2;
    }

    ll ans=max(v.at(a),v.at(b));
    ans=max(ans,ax1);
    ans=max(ans,ax2);
    cout<<ans<<endl;
}
int main()
{
    ifstream cin("arbint.in");
   ofstream cout("arbint.out");
    vector<ll> m2(30);

    for(int i=0; i<30; i++)
    {
        m2.at(i)=pow(2,i+1);
    }
    ll n,n2;
    cin>>n>>n2;
    ll p2;
    for(auto cl:m2)
    {
        if(cl>=n)
        {
            p2=cl*2-1;
            break;
        }
    }
    vector<ll> v(p2);
    for(int i=p2/2; i<p2/2+n; i++)
    {
        cin>>v.at(i);
    }

    for(int i=p2/2+n; i<p2 ; i++)
    {
        v.at(i)=0;
    }
    for(int i=p2-1; i>1; i-=2)
    {
        v.at(i/2-1)=max(v.at(i),v.at(i-1));
    }

    ll x;
    ll a,b;
    while(n2-->0)
    {
        cin>>x>>a>>b;

        if(x==1)
        {
            a-- ;

            rep(v,a,b,p2);


        }
        else
        {
            a-- ;
            b--;
            if(a!=b-1)
            {
                mn(v,a,b,p2);
            }
            else
            {
                cout<<max(v.at(p2/2+a),v.at(p2/2+b))<<endl;
            }

        }
    }
    return 0;
}