Cod sursa(job #1700340)

Utilizator mihai995mihai995 mihai995 Data 10 mai 2016 01:46:42
Problema Arbori de intervale Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.76 kb
#include <fstream>
#include <iostream>
using namespace std;

const int N = 1 << 17;

template<class Node, Node (*comp)(Node, Node), class Lazy, Node (*apply)(Node, int, int, Lazy), Lazy (*comp_lazy)(Lazy, Lazy)>
class Aint{
    Node aint[2 * N], n_zero, acc;
    Lazy lz[N], l_zero;

    void lazy_update(int pos, int st, int dr, Lazy L){
    	if ( st == dr )
    		aint[pos] = apply( aint[pos], st, dr, L);
    	else
    		lz[pos] = comp_lazy( lz[pos], L );
    }
    inline Node arbint(int pos, int st, int dr){
    	if ( st == dr ) return aint[pos];
    	aint[pos] = apply(aint[pos], st, dr, lz[pos]);

	int m = (st + dr) >> 1;
	lazy_update( 2 * pos,     st,    m,  lz[pos] );
	lazy_update( 2 * pos + 1, m + 1, dr, lz[pos] );
	lz[pos] = l_zero;

	return aint[pos];
    }
    void update(int pos, int st, int dr, int x, int y, Lazy L){
    	if ( x <= st && dr <= y )
    		return lazy_update(pos, st, dr, L);
	arbint(pos, st, dr);

	int m = (st + dr) >> 1;
	if ( x <= m )
		update(2 * pos,     st,    m,  x, y, L);
	if ( m < y )
		update(2 * pos + 1, m + 1, dr, x, y, L);

	aint[pos] = comp( arbint(2 * pos, st, m), arbint(2 * pos + 1, m + 1, dr) );
    }
    void query(int pos, int st, int dr, int x, int y){
    	arbint(pos, st, dr);
    	if ( x <= st && dr <= y ){
    		acc = comp( acc, aint[pos] );
    		return;
    	}
    	int m = (st + dr) >> 1;
    	if ( x <= m )
    		query(2 * pos,     st,    m,  x, y);
    	if ( m < y )
    		query(2 * pos + 1, m + 1, dr, x, y);
    }
public:
    void update(int x, int y, Lazy L){
    	return update(1, 1, N, x, y, L);
    }
    void update(int x, Lazy L){
    	return update(x, x, L);
    }
    Node query(int x, int y){
    	acc = n_zero;
    	query(1, 1, N, x, y);
    	return acc;
    }
    pair<int, Node> greedy(Node target, bool (*cmp)(Node, Node)){
    	if ( cmp(aint[1], target) )
    		return make_pair( N, arbint(1, 1, N) );
    	int pos = 1, st = 1, dr = N;
	Node ans = n_zero;
    	while (st != dr){
    		int m = (st + dr) >> 1;
    		pos <<= 1;
    		Node part = comp( ans, arbint(pos, st, m) );
    		if ( cmp(part, target) ){
    			ans = part;
    			st = m + 1;
    			pos++;
    		} else
    			dr = m;
    	}
    	return make_pair(st - 1, ans);
    }
};

inline int get_max(int a, int b){
	return a > b ? a : b;
}
int apply(int node, int x, int y, int val){
	return val != 0 ? val : node;
}

Aint<int, get_max, int, apply, 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;
}