Cod sursa(job #2986392)

Utilizator TudosieRazvanTudosie Marius-Razvan TudosieRazvan Data 28 februarie 2023 15:41:58
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.89 kb
#include <cstdio>
#include <cmath>
#include <climits>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <bitset>
#include <unordered_map>
#include <cstring>
#include <algorithm>

#define NMAX 100003

using namespace std;


FILE* fin, * fout;

int n,m;
int v[NMAX];
int arb[4 * NMAX+3];
int maxim;

void build(int nod, int st, int dr)
{
	if (st == dr)
	{
		//sunt intr-o singura casuta
		arb[nod] = v[st];
	}
	else {
		int mij = (st + dr) / 2;
		build(2 * nod, st, mij);
		build(2 * nod + 1, mij + 1, dr);

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

void update(int nod, int st, int dr,int poz,int val)
{
	if (st == dr)
	{
		//sunt in casuta poz
		arb[nod] = val;
	}
	else {
		int mij = (st + dr) / 2;
		if (poz <= mij)
		{
			update(2 * nod, st, mij,poz,val);
		}
		else if(mij+1<=poz)
		{
			update(2 * nod+1, mij+1, dr, poz, val);
		}
		arb[nod] = max(arb[2 * nod], arb[2 * nod + 1]);
	}
}

void query(int nod, int st, int dr, int qSt, int qDr)
{
	if (qSt<=st && dr<=qDr)
	{
		//sunt in casuta poz
		maxim = max(maxim, arb[nod]);
	}
	else {
		int mij = (st + dr) / 2;
		if (qSt<=mij)
		{
			query(2 * nod, st, mij, qSt, qDr);
		}
		if (mij +1<=qDr)
		{
			query(2 * nod + 1, mij + 1, dr, qSt, qDr);
		}
	}
}

int main()
{
	fin = fopen("arbint.in", "r");
	fout = fopen("arbint.out", "w");

	fscanf(fin, "%d %d", &n, &m);
	for (int i = 1; i <= n; i++)
	{
		fscanf(fin, "%d", &v[i]);
	}
	build(1, 1, n);

	for (int i = 1; i <= m; i++)
	{
		int op;
		fscanf(fin, "%d", &op);
		if (op == 1)
		{
			int poz, val;
			fscanf(fin, "%d %d", &poz, &val);
			v[poz] = val;
			update(1, 1, n, poz, val);
		}
		else {
			//query
			maxim = INT_MIN;
			int st, dr;
			fscanf(fin, "%d %d", &st, &dr);
			query(1, 1, n, st, dr);
			fprintf(fout, "%d\n", maxim);
		}
	}
	return 0;
}