Cod sursa(job #1170431)

Utilizator mihai995mihai995 mihai995 Data 13 aprilie 2014 16:40:33
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.8 kb
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;

const int N = 1 + 1e5;

class MonoAib{
    int st[N], dr[N], val[N], size;

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

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

    void improve(int x, int newVal){
        val[x] = newVal;
        for (int i = x ; i <= size ; i += lsb(i))
            st[i] = comp(st[i], newVal);
        for (int i = x ; i ; i -= lsb(i))
            dr[i] = comp(dr[i], newVal);
    }

    void replaceSt(int x, int badVal){

    }

    void replace(int x, int newVal){
        int oldVal = val[x];
        val[x] = newVal;
        for (int i = x ; i <= size ; i += lsb(i))
            if (st[i] == oldVal)
                st[i] = comp( comp( query(i - lsb(i) + 1, x), query(x + 1, i - 1) ), val[i] );
        for (int i = x ; i ; i -= lsb(i))
            if (dr[i] == oldVal)
                dr[i] = comp( val[i], query(i + 1, i + lsb(i) - 1) );
    }
public:
    void reset(int n){
        memset(st, 0, sizeof(st));
        memset(dr, 0, sizeof(dr));
        memset(val, 0, sizeof(val));
        size = n;
    }

    void update(int x, int newVal){
        if ( comp(val[x], newVal) == val[x] )
            replace(x, newVal);
        else
            improve(x, newVal);
    }

    int query(int x, int y){
        if (y < x) return 0;

        int ans = val[x], p;
        while (x != y){
            p = lsb(x | y);

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

            if (y & p){
                ans = comp(ans, st[y]);
                y -= p;
            }
        }
        return comp(ans, val[x]);
    }

    bool integrityCheck(int x){
        int ans;
        ///STANGA
        ans = 0;
        for (int i = x - lsb(x) + 1 ; i <= x ; i++)
            ans = comp(ans, val[i]);
        if ( st[x] != ans )
            return false;
        ///DREAPTA:
        ans = 0;
        for (int i = x ; i < x + lsb(x) ; i++)
            ans = comp(ans, val[i]);
        return dr[x] == ans;
    }

    bool fullIntegrityCheck(){
        for (int i = 1 ; i <= size ; i++)
            if ( !integrityCheck(i) )
                return false;
        return true;
    }
};

MonoAib 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;
}