Cod sursa(job #3338872)

Utilizator horatiu.avramAvram Popa Cristian Horatiu horatiu.avram Data 5 februarie 2026 12:45:51
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include<bits/stdc++.h>

using namespace std;

ifstream fin("arbint.in");
ofstream fout("arbint.out");

const int BSIZE=316;
const int MAXN=1e5;

int bucket[BSIZE+1],v[MAXN+1],n;

void update(int pos,int val) {
    int b=pos/BSIZE;
    int sb=b*BSIZE;
    v[pos]=val;
    bucket[b]=0;
    for(int i=sb; i<min(sb+BSIZE,n); i++) {
        bucket[b]=max(bucket[b],v[i]);
    }
}
int query(int l,int r) {
    int bl=l/BSIZE,br=r/BSIZE;
    int ebl=(bl+1)*BSIZE,sbr=br*BSIZE;
    int res=0;
    if(bl==br) {
        for(int i=l; i<=r; i++) {
            res=max(res,v[i]);
        }
        return res;
    }
    while(l<ebl) {
        res=max(res,v[l]);
        l++;
    }
    while(r>=sbr) {
        res=max(res,v[r]);
        r--;
    }
    for(int i=bl+1; i<br; i++) {
        res=max(res,bucket[i]);
    }
    return res;
}
int main() {
    int q,nb,t,a,b;
    fin>>n>>q;
    for(int i=0; i<n; i++) {
        fin>>v[i];
    }
    nb=0;
    for(int i=0; i<n; i+=BSIZE) {
        int j=i;
        bucket[nb]=0;
        while(j<n&&j<i+BSIZE) {
            bucket[nb]=max(bucket[nb],v[j]);
            j++;
        }
        nb++;
    }
    while(q--) {
        fin>>t>>a>>b;
        a--;
        if(t==0) {
            b--;
            fout<<query(a,b)<<'\n';
        } else {
            update(a,b);
        }
    }
    return 0;
}