Cod sursa(job #1877624)

Utilizator TeodorCotetCotet Teodor TeodorCotet Data 13 februarie 2017 16:50:57
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 1e5 + 5;
const int INF = 0x3f3f3f3f;

int n; int m;
int aib[NMAX];
int v[NMAX];

int query(int, int);

int bit(int x) { return x & -x; }

void change(int pos, int value) {

	v[pos] = value;

	for(int x = pos ; x <= n ; x += bit(x)) {

		if(aib[x] == pos) {

			int newMaxim = query(x - bit(x) + 1, x - 1);//log N, special query
			aib[x] = v[x] > v[newMaxim] ? x : newMaxim;

		} else 
			aib[x] = v[pos] > v[aib[x]] ? pos : aib[x];
	}
}

int query(int st, int dr) {

	int maxInd = 0;
	for(int pos = dr; pos >= st; ) {

		if(pos - bit(pos) + 1 < st) { //nu pot face query

			if(v[maxInd] < v[pos])
				maxInd = pos;
			pos--;
		} else {
			maxInd = v[maxInd] < v[aib[pos]] ? aib[pos] : maxInd;
			pos -= bit(pos);
		}
	}

	return maxInd;
}

int main() {

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

	cin >> n >> m;

	int a, b;

	for(int i = 1; i <= n ; ++i) {
		cin >> v[i];
		change(i, v[i]);
	}

	int t;

	aib[0] = -INF;

	for(int i = 1; i <= m ; ++i) {

		cin >> t >> a >> b;
		if(t == 0) cout << v[query(a, b)] << '\n';
		if(t == 1) change(a, b);
	}

	return 0;
}