Cod sursa(job #634337)

Utilizator mihaibogdan10Mihai Bogdan mihaibogdan10 Data 15 noiembrie 2011 23:03:06
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include<cstdio>
#include<vector>
#define Check() if (++pos == 100005) {fread (buff, 1, 100005, stdin); pos = 0;}
#define max(a,b) a>b ? a : b
using namespace std;

int n, m, valMax, pos;
int arb[300001];
char buff[100005];

void ReadInt(int &nr){
	nr = 0;
	while (buff[pos] < '0' || buff[pos] > '9')
		Check();
	while (buff[pos] >= '0' && buff[pos] <= '9'){
		nr = nr * 10 + (buff[pos] - '0');
		Check();
	}
}

void Update(int nod, int st, int dr, int poz, int val){
	if (st == dr) {arb[nod] = val; return;}
	int mij = (st + dr) / 2;
	if (poz <= mij) Update(2*nod, st, mij, poz, val);
	else Update(2*nod + 1, mij + 1, dr, poz, val);
	arb[nod] = max(arb[2*nod], arb[2*nod+1]);
}

void Query(int nod, int st, int dr, int &first, int &last){
	if (first <= st && dr <= last){
		if (valMax < arb[nod]) valMax = arb[nod]; 
		return;
	}
	int mij = (st + dr) / 2;
	if (first <= mij) Query(nod * 2, st, mij, first, last);
	if (mij < last) Query(nod * 2 + 1, mij + 1, dr, first, last);
}

int main(){
	freopen ("arbint.in", "r", stdin);
	freopen ("arbint.out", "w", stdout);
	
	ReadInt(n); ReadInt(m);
	int i, tip, a, b;
	for (i = 1; i <= n; i++) {
		ReadInt(a);
		Update(1, 1, n, i, a);
	}
	
	for (i = 0; i < m; i++){
		ReadInt(tip); ReadInt(a); ReadInt(b);
		if (tip == 1)	Update(1, 1, n, a, b);
		else {
			valMax = -1;
			Query(1, 1, n, a, b);
			printf ("%d\n", valMax);
		}
	}
	
	return 0;
}