Cod sursa(job #1623722)

Utilizator pas.andreiPopovici Andrei-Sorin pas.andrei Data 1 martie 2016 21:16:50
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.95 kb
#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <functional>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#define MOD 105011
#define NMAX 100005
#define INF 0x3f3f3f3f
#define pb push_back

using namespace std;

FILE *fin = freopen("arbint.in", "r", stdin);
FILE *fout = freopen("arbint.out", "w", stdout);

typedef pair<int, int> pii;

int v[NMAX];
int ai[4 * NMAX];
int ansQ;

void build(int poz, int st, int dr);
void query(int poz, int st, int dr, int qst, int qdr);
void update(int poz, int st, int dr, int x, int pozUpdate);

int main() {
	cin.sync_with_stdio(false);
	int n, m, i, q, a, b;

	cin >> n >> m;

	for (i = 1; i <= n; ++i)
		cin >> v[i];

	build(1, 1, n);

	for (i = 0; i < m; ++i) {
		cin >> q >> a >> b;

		if (q == 0) {
			ansQ = -1;
			query(1, 1, n, a, b);
			cout << ansQ << '\n';
		}
		else
			update(1, 1, n, b, a);
	}

	return 0;
}

void build(int poz, int st, int dr) {
	int mid = ((st + dr) >> 1), fs = (poz << 1);

	if (st == dr) {
		ai[poz] = v[st];
		return;
	}

	build(fs, st, mid); build(fs + 1, mid + 1, dr);
	ai[poz] = max(ai[fs], ai[fs + 1]);
}

void query(int poz, int st, int dr, int qst, int qdr) {
	int mid = ((st + dr) >> 1), fs = (poz << 1);

	if (st > qdr || dr < qst)
		return;
	if (qst <= st && qdr >= dr) {
		ansQ = max(ansQ, ai[poz]);
		return;
	}

	if (qst <= mid)
		query(fs, st, mid, qst, qdr);
	if (qdr > mid)
		query(fs + 1, mid + 1, dr, qst, qdr);
}

void update(int poz, int st, int dr, int x, int pozUpdate) {
	if (st == dr) {
		ai[poz] = x;
		return;
	}

	int mid = ((st + dr) >> 1), fs = (poz << 1);

	if (pozUpdate <= mid)
		update(fs, st, mid, x, pozUpdate);
	if (pozUpdate > mid)
		update(fs + 1, mid + 1, dr, x, pozUpdate);

	ai[poz] = max(ai[fs], ai[fs + 1]);
}