Pagini recente » Cod sursa (job #877145) | Cod sursa (job #737469) | Cod sursa (job #3130881) | Cod sursa (job #1640728) | Cod sursa (job #2223861)
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const string IN_FILE = "arbint.in";
const string OUT_FILE = "arbint.out";
const int INF = 0x3f3f3f3f;
class SegmentTree {
public:
SegmentTree(const vector<int>& values) {
for (size = 1; size < int(values.size()); size *= 2);
tree = vector<int>(2 * size, -INF);
for (int x = 0; x < int(values.size()); x++) {
tree[size + x] = values[x];
}
for (int x = size - 1; x > 0; x--) {
tree[x] = max(tree[2 * x], tree[2 * x + 1]);
}
}
void update(const int position, const int value) {
tree[position + size] = value;
for (int x = (position + size) / 2; x > 0; x /= 2) {
tree[x] = max(tree[2 * x], tree[2 * x + 1]);
}
}
int query(const int from, const int to) const {
int value = -INF;
int x = from + size;
int y = to + size;
while (x <= y) {
value = max(value, max(tree[x], tree[y]));
x = (x + 1) / 2;
y = (y - 1) / 2;
}
return value;
}
private:
int size;
vector<int> tree;
};
int main() {
ifstream in(IN_FILE);
ofstream out(OUT_FILE);
int n, m;
in >> n >> m;
auto values = vector<int>(n);
for (int i = 0; i < n; i++) {
in >> values[i];
}
auto tree = SegmentTree(values);
for (int i = 0; i < m; i++) {
int type;
in >> type;
if (type == 0) {
int from, to;
in >> from >> to;
out << tree.query(from - 1, to - 1) << "\n";
} else {
int position, value;
in >> position >> value;
tree.update(position - 1, value);
}
}
in.close();
out.close();
return 0;
}