Pagini recente » Cod sursa (job #2014404) | Cod sursa (job #531091) | Cod sursa (job #993884) | Cod sursa (job #1044945) | Cod sursa (job #2025968)
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
ifstream f("arbint.in");
ofstream g("arbint.out");
const int N = 1e5; // limit for array size
int q,a,b,n,m; // array size
int t[2 * N];
int combine(int a,int b)
{
return max(a,b);
}
void build() // build the tree
{
for (int i = n - 1; i > 0; i--)
{
t[i] = combine( t[i*2] , t[i*2+1] );
//cout<<i<<' '<<t[i]<<'\n';
}
}
void modify(int p, int value) // set value at position p
{
for (t[p += n-1] = value; p > 1; p /= 2)
t[p/2] = combine( t[p] , t[p^1] );
}
int query(int l, int r) //solution on interval [l, r)
{
int Sol = -1;
l += n-1;
r += n-1;
while( l < r )
{
if( l%2 == 1 )
Sol = combine( Sol, t[l++] );
if( r%2 == 0 )
Sol = combine( Sol, t[r--] );
l/=2;
r/=2;
}
Sol = combine( Sol, t[l]);
return Sol;
}
int main()
{
f>>n>>m;
for(int i = 0 ; i < n ; i++)
{
f>>t[n+i];
//cout<<n+i<<' '<<t[n+i]<<'\n';
}
build();
for(int i = 0 ; i < m ; i++)
{
f>>q>>a>>b;
if ( q == 0 )
g << query(a,b) <<'\n';
else
modify(a,b);
}
return 0;
}