Cod sursa(job #2884061)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 2 aprilie 2022 12:44:43
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.57 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x <<": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "file";
// ifstream fin(fn + ".in");
// ofstream fout(fn + ".out");

class InP {

	FILE *fin;
	char *buff;
	int sp;

public:

	InP(const char *c) {
		fin = fopen(c, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	char read_ch() {
		sp++;
		if (sp == 4096) {
			fread(buff, 1, 4096, fin);
			sp = 0;
		}

		return buff[sp];
	}

	InP& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');

		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			n = 0;
		}
		else {
			n = c - '0';
		}

		while (isdigit(c = read_ch()))
			n = n * 10 + c - '0';
		n = n * sgn;
		return *this;
	}

};
InP fin("arbint.in");

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

public:
	OutP(const char *c) {
		fout = fopen(c, "w");
		buff = new char[50000]();
		sp = 0;
	}

	~OutP() {
		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;
	}

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

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

	OutP& operator <<(char *c) {
		while (*c) {
			write_ch(*c);
			c++;
		}
		return (*this);
	}
};
OutP fout("arbint.out");

int n, m;
int a[400005];
void build(int nod, int st, int dr) {
	if (st == dr) {
		fin >> a[nod];
	} else {
		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 x) {
	if (st == dr) {
		a[nod] = x;
	}
	else {
		int mid = (st + dr) >> 1;
		if (p <= mid)
			upd(nod << 1, st, mid, p, x);
		else upd(nod << 1 | 1, mid + 1, dr, p, x);
		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--) {
		int op, x, y;
		fin >> op >> x >> y;
		if (op == 0)
			fout << qry(1, 1, n, x, y) << '\n';
		else upd(1, 1, n, x, y);

	}
	return 0;
}