Cod sursa(job #3374072)

Utilizator Tudor_TapeTapu Tudor Cristian Tudor_Tape Data 8 noiembrie 2026 17:04:43
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.78 kb
#include <bits/stdc++.h>
using namespace std;

#define NMAX 200000

struct aint{
    pair<int, int> V[4*NMAX+5];
    int P[NMAX+5];
    int R[4*NMAX+5];
    void run() {
        V[1].first=1;
        V[1].second=NMAX;
        for (int i=0; i<=NMAX; i++) P[i]=0;
        for (int i=0; i<=4*NMAX; i++) R[i]=0;
        for (int i=2; i<=4*NMAX; i++) {
            if (i%2==0) {
                V[i].first=V[i/2].first;
                V[i].second=V[i/2].first+((V[i/2].second-V[i/2].first)/2);
            }
            else {
                V[i].first=V[i/2].first+((V[i/2].second-V[i/2].first)/2)+1;
                V[i].second=V[i/2].second;
            }
            if (V[i].first==V[i].second && P[V[i].first]==0) {
                P[V[i].first]=i;
            }
        }
        P[1]=P[2]-1;
    }
    void update(int x, int poz, int i) {
        if (P[i]==poz) R[poz]=x;
        else {
            R[poz]=max(R[poz*2], R[poz*2+1]);
        }
        if (poz!=1) update(x, poz/2, i);
    }
    int ans(int poz, int l1, int l2) {
        if (V[poz].first==V[poz].second) return R[poz];
        if (V[poz].first>=l1 && V[poz].second<=l2) return R[poz];
        int s1=0;
        if (l1<V[poz*2+1].first) s1=ans(poz*2, l1, l2);
        int s2=0;
        if (l2>V[poz*2].second) s2=ans(poz*2+1, l1, l2);
        return max(s1, s2);
    }
};

int X[100005];
aint P;
int main() {
    int n, q;
    cin>>n>>q;
    P.run();
    for (int i=1; i<=n; i++) {
        cin>>X[i];
        P.update(X[i], P.P[i], i);
    }
    for (int i=1; i<=q; i++) {
        int q, a, b;
        cin>>q>>a>>b;
        if (q==1) {
            P.update(b, P.P[a], a);
        }
        else if (q==0) cout<<P.ans(1, a, b)<<"\n";
        else cout<<"🥚🥚🍳🍴👍\n";
    }
}