Cod sursa(job #1473335)

Utilizator theprdvtheprdv theprdv Data 19 august 2015 05:10:42
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#define BUCKET 256
#define bucket(x) ((x) / BUCKET)
#define DIM (1 << 13)
#define MAXN 100000
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define first(x) ((x) * BUCKET)

int pos = DIM, N, M, val[MAXN], MaxBuc[MAXN / BUCKET];
char buf[DIM];
 
inline int read_int(){
	while (buf[pos] > '9' || buf[pos] < '0'){
		++pos;
		if (pos >= DIM) fread(buf, 1, DIM, stdin), pos = 0;
	}

	int val = 0;
	while (buf[pos] >= '0' && buf[pos] <= '9'){
		val = val * 10 + buf[pos++] - '0';
		if (pos == DIM) fread(buf, 1, DIM, stdin), pos = 0;
	}
	return val;
}

void update(){
	int pos = read_int() - 1, value = read_int(), pos_buc = bucket(pos);
	val[pos] = value;
	MaxBuc[pos_buc] = 0;
	for (int i = first(pos), j = 0; j < DIM; ++i, ++j) MaxBuc[i] = MAX(val[i], MaxBuc[pos_buc]);
}

int query(){
	int l = read_int() - 1, r = read_int() - 1, max = 0, Fbuc = bucket(r) + 1, Lbuc = bucket(r);

	for (int i = Fbuc; i < Lbuc; ++i) max = MAX(max, MaxBuc[i]);
	while (l <= r){
		if (l == first(Fbuc)) l = first(Lbuc);
		max = MAX(max, val[l]);
		++l;
	}

	return max;
}

int main(){
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
	
	N = read_int(), M = read_int();
	for (int i = 0; i < N; ++i){
		val[i] = read_int();
		MaxBuc[bucket(i)] = MAX(MaxBuc[bucket(i)], val[i]);
	}

	for (int i = 0; i < M; ++i)
		if (read_int()) update();
		else printf("%d\n", query());

   
    return 0;
}