Cod sursa(job #157364)

Utilizator marcelcodreaCodrea Marcel marcelcodrea Data 12 martie 2008 23:18:51
Problema Arbori de intervale Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include<stdio.h>
int a[100001],m[410000],tip,p1,p2,n,i,mm;
int inita(int nod,int b,int e)
{
  if (b==e)
     m[nod]=b;
     else
       {
       inita(2*nod,b,(b+e)/2);
       inita(2*nod+1,(b+e)/2+1,e);
       if (a[m[2*nod]]<a[m[2*nod+1]]) m[nod]=m[2*nod+1];
                                 else m[nod]=m[2*nod];
       }
  return 0;
}
int query(int nod,int b, int e, int s,int d)
{
   int maxs,maxd;
  if (e<s||d<b) return -1;
   else
  if (s<=b&&e<=d) return m[nod];
   else
    {
     maxs=query(2*nod,b,(b+e)/2,s,d);
     maxd=query(2*nod+1,(b+e)/2+1,e,s,d);
     if (maxs==-1) return maxd;
     if (maxd==-1) return maxs;
     if (a[maxs]>a[maxd]) return maxs;
                     else return maxd;
    }
}
int update(int nod,int b,int e)
{
  if (b==e)
   m[nod]=b;
   else
  {
  update(2*nod,b,(b+e)/2);
  update(2*nod+1,(b+e)/2+1,e);
  if (a[m[2*nod]]>a[m[2*nod+1]]) m[nod]=m[2*nod];
                           else  m[nod]=m[2*nod+1];
  }
}
int main()
{
  freopen("arbint.in","r",stdin);
  freopen("arbint.out","w",stdout);
  scanf("%ld %ld",&n,&mm);
  for(i=1;i<=n;i++)
   scanf("%ld",&a[i]);
  inita(1,1,n);
  for(i=1;i<=mm;i++)
    {
      scanf("%ld %ld %ld",&tip,&p1,&p2);
      if (!tip)
       printf("%ld\n",a[query(1,1,n,p1,p2)]);
      if (tip)
       {
       a[p1]=p2;
       update(1,1,n);
       }
    }
  return 0;
}