Cod sursa(job #2886904)

Utilizator BlueLuca888Girbovan Robert Luca BlueLuca888 Data 8 aprilie 2022 16:28:58
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.95 kb
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")

using namespace std;

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

const int MAX_N = 100005;
int n, v[MAX_N];

const int MAX_Q = 100005;
int q, t, x, y;

struct SEGTREE{
    int aint[4 * MAX_N];

    void build(int nod, int st, int dr){
        int md = st + ((dr-st)>>1);
        if(st == dr){
            aint[nod] = v[md];
            return;
        }

        build(2*nod  , st  , md);
        build(2*nod+1, md+1, dr);
        aint[nod] = max(aint[2*nod], aint[2*nod+1]);
    }

    int poz, val;
    void update(int nod, int st, int dr){
        int md = st + ((dr-st)>>1);
        if(st == dr){
            aint[nod] = val;
            return;
        }

        if(poz <= md)
            update(2*nod  , st  , md);
        else
            update(2*nod+1, md+1, dr);

        aint[nod] = max(aint[2*nod], aint[2*nod+1]);
    }
    void renew(const int pozitie, const int newVal){
        poz = pozitie;
        val = newVal;
        update(1, 1, n);
    }

    int lft, rgt, sol;
    void query(int nod, int st, int dr){
        if(lft <= st && dr <= rgt){
            sol = max(sol, aint[nod]);
            return;
        }
        int md = st + ((dr-st)>>1);
        if(lft <= md) query(2*nod  , st  , md);
        if(rgt >  md) query(2*nod+1, md+1, dr);
    }
    int get_max(const int stanga, const int dreapta){
        sol = -1;
        lft = stanga;
        rgt = dreapta;
        query(1, 1, n);
        return sol;
    }

} tree;

int main (){
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);

    fin>>n>>q;
    for(int i=1; i<=n; i++)
        fin>>v[i];

    tree.build(1, 1, n);

    while(q--){
        fin>>t>>x>>y;

        if(t == 0) ///query
            fout<<tree.get_max(x, y)<<"\n";

        if(t == 1) ///update
            tree.renew(x, y);
    }
    return 0;
}