Cod sursa(job #974433)

Utilizator supermitelArdelean Razvan Mitel supermitel Data 17 iulie 2013 11:04:40
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.92 kb
#include <cstdio>
#include <algorithm>

using namespace std;

struct element
{
    int ist, idr;
    int max;
    element *st, *dr, *par;
    element(int interv = 0, int x = 0)
    {
        max = x;
        ist = idr = interv;
        st = dr = par = NULL;
    }
}*root, *a[100010];

int n, m;

void citire()
{
    int i, aux;
    scanf("%d%d", &n, &m);
    for(i = 1; i <= n; i++)
    {
        scanf("%d", &aux);
        a[i] = new element(i, aux);
    }
}

void make(element * &nod, int st, int dr, element *par = NULL)
{
    if(st > dr)
        return;
    if(st == dr)
    {
        a[st] -> par = par;
        nod = a[st];
        return;
    }
    nod = new element;
    nod -> ist = st;
    nod -> idr = dr;
    nod -> par = par;
    int mij = (st+dr)/2;
    make(nod -> st, st, mij, nod);
    make(nod -> dr, mij+1, dr, nod);
    nod -> max = max(nod -> st -> max , nod -> dr -> max);
}

int getmax(element* nod, int st, int dr)
{
    if(nod == NULL || nod -> ist > dr || nod -> idr < st)
        return -1;
    if(nod -> ist >= st && nod -> idr <= dr)
        return nod -> max;
    return max(getmax(nod -> st, st, dr), getmax(nod -> dr, st, dr));
}

int change(int poz, int b)
{
    a[poz] -> max = b;
    int ok = 1, aux;
    element *p = a[poz] -> par;
    while(ok && p)
    {
        ok = 0;
        aux = p -> max;
        p -> max = max(p -> st -> max, p -> dr -> max);
        if(p -> max != aux)
            ok = 1;
        p = p -> par;
    }
}

void solve()
{
    int command, x, y;
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d%d", &command, &x, &y);
        if(command == 0)
            printf("%d\n", getmax(root, x, y));
        else
            change(x, y);
    }
}

int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    citire();
    make(root, 1, n);
    solve();
    return 0;
}