Cod sursa(job #2684937)

Utilizator marquiswarrenMajor Marquis Warren marquiswarren Data 15 decembrie 2020 11:20:53
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 100001;
int n, m, arr[NMAX];

struct SegmentTree {
	int leftmost, rightmost;
	int value;
	SegmentTree *left, *right;

	SegmentTree(int leftmost, int rightmost, int array[]) : leftmost(leftmost), rightmost(rightmost) {
		if (leftmost == rightmost) {
			value = array[leftmost];
		} else {
			int mid = leftmost + (rightmost - leftmost) / 2;
			left = new SegmentTree(leftmost, mid, array);
			right = new SegmentTree(mid + 1, rightmost, array);
			recompute();
		}
	}

	void recompute() {
		if (leftmost == rightmost) return;
		value = max(left->value, right->value);
	}

	void update(int index, int newValue) {
		if (leftmost == rightmost) {
			value = newValue;
			return;
		}

		if (index <= left->rightmost) {
			left->update(index, newValue);
		} else {
			right->update(index, newValue);
		}

		recompute();
	}

	int query(int l, int r) {
		if (l > rightmost || r < leftmost) return INT_MIN;

		if (l <= leftmost && r >= rightmost) return value;

		return max(left->query(l, r), right->query(l, r));
	}
};

int main() {
	freopen("arbint.in", "r", stdin);
	freopen("arbint.out", "w", stdout);

	scanf("%d %d ", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf("%d ", &arr[i]);

	SegmentTree *tree = new SegmentTree(1, n, arr);

	while (m--) {
		int type, a, b;
		scanf("%d %d %d ", &type, &a, &b);
		if (type == 0)
			printf("%d\n", tree->query(a, b));
		else
			tree->update(a, b);
	}
}