Cod sursa(job #1473349)

Utilizator theprdvtheprdv theprdv Data 19 august 2015 07:04:14
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.95 kb
#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#include <algorithm>
#include <ctime>
#define MaxN 101000
#define BUCKET 256
#define Dim (1<<13)
#define bucket(x) ((x)/BUCKET)
#define first(x) ((x)*BUCKET)

using namespace std;

unsigned n, m, poz;
unsigned val[MaxN], maxBuc[MaxN / BUCKET];
char lin[Dim];

inline void cit(unsigned &x){
	x = 0;
	while (lin[poz]<'0' || lin[poz]>'9'){
		poz++;
		if (poz == Dim) fread(lin, 1, Dim, stdin), poz = 0;
	}
	while (lin[poz] >= '0' && lin[poz] <= '9'){
		x = 10 * x + lin[poz++] - '0';
		if (poz == Dim) fread(lin, 1, Dim, stdin), poz = 0;
	}
}

inline void scrie(unsigned k){
	char lin[30], *s;
	s = lin + 29; s[0] = 0;
	do {
		unsigned cat = k / 10;
		s--;
		s[0] = k - cat * 10 + '0';
		k = cat;
	} while (k);
	puts(s);
}

inline void update(unsigned k, unsigned x){
	unsigned b = bucket(k);
	if (val[k] == maxBuc[b]){
		maxBuc[b] = val[k] = 0;
		for (unsigned i = first(b); i<first(b + 1); i++)
			if (val[i] > maxBuc[b]) maxBuc[b] = val[i];
	}
	val[k] = x;
	if (x>maxBuc[b]) maxBuc[b] = x;
}

inline void query(unsigned st, unsigned fn){
	unsigned b1 = bucket(st), b2 = bucket(fn);
	unsigned sol = 0;
	for (unsigned i = b1 + 1; i<b2; i++)
		if (maxBuc[i] > sol) sol = maxBuc[i];
	if (sol < maxBuc[b1])
		for (unsigned i = st; i<first(b1 + 1) && i <= fn; i++)
			if (val[i] > sol) sol = val[i];
	if (sol < maxBuc[b2])
		for (unsigned i = max(first(b2), st); i <= fn; i++)
			if (val[i] > sol) sol = val[i];
	//scrie(sol);
	printf("%d\n", sol);
}

int main(){
	freopen("tests/grader_test7.in", "r", stdin);
	freopen("data.out", "w", stdout);

	cit(n); cit(m);
	for (unsigned i = 0; i<n; i++){
		cit(val[i]);
		if (val[i] > maxBuc[bucket(i)]) maxBuc[bucket(i)] = val[i];
	}
	while (m--){
		unsigned op, a, b;
		cit(op); cit(a); cit(b);
		if (op == 1) update(a - 1, b);
		else query(a - 1, b - 1);
	}
	

	printf("%f\n", (float)clock() / CLOCKS_PER_SEC);
	return 0;
}