Cod sursa(job #1673987)

Utilizator bogdanciurezubogdan ciurezu bogdanciurezu Data 4 aprilie 2016 11:54:56
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <fstream>

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

const int nmax = 1000000;
int n, adi[nmax], p = 1, m;

int query(int ind, int st, int dr, int stc, int drc){
    int mij;
    if(st == stc && dr == drc){
        return adi[ind];
    }
    else {
        mij = (st + dr) / 2;

        if(stc <= mij && drc > mij){
            int x1, x2;
            x1 = query( 2 * ind, st, mij, stc, mij);
            x2 = query( 2 * ind + 1, mij + 1, dr, mij + 1, drc);
            return max( x1,  x2);
        }
        else if(stc <= mij){
            return query( 2 * ind, st, mij, stc, drc );
        }
        else return query( 2 * ind + 1, mij + 1, dr, stc, drc );
    }

}

void update(int poz, int val){
    adi[poz] = val;
    poz /= 2;

    while(poz){
        adi[poz] = max( adi[2 * poz], adi[2 * poz + 1] );
        poz /= 2;
    }
}

int main()
{
    f>>n>>m;

    while(p <= n){
        p<<=1;
    }

    for(int i = p; i <= p + n - 1; ++i){
        f>>adi[i];
    }

    for(int i = p - 1; i >= 1; --i){
        adi[i] = max( adi[2 * i], adi[2 * i + 1] );
    }

    for(int i = 1, o, a, b; i <= m; ++i){
        f>>o>>a>>b;

        if(o == 1){
            update(p + a - 1, b);
        }
        else {
            g<<query(1, 1, p, a, b)<<'\n';
        }
    }
    return 0;
}