Pagini recente » Cod sursa (job #184250) | Cod sursa (job #435638) | Cod sursa (job #3206824) | Cod sursa (job #304526) | Cod sursa (job #2124838)
#include <iostream>
#include <fstream>
#include <climits>
using namespace std;
ifstream f("arbint.in" );
ofstream g("arbint.out");
struct NODE
{
int x;
int y;
int vmax;
NODE* parent;
NODE* p_left;
NODE* p_right;
} root;
int N, M, v[100003];
NODE* frunze[100003];
void BuildTree(NODE& nod, int first, int last)
{
nod.x = first;
nod.y = last;
if ( first >= last )
{
frunze[first] = &nod;
nod.vmax = v[first];
nod.p_left = NULL;
nod.p_right = NULL;
} else {
int m = (first+last)/2;
nod.p_left = new NODE;
nod.p_right = new NODE;
nod.p_left ->parent = &nod;
nod.p_right->parent = &nod;
BuildTree(*nod.p_left , first, m);
BuildTree(*nod.p_right, m+1, last);
}
if ( nod.p_left != NULL ) nod.vmax = max(nod.vmax, nod.p_left->vmax);
if ( nod.p_right != NULL ) nod.vmax = max(nod.vmax, nod.p_right->vmax);
}
int MaxValue(NODE& nod, int first, int last)
{
if ( first <= nod.x && nod.y <= last )
return nod.vmax;
int maxx = -1;
if ( first <= nod.p_left->y )
maxx = max(maxx, MaxValue(*nod.p_left, first, nod.p_left->y));
if ( last >= nod.p_right->x )
maxx = max(maxx, MaxValue(*nod.p_right, nod.p_right->x, last));
return maxx;
}
void Update(NODE* nod, int x)
{
nod->vmax = x;
if ( nod->p_left != NULL ) nod->vmax = max(nod->vmax, nod->p_left->vmax);
if ( nod->p_right != NULL ) nod->vmax = max(nod->vmax, nod->p_right->vmax);
if ( nod->parent != NULL ) Update(nod->parent, x);
}
int main()
{
f >> N >> M;
for ( int i=1; i<=N; i++ )
f >> v[i];
BuildTree(root, 1, N);
int OpType = 0, a, b;
for ( int i=1; i<=M; i++ )
{
f >> OpType >> a >> b;
if ( OpType == 0 )
g << MaxValue(root, a, b) << '\n';
else
{
Update(frunze[a], b);
}
}
}