Cod sursa(job #2419643)

Utilizator rainerretzler rainer Data 9 mai 2019 03:24:10
Problema Arbori de intervale Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.27 kb
// bril.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <fstream>
#include <cstdio>
#include <stdio.h>
#include<string.h>
#include<string>
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");









int noduri[270010];
int n, m,a,b;
int el[100010];
char pi[18000];
int x, y, z;

void inputEl() {
	
}

void splitXYZ() {
	int s = 0,p=0,t=0;
	while (pi[p] != '\0') {
		if (pi[p] == ' ') {
			if (t == 0) {
				x = s;
				s = 0;
				++t;
			}
			else {
				a = s;
				s = 0;
			}
		}
		else
			s = 10 * s + (pi[p] - '0');
		++p;
		
	}
	b = s;
}

void inP() {
	int d = n,s=0,k=0,i;
	while (d) {
		fin.getline(pi, 1 << 16,'\n');
		i = 0;
		while (pi[i] != '\0'&&d!=0) {
			if (pi[i] != ' ')
				s = 10 * s + (pi[i] - '0');
			else {
				++k;
				--d;
				el[k] = s;
				s = 0;
			}
			++i;
		}
		if (d == 1) {
			el[k+1] = s;
			d = 0;
		}
	}
}


void constr(int poz, int left, int right) {
	if (left == right) {
		noduri[poz] = el[left];

	}
	else {
		int mid = (left + right) / 2;
		constr(2 * poz, left, mid);
		constr(2 * poz+1, mid+1, right);
		
		noduri[poz] = max(noduri[2 * poz], noduri[2 * poz + 1]);
	}
}


void do1(int poz, int left, int right) {
	if (left == a&&right == a)
		noduri[poz] = b;
	else {
		int mid = (left + right) / 2;
		if (a <= mid)
			do1(2*poz, left, mid);
		else
			do1(2*poz+1, mid+1, right);
		noduri[poz] = max(noduri[2 * poz], noduri[2 * poz + 1]);
	}
}

int do0(int poz, int a, int b, int left, int right) {
	if (left == a&&right == b)
		return noduri[poz];
	int mid = (left + right) / 2;
	if (b <= mid)
		return do0(2*poz, a, b, left, mid);
	if (a <= mid&&b > mid)
		return max(do0(2 * poz, a, mid, left, mid), do0(2 * poz + 1,mid+1,b, mid+1, right));
	if (a > mid)
		return do0(2 * poz+1, a, b, mid+1, right);
	return 0;

}


int main()
{
	
	
	fin >> n >> m;
	string cc;
	getline(fin, cc);
	inP();
	constr(1,1,n);
	for (int i = 1; i <= m; ++i) {
		fin.getline(pi,200,'\n');
		splitXYZ();
		if (x == 0)
			fout << do0(1, a, b, 1, n) << "\n";
		else
			do1(1,1,n);
	}
	fin.close();
	fout.close();
    return 0;
}