Cod sursa(job #1700299)

Utilizator mihai995mihai995 mihai995 Data 9 mai 2016 23:32:14
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <fstream>
using namespace std;

const int N = 1 << 17;

template<class Node, Node (*comp)(Node, Node)>
class Aint{
    Node aint[2 * N];
public:
    void update(int poz, Node val){
        aint[ poz += N - 1 ] = val;
        while (poz >>= 1)
            aint[poz] = comp(aint[poz << 1], aint[1 + (poz << 1)]);
    }

    Node query(int x, int y){
        Node ansX = aint[x += N - 1], ansY = aint[y += N - 1];

        int stopX = x >> (31 - __builtin_clz(x ^ y) );
        int stopY = y >> (31 - __builtin_clz(x ^ y) );

        while ( stopX < ( x >>= __builtin_ctz(~x) ) )
            ansX = comp( ansX, aint[x ^= 1] );
        while ( stopY < ( y >>= __builtin_ctz(y) ) )
            ansY = comp( aint[y ^= 1], ansY );
        return comp(ansX, ansY);
    }
};

inline int get_max(int a, int b){
	return a > b ? a : b;
}
Aint<int, get_max> 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;
}