Cod sursa(job #1749173)

Utilizator delia_ioanaCeapa Delia Ioana delia_ioana Data 27 august 2016 22:58:52
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.68 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <climits>
#include <math.h>
#define dim 100001
 
using namespace std;
int m, n, aux, start = 0, p1, p2, arb_size, a, b, pos, val;
ifstream infile("arbint.in"); 
ofstream outfile("arbint.out"); 

void constructTree (vector<int> &arbore, int start, int end, int poz) {
	if (start == end) {
		arbore[poz] = val;
		return;
	}
	int mid = (start + end) / 2;
	if (pos <= mid) 
		constructTree(arbore, start, mid, poz * 2 + 1);
	else constructTree(arbore, mid + 1, end, poz * 2 + 2);
	arbore[poz] = max(arbore[poz * 2 + 1], arbore[poz * 2 + 2]);
} 

int findMax (vector<int> &arbore, int start, int end, int poz) {
	if (start >= a && b >= end)
		return arbore[poz];
	if (b < start || a > end)
		return INT_MIN;
	int mid = (start + end) / 2;
	return max(findMax(arbore, start, mid, poz * 2 + 1), findMax(arbore, mid + 1, end, poz * 2 + 2));
}

int nextPow2 ()
{
	if (n < 0)
	        return 0;
	-- n;
	n |= n >> 1;
	n |= n >> 2;
	n |= n >> 4;
	n |= n >> 8;
	n |= n >> 16;
	return n + 1;
}

int main() {
	infile >> n >> m;
	int save = n;
	arb_size = nextPow2() * 2 - 1;
	vector<int> arbore(arb_size);
	n = save;

	for (int i = 0; i < n; i ++) {
		infile >> aux;
		pos = i;
		val = aux;
		constructTree (arbore, 0, n - 1, 0);
	}

	infile >> aux;
	while (aux == 1) {
		infile >> p1 >> p2 >> aux;
		pos = p1 - 1;
		val = p2;
		constructTree (arbore, 0, n - 1, 0);
		start ++;
	}
	
	for (int i = start; i < m; i ++) {
		infile >> p1 >> p2;
		a = p1 - 1;
		b = p2 - 1;
		if (aux == 0)
			outfile << findMax(arbore, 0, n - 1, 0) << endl;	
		else {
			pos = p1 - 1;
			val = p2;
			constructTree (arbore, 0, n - 1, 0);
		}
		if (i != m - 1)
			infile >> aux;
	}

	return 0;
}