Cod sursa(job #950708)

Utilizator Andrei1998Andrei Constantinescu Andrei1998 Data 17 mai 2013 17:01:57
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.34 kb
#include <fstream>

using namespace std;
#define inf 1000000005

struct nod
{
   int st,dr,min;   
   
   nod()
   {
      st=-1;
      dr=-1;
      min=-1;     
   } 
}arbore[500006];

int v[100003];

int minim(int a,int b)
{
   if(a>b)
     return a;
   return b;    
}

void construct(int st,int dr,int poz)
{
  // cout<<"construct("<<st<<","<<dr<<","<<poz<<")"<<endl;
   
   arbore[poz].st=st;
   arbore[poz].dr=dr;
   if(st==dr)
   {
      arbore[poz].min=v[st];          
   }     
   else
   {
      construct(st,(st+dr)/2,poz<<1);
      construct((st+dr)/2+1,dr,(poz<<1)+1);
      arbore[poz].min=minim(arbore[poz<<1].min,arbore[(poz<<1)+1].min);    
   }
}

int query(int st,int dr,int poz)
{
   if(arbore[poz].st==st && arbore[poz].dr==dr)
     return arbore[poz].min;
   else
   {
     int mijl=(arbore[poz].st+arbore[poz].dr)>>1;
     
     if(st>mijl)
       return query(st,dr,(poz<<1)+1);
     else if(dr<=mijl)
       return query(st,dr,poz<<1);
     else
       return minim(query(st,mijl,poz<<1),query(mijl+1,dr,(poz<<1)+1));      
   } 
}

void update(int unde,int ce_val,int poz)
{
  // cout<<"update("<<unde<<","<<ce_val<<","<<poz<<") = "<<arbore[poz].st<<' '<<arbore[poz].dr<<' '<<arbore[poz].min<<endl;
   if(arbore[poz].st==arbore[poz].dr && arbore[poz].st==unde)
   {
      v[unde]=ce_val;
      arbore[poz].min=v[unde];                                  
   }     
   else
   {
      int mijl=(arbore[poz].st+arbore[poz].dr)>>1;
    // cout<<"mijl="<<mijl<<endl;
      if(unde<=mijl)
      {
         update(unde,ce_val,poz<<1);   
         arbore[poz].min=minim(arbore[poz<<1].min,arbore[(poz<<1)+1].min);         
      }    
      else
      {
         update(unde,ce_val,(poz<<1)+1);
         arbore[poz].min=minim(arbore[poz<<1].min,arbore[(poz<<1)+1].min);    
      }
   }
}

int main()
{
    ifstream cin("arbint.in");
    ofstream cout("arbint.out");
    
    int n,m,i;
    cin>>n;
    cin>>m;
    for(i=1;i<=n;i++)
      cin>>v[i];
    
    construct(1,n,1);
    
    int cod,a,b;
    
    for(i=0;i<m;i++)
    {
        cin>>cod>>a>>b;
        
        if(cod==0)
          cout<<query(a,b,1)<<'\n';
        else
          update(a,b,1);
                    
    }
    
    cin.close();
    cout.close();
    //system("PAUSE");
    return 0;
}