Cod sursa(job #1541577)

Utilizator gabi.cristacheGabi Cristache gabi.cristache Data 4 decembrie 2015 12:53:51
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
// infoarenaDFSnonRec.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <fstream>
#include <vector>
#include <assert.h>

#define MaxN 100001

using namespace std;

ifstream fin("arbint.in");
ofstream fout("arbint.out");

int N, M, op, a, b;
int arb[MaxN * 4 + 66];
int arr[MaxN];

int max(int a, int b) {
	return a > b ? a : b;
}

void update(int node, int left, int right, int pos, int value) {
	if (left == right) {
		assert(left == pos);
		arb[node] = value;
		return;
	}

	int mid = left + (right - left) / 2;
	
	if (pos <= mid) update(2 * node, left, mid, pos, value);
	else			update(2 * node + 1, mid + 1, right, pos, value);

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

void query(int node, int left, int right, int start, int end, int& sol) {
	if (start <= left && right <= end) {
		sol = max(sol, arb[node]);
		return;
	}

	int mid = left + (right - left) / 2;

	if (start <= mid) query(2 * node, left, mid, start, end, sol);
	if (mid < end)    query(2 * node + 1, mid + 1, right, start, end, sol);
}

int main()
{
	fin >> N >> M;
	for (int i = 1; i <= N; ++i) {
		fin >> arr[i];
		update(1, 1, N, i, arr[i]);
	}

	for (int i = 0; i < M; ++i) {
		fin >> op >> a >> b;

		if (op == 0) {
			int max = -1;
			query(1, 1, N, a, b, max);
			fout << max << '\n';
		}
		else {
			update(1, 1, N, a, b);
		}
	}

	return 0;
}