Cod sursa(job #2739137)

Utilizator ardutgamerAndrei Bancila ardutgamer Data 6 aprilie 2021 22:09:48
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

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

const int NMAX = 200005;
int a[NMAX * 4], v[NMAX];

void build(int nod, int st, int dr)
{
	if (st == dr)
	{
		a[nod] = v[st];
		return;
	}
	int mij = (st + dr) / 2;
	build(2 * nod, st, mij);
	build(2 * nod + 1, mij + 1, dr);
	a[nod] = max(a[2 * nod], a[2 * nod + 1]);
}

void update(int nod, int st, int dr, int pos, int val)
{
	if (st == dr)
	{
		a[nod] = val;
		return;
	}
	int mij = (st + dr) / 2;
	if (pos <= mij)
		update(2 * nod, st, mij, pos, val);
	else
		update(2 * nod + 1, mij + 1, dr, pos, val);
	a[nod] = max(a[2 * nod], a[2 * nod + 1]);
}

int query(int nod, int st, int dr, int x, int y)
{
	if (dr < x || st > y)
		return INT_MIN;
	if (x <= st && dr <= y)
		return a[nod];
	int mij = (st + dr) / 2;
	return max(query(2 * nod, st, mij, x, mij), query(2 * nod + 1, mij + 1, dr, mij+1, y));
}

int n, q;

int main()
{
	ios_base::sync_with_stdio(false);
	fin.tie(0); fout.tie(0);
	fin >> n >> q;
	for (int i = 1; i <= n; i++)
		fin >> v[i];
	build(1, 1, n);
	while (q--)
	{
		int tip;
		fin >> tip;
		if (tip == 1)
		{
			int a, b;
			fin >> a >> b;
			update(1, 1, n, a, b);
		}
		else
		{
			int a, b;
			fin >> a >> b;
			fout << query(1, 1, n, a, b) << '\n';
		}
	}
	return 0;
}