Cod sursa(job #990380)

Utilizator harababurelPuscas Sergiu harababurel Data 28 august 2013 10:03:59
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <iostream>
#include <fstream>
#define nmax 100005
#define inf (1<<30)
using namespace std;

int arb[3*nmax], v[nmax];
int n, m, sol, op, x, y;

//int lo[nmax], hi[nmax];

bool intersection(int st, int dr, int a,  int b) {
	if(min(dr, b) >= max(st, a)) return true;
	return false;
}


void update(int nod, int st, int dr, int pos, int val) {
	int mij = (st + dr) >> 1;
	

	//lo[nod] = st;	//ca sa stiu ce interval caracterizeaza fiecare nod
	//hi[nod] = dr;

	if(st == dr) {
		arb[nod] = val;
		return;
	}

	if(pos <= mij) update(2*nod, st, mij, pos, val);
	else update(2*nod+1, mij+1, dr, pos, val);

	arb[nod] = max(arb[2*nod], arb[2*nod+1]);
}

int query(int nod, int st, int dr, int a, int b) {
	//<nod> reprezinta intervalul [<st>, <dr>]
	//trebuie sa updatez intervalul [<a>, <b>]

	int mij = (st + dr) >> 1;

	if(a <= st && b >= dr) return arb[nod];

	int leftmax = 0, rightmax = 0;
	if(intersection(st, mij, a, b))   leftmax  = query(2*nod, st, mij, a, b);
	if(intersection(mij+1, dr, a, b)) rightmax = query(2*nod+1, mij+1, dr, a, b);
	
	return max(leftmax, rightmax);
}

int main() {
	ifstream f("arbint.in");
	ofstream g("arbint.out");

	f>>n>>m;
	for(int i=1; i<=n; i++) {
		f>>v[i];
		update(1, 1, n, i, v[i]);
	}

	for(int i=1; i<=m; i++) {
		f>>op>>x>>y;
		if(op == 1) update(1, 1, n, x, y);
		else g<<query(1, 1, n, x, y)<<"\n";
	}

/*
	cout<<"\n\n";
	for(int i=1; i<=2*n; i++) {
		cout<<i<<"["<<lo[i]<<"->"<<hi[i]<<"]: "<<arb[i]<<"\n";
	}
*/
		

	return 0;
}