Nu aveti permisiuni pentru a descarca fisierul grader_test19.in
Cod sursa(job #1969924)
Utilizator | Data | 18 aprilie 2017 18:49:52 | |
---|---|---|---|
Problema | Arbori de intervale | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.44 kb |
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin ("arbint.in");
ofstream fout ("arbint.out");
const int maxn = 1e5 + 5;
int B[maxn], V[maxn], rad, n, q;
inline int Bucket(int i) {
return i / rad;
}
inline int First(int i) {
return i * rad;
}
void Update (int pos, int val) {
int i = Bucket(pos), j;
if (V[pos] == B[i]) {
V[pos] = 0;
B[i] = 0;
for (j = First(i); j < First(i + 1); j++) {
B[i] = max(B[i], V[j]);
}
}
V[pos] = val;
B[i] = max(B[i], val);
}
int Query (int x, int y) {
int ans = 0, i;
int left = Bucket(x);
int right = Bucket(y);
for (i = left + 1; i < right; i++) {
ans = max(ans, B[i]);
}
if (ans < B[left]) {
for (i = x; i <= y && i < First(left + 1); i++) {
ans = max(ans, V[i]);
}
}
if (ans < B[right]) {
for (i = max(First(right), x); i <= y; i++) {
ans = max(ans, V[i]);
}
}
return ans;
}
int main() {
ios_base :: sync_with_stdio(false);
int op, x, y;
fin >> n >> q;
rad = sqrt(n);
for (int i = 1; i <= n; i++) {
fin >> V[i];
if (V[i] > B[Bucket(i)]) B[Bucket(i)] = V[i];
}
while (q--) {
fin >> op >> x >> y;
if (op == 1) Update(x, y);
else fout << Query(x, y) << "\n";
}
return 0;
}