Cod sursa(job #3153808)

Utilizator petric_mariaPetric Maria petric_maria Data 1 octombrie 2023 13:22:22
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("arbint.in");
ofstream g("arbint.out");
int n,m,a[300001],i,j,x,p,b,c;
int maxim(int stq,int drq,int stn,int drn,int indn){
    if(stn>=stq && drn<=drq) return a[indn];
    int ma=0;
    if(stq<=(stn+drn)/2) ma=maxim(stq, drq, stn, (stn+drn)/2, indn*2);
    if(drq>(stn+drn)/2) ma=max(ma, maxim(stq, drq, (stn+drn)/2+1, drn, indn*2+1));
    return ma;
}
void update1(int poz,int val){
    a[poz]=val;
    while(poz>=1){
        poz/=2;
        a[poz]=max(a[poz*2], a[poz*2+1]);
    }
}
void update2(int poz, int val, int stn,int drn, int indn){
    if(stn==drn) a[stn]=val;
    else{
        if(poz<=(stn+drn)/2) update2(poz, val, stn, (stn+drn)/2, indn*2);
        else  update2(poz, val, (stn+drn)/2+1, drn, indn*2+1);
        a[poz]=max(a[poz*2], a[poz*2+1]);
    }
}
int main()
{
    f>>n>>m;
    p=1;
    while(p<n) p*=2;
    for(i=1;i<=n;++i){
        f>>x;
        a[p+i-1]=x;
    }
    for(i=p-1;i>=1;--i) a[i]=max(a[i*2],a[i*2+1]);
    for(i=1;i<=m;++i){
        f>>x>>b>>c;
        if(x==0){
            g<<maxim(b,c,1,p,1)<<'\n';
//            for(j=1;j<=2*p+1;++j) cout<<a[j]<<' ';
//            cout<<"\n\n";
        }
        else{
            b+=(p-1);
            update1(b,c);
        }
    }
    //for(i=1;i<=2*p+1;++i) cout<<a[i]<<' ';
    return 0;
}