Pagini recente » Cod sursa (job #3280613) | Cod sursa (job #3280951) | Cod sursa (job #1295542) | Cod sursa (job #3171166) | Cod sursa (job #3202519)
#include <fstream>
#include <vector>
using namespace std;
struct Node {
int start, fn, mx;
Node *left, *right;
};
ifstream f("arbint.in");
ofstream g("arbint.out");
int n,a[100001];
Node* buildSegTree(int a[], int start, int fn) {
if (start == fn) {
Node* leaf = new Node();
leaf->start = start;
leaf->fn = fn;
leaf->mx = a[start];
leaf->left = leaf->right = nullptr;
return leaf;
}
Node* root = new Node();
root->start = start;
root->fn = fn;
int mid = start + (fn - start) / 2;
root->left = buildSegTree(a, start, mid);
root->right = buildSegTree(a, mid + 1, fn);
root->mx = max(root->left->mx , root->right->mx);
return root;
}
int query(Node* root, int start, int fn) {
if (root->start > fn || root->fn < start) return 0;
if (start <= root->start && root->fn <= fn) return root->mx;
return max(query(root->left, start, fn) , query(root->right, start, fn));
}
void update(Node* root, int index, int newValue) {
if (root->start == root->fn)
{
root->mx = newValue;
return;
}
int mid = root->start + (root->fn - root->start) / 2;
if (index <= mid)
update(root->left, index, newValue);
else
update(root->right, index, newValue);
root->mx = max ( root->left->mx , root->right->mx);
}
int q;
int main()
{
f>>n>>q;
for(int i=1;i<=n;i++)
f>>a[i];
Node* root = buildSegTree(a, 1, n);
while(q--)
{
int x,y,tip;
f>> tip>> x>> y;
if(tip == 0)
g << query(root, x, y) << '\n';
else
update(root, x ,y);
}
return 0;
}