Cod sursa(job #1197426)

Utilizator ariel_roAriel Chelsau ariel_ro Data 11 iunie 2014 22:21:38
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.76 kb
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
#define NX 100005
#define NXARB 262144
 
int t[NXARB], A, B, max;
 
inline int MaximTwoElems(int a, int b) {
       if (a > b) 
           return a;
       return b;
}
 
void createArbInt(int nod, int st, int dr, int val)
{
    if (st == dr)
    {
        t[nod] = val;
    }
    else
    {
        int mij = (st + dr) >> 1;
        if (A <= mij)
        {
            createArbInt(nod << 1, st, mij, val);
        } else
		{
            createArbInt((nod << 1) + 1, mij + 1, dr, val);
        }
        t[nod] = MaximTwoElems(t[nod << 1], t[(nod << 1) + 1]);
    }
}
 
void query(int nod, int st, int dr)
{
    if (A <= st && dr <= B)
    {
        max = MaximTwoElems(max, t[nod]);
    }
    else
    {
        int mij = (st + dr) >> 1;
        int val1 = 0, val2 = 0;
        if (A <= mij)
        {
            query(nod << 1, st, mij);
        }
         
        if (B > mij)
        {
            query((nod << 1) + 1, mij + 1, dr);
        }
    }
}
 
int main()
{
    int N, M, op, an, bn, val;
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
  
    scanf("%d %d", &N, &M);
   
    for (int i = 1; i <= N; i++)
    {
        scanf("%d", &val);
        A = i; B = i;
        createArbInt(1, 1, N, val);
    }
 
    for (int i = 1; i <= M; i++)
    {
        scanf("%d %d %d\n", &op, &an, &bn);
   
        switch (op)
        {
        case 0:
            A = an;
            B = bn;
			max = -1;
			query(1, 1, N);
            printf("%d\n", max);
            break;
        case 1:
            A = an;
            B = an;
            createArbInt(1, 1, N, bn);
              
            break;
        }
    }
 
}