Cod sursa(job #1952166)

Utilizator waren4Marius Radu waren4 Data 3 aprilie 2017 23:05:03
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

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

int Arb[200004],n,m,Max;

void update(int nod, int st, int dr, int p, int x) {
    if (st == dr) {
        Arb[nod] = x;
        return;
    }
    int mij = (st + dr) / 2;
    if (p <= mij) {
        update(2*nod,st,mij,p,x);
    }
    else {
        update(2*nod+1,mij+1,dr,p,x);
    }
    Arb[nod] = max(Arb[nod*2],Arb[nod*2+1]);
}

void query(int nod, int st, int dr, int x, int y) {
    if (x <= st && y >= dr) {
        Max = max(Max,Arb[nod]);
        return;
    }
    int mij = (st + dr) / 2;
    if (x <= mij) {
        query(nod*2,st,mij,x,y);
    }
    if (y > mij) {
        query(nod*2+1,mij+1,dr,x,y);
    }
}

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