Cod sursa(job #1893176)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 25 februarie 2017 15:22:31
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.64 kb
#include <bits/stdc++.h>

using namespace std;

int aint[400005];
int v[100005];
int qlf, qrg, val;

int combine(const int &a, const int &b){
    if(a > b){
        return a;
    }
    return b;
}

void build(const int &pos, const int &lf, const int &rg){
    if(lf == rg){
        aint[pos] = v[lf];
        return;
    }
    int md = (lf + rg)/2;
    build(2 * pos, lf, md);
    build(2 * pos + 1, md + 1, rg);
    aint[pos] = combine(aint[2 * pos], aint[2 * pos + 1]);
}

int query(const int &pos, const int &lf, const int &rg){
    if(lf > qrg || rg < qlf){
        return -1;
    }else if(qlf <= lf && rg <= qrg){
        return aint[pos];
    }
    int md = (lf + rg)/2;
    return combine(query(2 * pos, lf, md), query(2 * pos + 1, md + 1, rg));
}

int update(const int &pos, const int &lf, const int &rg){
    if(lf == rg && lf == qlf){
        aint[pos] = val;
        return aint[pos];
    }else if(lf <= qlf && qlf <= rg){
        int md = (lf + rg)/2;
        aint[pos] = combine(update(2 * pos, lf, md), update(2 * pos + 1, md + 1, rg));
    }
    return aint[pos];
}

int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    int n,q,i;
    scanf("%d %d", &n, &q);
    for(i = 1;i <= n;i++){
        scanf("%d", &v[i]);
    }
    build(1, 1, n);
    for(i = 1;i <= q;i++){
        int op, x, y;
        scanf("%d %d %d", &op, &x, &y);
        if(op == 0){
            qlf = x;
            qrg = y;
            printf("%d\n", query(1, 1, n));
        }else{
            qlf = x;
            val = y;
            update(1, 1, n);
        }
    }
    return 0;
}