Cod sursa(job #2419898)

Utilizator MortemPlaiasu Iulia-Silvia Mortem Data 9 mai 2019 19:47:32
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <iostream>

FILE * fin=fopen("arbint.in","r");
FILE * fout=fopen("arbint.out","w");

int n,m;
int v[1005000];

void update(int a, int pos)
{
  int prev1 = v[pos*2+1];
  int prev2 = v[pos*2+2];
  v[pos]=std::max(prev2,prev1);
  if(pos>0)
    update(a,(pos-1)/2);
}

void push(int a,int ind,int low, int high,int pos)
{
  if(low==high)
  {
    v[pos]=a;
    update(a,(pos-1)/2);
    return;
  }
  int m= (high+low)/2;
  if(ind<=m)
    return push(a,ind,low,m,pos*2+1);
  return   push(a,ind,m+1,high,pos*2+2);
}

void write()
{
  for(int i=0;i<=4*n;i++)
    std::cout<<v[i]<<" ";
  std::cout<<"\n";
}

int ask(int a, int b, int low,int high, int pos)
{
  if(low==high && low>=a && low<=b)
    return v[pos];
  if(high<a|| b<low)
    return -1;
  if(a<=low && high<=b )
    return v[pos];
  int m = (high+low)/2;
  return std::max(ask(a,b,low,m,   pos*2+1),
                  ask(a,b,m+1,high,pos*2+2));
}

int main()
{
  fscanf(fin,"%d %d",&n,&m);
  for(int i=1;i<=1004999;i++)
    v[i]=-1;
  for(int i=1;i<=n;i++)
  {
    int j;
    fscanf(fin,"%d",&j);
    push(j,i,1,n,0);
  }
  for(int i=0;i<m;i++)
  {
    int a, b, c;
//    write();
    fscanf(fin,"%d %d %d",&c,&a,&b);
    if(c==0)
      fprintf(fout,"%d\n",ask(a,b,1,n,0));
    else
      push(b,a,1,n,0);
   // std::cout<<"\n";
  }
//  write();
}