Cod sursa(job #210077)

Utilizator vlad_popaVlad Popa vlad_popa Data 26 septembrie 2008 13:11:45
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <cstdio>
#include <algorithm>

using namespace std;

int N, M;
int V[100001];
int A[1<<18];
int a, b;
int rez;

#define ns (nod<<1)
#define nd (ns + 1)
#define mj ((st + dr) >> 1)

void make (int nod, int st, int dr){
	if (st >= dr) A[nod] = V[st];
	else{
		make (ns, st, mj);
		make (nd, mj + 1, dr);

		A[nod] = max (A[ns], A[nd]);
	}	
}

void update (int nod, int st, int dr){
	if (st >= dr) A[nod] = b;
	else{
		if (mj >= a) update (ns, st, mj);
		else update (nd, mj + 1, dr);

        A[nod] = max (A[ns], A[nd]); 
	}
}

void query (int nod, int st, int dr){
	if (a <= st && dr <= b) rez = max (rez, A[nod]);
	else{
		if (a <= mj) query (ns, st, mj);
		if (b > mj) query (nd, mj + 1, dr);
	}
}

void read (){
	int i, t;
	scanf ("%d %d\n", &N, &M);
	for (i = 1; i <= N; ++ i) scanf (" %d", V + i);
	make (1, 1, N);

	for (i = 1; i <= M; ++ i){
		scanf ("%d %d %d\n", &t, &a, &b); rez = -1;
		if (t) update (1, 1, N);
		else query (1, 1, N), printf ("%d\n", rez); 
	}
}

int main (){
	freopen ("arbint.in", "r", stdin);
	freopen ("arbint.out", "w", stdout);

	read ();

	return 0;
}