Cod sursa(job #2189039)

Utilizator ipus1Stefan Enescu ipus1 Data 27 martie 2018 17:31:22
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include<cstdio>
int arbMax[400100];
int maxim;
int maxx(int x, int y)
    {if(x >= y)
        return x;
    return y;
    }
void update(int nod, int nodSt, int nodDr, int updPos, int updVal)
    {if(nodSt == nodDr)
        arbMax[nod] = updVal;
    else
        {int m = (nodSt + nodDr) / 2;
        if(updPos <= m)
            update(2 * nod, nodSt, m, updPos, updVal);
        else
            update(2 * nod + 1, m + 1, nodDr, updPos, updVal);
        arbMax[nod] = maxx(arbMax[2 * nod], arbMax[2 * nod + 1]);
        }
    }
void query(int nod, int nodSt, int nodDr, int updSt, int updDr)
    {if(nodSt >= updSt && nodDr <= updDr)
        maxim = maxx(maxim, arbMax[nod]);
    else
        {int m = (nodSt + nodDr) / 2;
        if(updSt <= m)
            query(2 * nod, nodSt, m, updSt, updDr);
        if(updDr >= m + 1)
            query(2 * nod + 1, m + 1, nodDr, updSt, updDr);
        }
    }
int main ()
{freopen ("arbint.in","r",stdin);
freopen ("arbint.out","w",stdout);
int n,m,c,x,y,i;
scanf("%d%d",&n,&m);
for(i = 1; i <= n; i++)
    {scanf("%d",&x);
    update(1, 1, n, i, x);
    }
for(i = 1; i <= m; i++)
    {scanf("%d%d%d",&c,&x,&y);
    if(c == 1)
        update(1, 1, n, x, y);
    else
        {maxim = -1;
        query(1, 1, n, x, y);
        printf("%d\n",maxim);
        }
    }
return 0;
}