Cod sursa(job #1370929)

Utilizator abel1090Abel Putovits abel1090 Data 3 martie 2015 18:10:07
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
///INTERVAL TREE
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

void update(vector<int>& intTree, int tInd, 
		const int vInd, const int val, int x, int y) {
	if(x==y) {
		intTree[tInd] = val;
		return;
	}	

	int m = (x+y)/2;
	if(vInd <= m) 	
		update(intTree, 2*tInd, vInd, val, x, m);
	else
		update(intTree, 2*tInd+1, vInd, val, m+1, y);

	intTree[tInd] = max(intTree[2*tInd], intTree[2*tInd+1]);
}

int query(vector<int>& intTree, int tInd, const int a, const int b, int x,  int y) {
	if(a<=x && y<=b) {
		return intTree[tInd];
	}

	int m = (x+y)/2;
	int right = 0, left = 0;
	
	if(a <= m && x<=m) 
		left = query(intTree, 2*tInd, a, b, x, m);
	if(b > m && m+1<=y)
		right = query(intTree, 2*tInd+1, a, b, m+1, y);

	return max(left, right);
}

int main() {
	ifstream inStr("arbint.in");
	ofstream outStr("arbint.out");

	int N, M;
	inStr >> N >> M;
	
	vector<int> intTree(4*N, 0);

	int x;
	for(int i=1; i<=N; ++i) {
		inStr >> x;
		update(intTree, 1, i, x, 1, N);
	}	
	
	int tp, a, b;
	for(int i=0; i<M; ++i) {
		inStr >> tp >> a >> b;
		if(!tp) outStr << query(intTree, 1, a, b, 1, N) << '\n';
		else update(intTree, 1, a, b, 1, N);
	}
	
	return 0;
}