Pagini recente » Cod sursa (job #2313033) | Cod sursa (job #22715) | Cod sursa (job #2671706) | Cod sursa (job #3190693) | Cod sursa (job #1947186)
#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) :
size(1),
tree(vector<int>()) {
for (; 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(int where, const int value) {
tree[where += size] = value;
for (where /= 2; where > 0; where /= 2) {
tree[where] = max(tree[2 * where], tree[2 * where + 1]);
}
}
int query(int from, int to) const {
int value = -INF;
from += size;
to += size;
while (from <= to) {
value = max(value, max(tree[from], tree[to]));
from = (from + 1) / 2;
to = (to - 1) / 2;
}
return value;
}
private:
int size;
vector<int> tree;
};
int main() {
ifstream in(IN_FILE);
ofstream out(OUT_FILE);
int n, q;
in >> n >> q;
auto values = vector<int>(n);
for (int i = 0; i < n; i++) {
in >> values[i];
}
auto tree = SegmentTree(values);
for (int i = 0; i < q; i++) {
int type;
in >> type;
if (type == 0) {
int from, to;
in >> from >> to;
out << tree.query(from - 1, to - 1) << "\n";
} else {
int where, value;
in >> where >> value;
tree.update(where - 1, value);
}
}
in.close();
out.close();
return 0;
}