Cod sursa(job #1399179)

Utilizator robertstrecheStreche Robert robertstreche Data 24 martie 2015 16:55:56
Problema Arbori de intervale Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.64 kb
#include <cstdio>

#define NMAX 100005
#define max(a,b) a>b?a:b

using namespace std;

int tree[3*NMAX];
int n,tip,poz,val,st,dr,nr_query;

#define DIM 10000
char buff[DIM];
int poz1=0;

void citeste(int &numar)
{
     numar = 0;
     while (buff[poz1] < '0' || buff[poz1] > '9')
          if (++poz1 == DIM)
               fread(buff,1,DIM,stdin),poz1=0;
     while ('0'<=buff[poz1] && buff[poz1]<='9')
     {
          numar = numar*10 + buff[poz1] - '0';
          if (++poz1 == DIM)
               fread(buff,1,DIM,stdin),poz1=0;
     }
}
inline void update(int nod,int poz1,int poz2)
{
    if (poz1==poz2)tree[nod]=val;
    else
    {
        int m=(poz1+poz2)/2;
        if (poz<=m)update(2*nod,poz1,m);
        else update(2*nod+1,m+1,poz2);
        tree[nod]=max(tree[2*nod],tree[2*nod+1]);
    }
}
inline int query(int nod,int poz1,int poz2)
{
    if (poz1>=st && poz2<=dr)return tree[nod];
    if (poz1>dr || poz2<st)return 0;
    int m=(poz1+poz2)/2;
    return max(query(2*nod,poz1,m),query(2*nod+1,m+1,poz2));
}
int main()
{
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);

    citeste(n);
    citeste(nr_query);
    for (int i=1;i<=n;i++)
    {
        poz=i;
        citeste(val);
        update(1,1,n);
    }
    for (int i=1;i<=nr_query;i++)
    {
        citeste(tip);
        if (tip==1)
        {
            citeste(poz);
            citeste(val);
            update(1,1,n);
        }
        else
        {
            citeste(st);
            citeste(dr);
            printf("%d\n",query(1,1,n));
        }
    }
    fclose(stdin);
    fclose(stdout);
}