Cod sursa(job #1473345)

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

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

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

void update(){
	int pos = read_int() - 1, value = read_int(), pos_buc = bucket(pos);
	
	if (MaxBuc[pos_buc] == val[pos]){
		val[pos] = value;
		MaxBuc[pos_buc] = 0;

		for (int i = first(pos); i < first(pos_buc + 1); ++i)
			MaxBuc[pos_buc] = MAX(val[i], MaxBuc[pos_buc]);
	}
	val[pos] = value;
}

inline int query(){
	int l = read_int() - 1, r = read_int() - 1, max = 0, Fbuc = bucket(l) + 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;
}