Cod sursa(job #190197)

Utilizator crusRus Cristian crus Data 20 mai 2008 18:47:01
Problema Arbori de intervale Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 1.57 kb
#include <stdio.h>
#define input "arbint.in"
#define output "arbint.out"
#define nmax 100001
long n,m;
long v[nmax],s[nmax+nmax],f[nmax+nmax];
void compute()
{
 int i,c=1,mij;
 s[1]=1; f[1]=n;
 for (i=1;i<n;i++)
     {
      mij=(s[i]+f[i])/2;
      c++;
      s[c]=s[i]; f[c]=mij;
      c++;
      s[c]=mij+1; f[c]=f[i];
     }
}
void fill(long pz,long val,long p)
{
 int mij;
 if (s[pz]==f[pz]) v[pz]=val;
    else
     {
      if (v[pz]<val) v[pz]=val;
      mij=(s[pz]+f[pz])/2;
      if (p<=mij) fill(2*pz,val,p);
	 else fill(2*pz+1,val,p);
     }
}
int cs(long a,long b,long pz)
{
 if (a==s[pz] && b>=f[pz]) return v[pz];
    else
    if (a<=f[2*pz]) return cs(a,b,2*pz);
       else return cs(a,b,2*pz+1);
}
int cd(long a,long b,long pz)
{
 if (a<=s[pz] && b==f[pz]) return v[pz];
    else
    if (b<f[2*pz]) return cd(a,b,2*pz);
       else return cd(a,b,2*pz+1);
}
long max(long a,long b)
{
 if (a>b) return a;
 return b;
}
void move(long a,long b,long pz)
{
 if (s[pz]==f[pz]) v[pz]=b;
    else
    {
     if (a<=f[2*pz]) move(a,b,2*pz);
	else move(a,b,2*pz+1);
     v[pz]=max(v[2*pz],v[2*pz+1]);
    }
}
int main()
{
 long i,el,a,b,c,s1,d1;
 freopen(input,"r",stdin);
 freopen(output,"w",stdout);
 scanf("%ld %ld",&n,&m);
 compute();
 for (i=1;i<=n;i++)
     {
      scanf("%ld",&el);
      fill(1,el,i);
     }
 el=el;
 for (i=1;i<=m;i++)
     {
      scanf("%ld %ld %ld",&c,&a,&b);
      if (!c)
	 {
	  s1=cs(a,b,1);
	  d1=cd(a,b,1);
	  printf("%ld\n",max(s1,d1));
	 }
	 else move(a,b,1);
     }
 return 0;
}