Cod sursa(job #843783)

Utilizator matei_cChristescu Matei matei_c Data 28 decembrie 2012 13:58:21
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define maxarb ( 1 << 19 )

int arbore[maxarb] ;

int poz, val ;
int sol ;
int start, finish ;
 
void modifica(int nod, int st, int dr)
{
    if( st == dr )
        arbore[nod] = val ;
    else
    {
        int mij = ( st + dr ) >> 1 ;
		
        if( poz <= mij )
            modifica( 2 * nod, st, mij ) ;
        else
            modifica( 2 * nod + 1, mij + 1, dr ) ;
		
        arbore[nod] = max( arbore[ 2 * nod ], arbore[ 2 * nod + 1 ] ) ;
    }
}
 
void query(int nod, int st, int dr)
{
    if( start <= st && dr <= finish )
    {
        if( arbore[nod] > sol )
            sol = arbore[nod] ;
    }
    else
    {
        int mij = ( st + dr ) >> 1 ;
		
        if( start <= mij )
            query( 2 * nod, st, mij ) ;
        if( finish > mij )
            query( 2 * nod + 1, mij + 1, dr ) ;
    }
}
 
int main()
{
	
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    
	int n, m, cod ;
	
	scanf("%d%d", &n, &m );
	
    for(int i = 1; i <= n; ++i )
    {
        scanf("%d", &val);
        poz = i ;
        modifica( 1, 1, n ) ;
    }
	
    for(int i = 1; i <= m; ++i )
    {
        scanf("%d", &cod );
		
        if( cod )
        {
            scanf("%d%d", &poz, &val ) ;
            modifica( 1, 1, n ) ;
        }
        else
        {
            scanf("%d%d", &start, &finish ) ;
            sol = 0 ;
			
			query( 1, 1, n ) ;
            printf("%d\n", sol );
        }
    }
	
    return 0 ;
	
}