Cod sursa(job #2432927)

Utilizator smoc_georgemarianSmoc George-Marian smoc_georgemarian Data 25 iunie 2019 15:15:35
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.39 kb
#include <bits/stdc++.h>
#define NMAX 100009
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int a[4*NMAX];
int v[NMAX];
int n,m;
int c, x,y;

void citire();
void build(int node, int st, int dr);
int query(int node, int st, int dr, int x, int y);
void update(int node, int st, int dr, int x, int val);
int main()
{
 citire();
 build(1,1,n);
 for(int i=1;i<=m;i++)
    {
     fin>>c>>x>>y;
     if(!c)
        {
         fout<<query(1,1,n,x,y)<<'\n';
        }
        else
        {
         update(1,1,n,x,y);
        }
    }
    return 0;
}
void citire()
{
  fin>>n>>m;
  for(int i=1;i<=n;i++)
      fin>>v[i];
}
void build(int node, int st, int dr)
{ if(st==dr)
    { a[node]=v[st];return;}
  int mj;
  mj= (st+dr)/2;
  build(2*node,st,mj);
  build(2*node+1,mj+1,dr);
  a[node]=max(a[2*node],a[2*node+1]);
}
void update(int node, int st, int dr ,int x, int val)
{
 if(st==dr)
        {a[node]=val;return;}
 int mj;
 mj=(st+dr)/2;
 if(x<=mj)
      update(2*node,st,mj,x,val);
      else
      update(2*node+1,mj+1,dr,x,val);
 a[node]=max(a[2*node],a[2*node+1]);

}
int query(int node, int st, int dr, int x, int y)
{ int ans1=0,ans2=0;
  if(x<=st && dr<=y)
    return a[node];
  int mj=(st+dr)/2;
  if(x<=mj)
    ans1=query(2*node,st,mj,x,y);

  if(y>mj)
    ans2=query(2*node+1,mj+1,dr,x,y);
  return max(ans1,ans2);
}