Cod sursa(job #2727620)

Utilizator vansJos da pa perete vans Data 22 martie 2021 10:55:07
Problema Arbori de intervale Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.49 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 1e5, CHMAX = sqrt(NMAX) + 1;

int t, a[NMAX + 1], ch[CHMAX + 5], al, chl, chsz, x, y, op;

void generCh() {
    chsz = sqrt(al);
    for(int i = 1; i <= al; ++i) {
        if((i - 1) % chsz == 0) ch[++chl] = a[i];
        ch[chl] = max(ch[chl], a[i]);
    }
}

inline int getCh(const int X) {
    return (X - 1) / chsz + 1;
}

inline void updCh(const int POZ, const int VAL) {
    const int CHPOZ = getCh(POZ);
    ch[CHPOZ] = a[POZ] = VAL;
    for(int i = (CHPOZ - 1) * chsz + 1; i <= CHPOZ * chsz; ++i)
        ch[CHPOZ] = max(ch[CHPOZ], a[POZ]);
}

inline int queryCh(const int ST, const int DR) {
    int TORET = a[ST];
    const int CHST = getCh(ST), CHDR = getCh(DR);
    for(int i = CHST + 1; i <= CHDR - 1; ++i) TORET = max(TORET, ch[i]);
    for(int i = min(CHST * chsz, DR); i >= ST; --i) TORET = max(TORET, a[i]);
    for(int i = max((CHDR - 1) * chsz + 1, ST); i <= DR; ++i) TORET = max(TORET, a[i]);
    return TORET;
}

int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    scanf("%d%d", &al, &t);
    for(int i = 1; i <= al; ++i) scanf("%d", &a[i]);
    generCh();

    /*for(int i = 1; i <= al; ++i) cout<<a[i]<<" "; cout<<"\n";
    for(int i = 1; i <= al; ++i) cout<<getCh(i)<<" "; cout<<"\n";*/

    while(t--) {
        scanf("%d%d%d", &op, &x, &y);
        if(!op) printf("%d\n", queryCh(x, y));
        else updCh(x, y);
    }
    return 0;
}