Cod sursa(job #1193814)

Utilizator mihai995mihai995 mihai995 Data 1 iunie 2014 22:50:10
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
using namespace std;

const int N = 1 << 17;

class Aint{
    int aint[2 * N];

    inline static int getPos(int n){
        return N + n - 1;
    }
public:
    void update(int poz, int val){
        poz = getPos(poz);
        aint[poz] = val;

        while (poz >>= 1)
            aint[poz] = max(aint[poz << 1], aint[1 + (poz << 1)]);
    }

    int query(int x, int y){
        x = getPos(x);
        y = getPos(y);

        int ans = max( aint[x], aint[y] );
        while ( (x >> 1) != (y >> 1) ){
            if ( (x & 1) == 0 )
                ans = max(ans, aint[x + 1]);
            if (y & 1)
                ans = max(ans, aint[y - 1]);
            x >>= 1;
            y >>= 1;
        }
        return ans;
    }
};

Aint A;

int main(){
    int n, nrQ, tip, x, y;
    ifstream in("arbint.in");

    in >> n >> nrQ;
    for (int i = 1 ; i <= n ; i++){
        in >> x;
        A.update(i, x);
    }

    ofstream out("arbint.out");
    while (nrQ--){
        in >> tip >> x >> y;

        if (tip == 0)
            out << A.query(x, y) << '\n';
        else
            A.update(x, y);
    }

    out.close();
    in.close();

    return 0;
}