Cod sursa(job #2867503)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 10 martie 2022 13:27:43
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.1 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define dbg(x) cout << #x <<": " << x << "\n";
using ll = long long;

class InP {
	FILE *fin;
	char *buff;
	int sp;

public:
	InP(const char *p) {
		fin = fopen(p, "r");
		buff = new char[4096]();
		sp = 4095;
	}
	~InP() {
		fclose(fin);
	}
	char read_ch() {
		sp++;
		if (sp == 4096) {
			fread(buff, 1, 4096, fin);
			sp = 0;
		}
		return buff[sp];
	}

	InP &operator >>(int &n) {
		char c;
		int sgn = 1;
		while (!isdigit(c = read_ch()) && c != '-');
		if (c == '-') {
			n = 0;
			sgn = -1;
		}
		else n = c - '0';
		while (isdigit(c = read_ch()))
			n = n * 10 + c - '0';
		n = n * sgn;
		return *this;
	}

	InP &operator >>(ll &n) {
		char c;
		int sgn = 1;
		while (!isdigit(c = read_ch()) && c != '-');
		if (c == '-') {
			n = 0;
			sgn = -1;
		}
		else n = c - '0';
		while (isdigit(c = read_ch()))
			n = n * 10LL + c - '0';
		n = n * sgn;
		return *this;
	}

	InP &operator >> (char &n) {
		n = read_ch();
		// while ((n = read_ch()) != '\n' && n != ' ');
		return *this;
	}

};

class OuP {
	FILE *fout;
	char *buff;
	int sp;

public:

	OuP(const char *p) {
		fout = fopen(p, "w");
		buff = new char[50000];
		sp = 0;
	}
	~OuP() {
		fwrite(buff, 1, sp, fout);
		fclose(fout);
	}
	void write_ch(char c) {
		if (sp == 50000) {
			fwrite(buff, 1, sp, fout);
			sp = 0;
		}
		buff[sp++] = c;
	}

	OuP &operator <<(int n) {
		if (n <= 9)
			write_ch(n + '0');
		else {
			*(this) << (n / 10);
			write_ch(n % 10 + '0');
		}
		return *this;
	}

	OuP &operator <<(ll n) {
		if (n <= 9)
			write_ch(n + '0');
		else {
			*(this) << (n / 10);
			write_ch(n % 10 + '0');
		}
		return *this;
	}

	OuP &operator <<(char c) {
		write_ch(c);
		return *this;
	}

	OuP &operator <<(const char *c) {
		while (*c) {
			write_ch(*c);
			c++;
		}
		return *this;
	}

};

// InP fin("file.in");
// OuP fout("file.out");

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

int n, m;
int op, x, y;
int a[400005];
void build(int nod, int st, int dr) {
	if (st == dr) {
		fin >> a[nod];
		return;
	}
	int mid = (st + dr) >> 1;
	build(nod << 1, st, mid);
	build(nod << 1 | 1, mid + 1, dr);
	a[nod] = max(a[nod << 1], a[nod << 1 | 1]);
}

void upd(int nod, int st, int dr,  int p , int val) {
	if (st == p) {
		a[nod] = val;
		return;
	}
	int mid = (st + dr) >> 1;
	if (p <= mid)
		upd(nod << 1, st, mid, p, val);
	else upd(nod << 1 | 1, mid + 1, dr, p, val);
	a[nod] = max(a[nod << 1], a[nod << 1 | 1]);
}

int qry(int nod, int st, int dr, int x, int y) {
	if (st > dr || st > y || dr < x)
		return -1;
	if (st >= x && dr <= y)
		return a[nod];
	int mid = (st + dr) >> 1;
	int q1 = qry(nod << 1, st, mid , x, y);
	int q2 = qry(nod << 1 | 1, mid + 1, dr, x, y);
	return max(q1, q2);
}


int main() {

	fin >> n >> m;
	build(1, 1, n);
	while (m--) {
		fin >> op >> x >> y;
		if (op == 0)
			fout << qry(1, 1, n, x, y) << '\n';
		upd(1, 1, n, x, y);
	}




// fin.close();
// fout.close();
	return 0;
}