Cod sursa(job #156348)

Utilizator cos_minBondane Cosmin cos_min Data 12 martie 2008 14:46:12
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.7 kb
#include <cstdio>
#include <algorithm>
#define MaxN 101000
#define BUCKET 333
#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 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];
	printf("%u\n", sol);
}

int main(){
	freopen("arbint.in", "r", stdin);
	freopen("arbint.out", "w", stdout);
	cit(n); cit(m);
	//scanf("%d%d", &n, &m);
	for (unsigned i=0; i<n; i++){
		cit(val[i]);
		//scanf("%d", &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);
	//	scanf("%d%d%d", &op, &a, &b);
		if (op==1) update(a-1, b);
			else query(a-1, b-1);
	}
}