Cod sursa(job #2286119)

Utilizator marcudanfDaniel Marcu marcudanf Data 19 noiembrie 2018 20:31:51
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include <fstream>
#define mid (left+right)/2

const int NMAX = 1e5 + 5;

using namespace std;

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

int n, m, Max;
int v[NMAX];
int a[2*NMAX];
int pos[NMAX];

void search(int node, int x, int y, int left, int right){
	if(left > y or right < x)
		return;
	if(left >= x and right <= y){
		Max = max(Max, a[node]);
		return;
	}
	search(2 * node, x, y, left, mid);
	search(2 * node + 1, x, y, mid + 1, right);
}

void update_father(int node){
	if(!node)
		return;
	a[node] = max(a[2*node], a[2*node+1]);
	update_father(node/2);
}

void build(int node, int left, int right){
	if(left == right){
		a[node] = v[left];
		pos[left] = node;
		return;
	}
	build(2 * node, left, mid);
	build(2 * node + 1, mid + 1, right);
	a[node] = max(a[2*node], a[2*node+1]);
}

int main(){
	fin >> n >> m;
	for(int i = 1; i <= n; i++)
		fin >> v[i];
	build(1, 1, n);
	while(m--){
		int task, x, y;
		fin >> task >> x >> y;
		if(task == 0){
			Max = 0;
			search(1, x, y, 1, n);
			fout << Max << '\n';
		}else{
			a[pos[x]] = y;
			update_father(pos[x]/2);
		}
	}
	return 0;
}