Cod sursa(job #2419538)

Utilizator rainerretzler rainer Data 8 mai 2019 19:40:53
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.78 kb
// bril.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"


using namespace std;

#define min(a,b) (a>b?b:a)
#define max(a,b) (a<b?b:a)


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




struct nod {
	int ma, left, right;
	nod *leftNod, *rightNod;
};

nod *root;
int n, m;


void constr(nod *no) {
	if (no->left == no->right) {
		fin >> no->ma;
		no->rightNod = NULL;
		no->leftNod = NULL;
	}
	else {
		nod *le = new nod();
		nod *ri = new nod();
		no->leftNod = le;
		no->rightNod = ri;
		int mid = (no->left + no->right) / 2;
		le->left = no->left;
		ri->right = no->right;
		if (no->right - no->left != 1) {
			le->right = mid;
			ri->left = mid + 1;
		}
		else {
			le->right = no->left;
			ri->left = no->right;
		}
		constr(le);
		constr(ri);
		no->ma = max(le->ma, ri->ma);
	}
}


void do1(nod *no, int a, int b) {
	if (no->left == a&&no->right == a)
		no->ma = b;
	else {
		int mid = (no->left + no->right) / 2;
		if (a <= mid)
			do1(no->leftNod, a, b);
		else
			do1(no->rightNod, a, b);
		no->ma = max(no->leftNod->ma, no->rightNod->ma);
	}
}

int do0(nod *no, int a, int b) {
	if (no->left == a&&no->right == b)
		return no->ma;
	int mid = (no->left + no->right) / 2;
	if (b <= mid)
		return do0(no->leftNod, a, b);
	if (a <= mid&&b > mid)
		return max(do0(no->leftNod, a, mid), do0(no->rightNod,mid+1,b));
	if (a > mid)
		return do0(no->rightNod, a, b);
	return 0;

}


int main()
{
	fin >> n >> m;
	root = new nod();
	root->left = 1;
	root->right = n;
	constr(root);
	int x, y, z;
	for (int i = 1; i <= m; ++i) {
		fin >> x >> y >> z;
		if (x == 0)
			fout << do0(root,y,z)<<"\n";
		else
			do1(root, y, z);
	}
	fin.close();
	fout.close();
    return 0;
}