Cod sursa(job #2497867)

Utilizator AntoniuFicAntoniu Ficard AntoniuFic Data 23 noiembrie 2019 11:37:53
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include <iostream>
#include <fstream>
///https://infoarena.ro/problema/armin
using namespace std;

int arb[400000], a[100000], n, m;

int build(int s, int f, int pos){
    if(s == f){
        arb[pos] = a[s];
        return arb[pos];
    }
    int mij = (s + f) / 2;
    arb[pos] = max(build(s, mij, 2 * pos), build(mij + 1, f, 2 * pos + 1));
    return arb[pos];
}

int update(int s, int f, int pos, int val, int u){
    if(s == f){
        arb[pos] = val;
        return val;
    }
    int mij = (s + f) / 2;
    if(u <= mij && u >= s)
        arb[pos] = max(arb[2 * pos + 1], update(s, mij, 2 * pos, val, u));
    else if(u >= mij + 1 && u <= f){
        arb[pos] = max(arb[2 * pos], update(mij + 1, f, 2 * pos + 1, val, u));
    }
    return arb[pos];
}

int call(int s, int f, int pos, int x, int y){
    if(x == s && f == y)
        return arb[pos];
    if(s == f){
        return arb[pos];
    }
    int mij = (s + f) / 2;
    if(x > mij && y > mij){
        return call(mij + 1, f, pos * 2 + 1, x , y);
    }else if(x <= mij && y <= mij){
        return call(s, mij, 2 * pos, x, y);
    }else{
        return max(call(s, mij, 2 * pos, x, mij), call(mij + 1, f, 2 * pos + 1, mij + 1, y));
    }
}

int main(){
    ifstream f("arbint.in");
    ofstream g("arbint.out");
    f>>n>>m;
    for (int i = 1; i <= n; ++i) {
        f>>a[i];
    }
    build(1, n, 1);
    for (int i = 0, code, x, y; i < m; ++i) {
        f>>code>>x>>y;
        if(code == 0){
            g<<call(1, n, 1, x, y)<<'\n';
        }else{
            update(1, n, 1, y, x);
        }
    }
    return 0;
}