Cod sursa(job #1453701)

Utilizator andreiulianAndrei andreiulian Data 24 iunie 2015 11:30:22
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
int arbmax[400005],ch[400005],N,M,val,a,b;
void update(int nod, int st, int dr);
int interogare(int nod, int st, int dr);
int main(){
	ifstream in("arbint.in");
	ofstream out("arbint.out");
	in>>N>>M;
	int i,c;
	for(i=1;i<400000;++i) ch[i]=-1;
	for(i=1;i<=N;++i){
		in>>val;
		a=i, b=i;
		update(1,1,N);
	}
	for(i=1;i<=M;++i){
		in>>c;
		if(c==1){
			in>>a;
			b=a;
			in>>val;
			update(1,1,N);
		}
		else{
			in>>a>>b;
			out<<interogare(1,1,N)<<'\n';
		}
	}
}
void update(int nod, int st, int dr){
	if(a<=st && dr<=b) arbmax[nod]=val,ch[nod]=val;
	else{
		if(ch[nod]>-1) ch[nod*2]=ch[nod*2+1]=ch[nod];
		ch[nod]=-1;
		int m=(st+dr)/2;
		if(a<=m) update(nod*2, st, m);
		if(b>m) update(nod*2+1, m+1, dr);
		arbmax[nod]=max(arbmax[nod*2], arbmax[nod*2+1]);
	}
}

int interogare(int nod, int st, int dr)
{
	if(a<=st && dr<=b) {return arbmax[nod];}
	else{
		int m=(st+dr)/2;
		int x=-1,y=-1;
		if(a<=m) {if(ch[nod*2]>-1) {x=ch[nod*2];}
			else {x=interogare(nod*2, st, m);  } }
		if(m<b) {if(ch[nod*2+1]>-1) y=ch[nod*2+1];
			else y=interogare(nod*2+1, m+1, dr);}
		int r=max(x,y);
		return r;
	}
}