Cod sursa(job #1182007)

Utilizator mihai995mihai995 mihai995 Data 4 mai 2014 14:37:15
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.88 kb
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;

const int N = 1e5;

template<class Type, const Type E, Type comp(Type, Type)>
class MonoAib{
    Type st[N + 2], dr[N + 1];
    int size;

    inline static int lsb(int x){
        return x & -x;
    }

    inline Type comp3(Type a, Type b, Type c){
        return comp( comp(a, b), c );
    }
public:
    void reset(int n){
        memset(st, E, sizeof(st));
        memset(dr, E, sizeof(dr));
        size = n;
    }

    void update(int x, Type newVal){
        for (int i = x + 1 ; i <= size + 1 ; i += lsb(i))
            st[i] = comp3( eQuery(i - lsb(i), x), newVal, eQuery(x + 1, i) );
        for (int i = x ; i ; i -= lsb(i))
            dr[i] = comp3( eQuery(i, x), newVal, eQuery(x + 1, i + lsb(i)) );
    }

    Type eQuery(int x, int y){
        if (y <= x)
            return E;

        Type left = E, right = E;
        int p;

        while (x != y){
            p = lsb(x | y);

            if (x & p){
                left = comp(left, dr[x]);
                x += p;
            }

            if (y & p){
                right = comp(st[y], right);
                y -= p;
            }
        }
        return comp(left, right);
    }

    Type query(int x, int y){
        return eQuery(x, y + 1);
    }
};

inline int max(int a, int b){
    return a > b ? a : b;
}

MonoAib<int, 0, max> A;


int main(){
    int n, nrE, tip, x, y;

    ifstream in("arbint.in");
    in >> n >> nrE;
    A.reset(n);

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

    ofstream out("arbint.out");

    while (nrE--){
        in >> tip >> x >> y;

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

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

    return 0;
}