Cod sursa(job #1922617)

Utilizator al.mocanuAlexandru Mocanu al.mocanu Data 10 martie 2017 18:05:36
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>
#define MAX 100005
using namespace std;

ifstream f("arbint.in");
ofstream g("arbint.out");

int n, m, t, x, y;
int a[3 * MAX];

void update(int pos, int l, int r, int x, int y){
    if(l == r){
        a[pos] = y;
        return;
    }

    int m = (l + r) / 2;
    if(x <= m)
        update(2 * pos, l, m, x, y);
    else
        update(2 * pos + 1, m + 1, r, x, y);

    a[pos] = max(a[2 * pos], a[2 * pos + 1]);
}

int query(int pos, int l, int r, int s, int d){
    if(s <= l && r <= d)
        return a[pos];

    int m = (l + r) / 2;
    int v1 = 0, v2 = 0;
    if(s <= m)
        v1 = query(2 * pos, l, m, s, d);
    if(m < d)
        v2 = query(2 * pos + 1, m + 1, r, s, d);

    return max(v1, v2);
}

int main(){
    f >> n >> m;
    for(int i = 1; i <= n; ++i){
        f >> x;
        update(1, 1, n, i, x);
    }

    for(int i = 0; i < m; ++i){
        f >> t >> x >> y;
        if(t == 0)
            g << query(1, 1, n, x, y) << "\n";
        else
            update(1, 1, n, x, y);
    }
    return 0;
}