Cod sursa(job #181906)

Utilizator mithyPopovici Adrian mithy Data 19 aprilie 2008 10:52:35
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
#include <stdio.h>
#define NMax 1000

int n, m, maxim;
int arbint[NMax*NMax];

int max( int a, int b )
{
   if ( a < b ) return b;
   return a;
}

void citire();
void update(int nod, int in, int sf, int poz, int val);
void query(int nod, int in, int sf, int a, int b);

int main()
{
   citire();
   return 0;
}

void update(int nod, int in, int sf, int poz, int val)
{
   if ( in == sf )
      arbint[nod] = val;
   else
   {
      int mij = (in+sf)/2;
      if ( poz <= mij )
         update( 2*nod, in, mij, poz, val);
      else
         update( 2*nod+1, mij+1, sf, poz, val);
      arbint[nod] = max(arbint[2*nod], arbint[2*nod+1]);
   }
}
void query(int nod, int in, int sf, int a, int b)
{
   if ( a<=in && sf <= b)
      maxim = max(maxim, arbint[nod]);
   else
   {
      int mij = (in+sf)/2;

      if ( a <= mij )
         query( 2*nod, in, mij, a, b );

      if ( mij+1 <= b )
         query( 2*nod+1, mij+1, sf, a, b );
   }
}


void citire()
{
   int i, x, y, op, aux;

   freopen( "arbint.in", "rt", stdin );
   freopen( "arbint.out", "wt", stdout );

   scanf( "%lld %lld", &n, &m );

   for (i=1; i<=n; i++)
   {
      scanf( "%lld", &aux );
      update(1,1,n,i,aux);
   }

   for (i=0; i<m; i++)
   {
      scanf( "%d %d %d", &op, &x, &y );

      switch (op)
      {
         case 0:

              maxim = -1;
              query(1, 1, n, x, y );

              printf( "%d\n", maxim );
              break;

         case 1:

              update(1, 1, n, x, y );
              break;

      }
   }
}