Cod sursa(job #1423535)

Utilizator Vele_GeorgeVele George Vele_George Data 21 aprilie 2015 22:42:38
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <iostream>
#include <fstream>
#define inf (1<<30)
using namespace std;

int imax;

ifstream f("arbint.in");
ofstream g("arbint.out");


int max(int a, int b)
{
    if (a>b) return a;
             return b;

}


struct nod
{
    int value;
    nod *st, *dr;
} *R = new nod(); // root


void Genesis (nod *&p, int a, int b)
{
    p->value = 0;

    if (a==b) return;

    int mid = (a+b)/2;

    p->st = new nod();
    Genesis(p->st, a, mid);

    p->dr = new nod();
    Genesis(p->dr, mid+1, b);
}

void update(nod *&p, int a, int b, int poz, int val)
{

    if (a==b) p->value = val;
    else
    {
        int mid =(a+b)/2;
        if (poz <=mid) update(p->st, a, mid, poz, val);
                  else update(p->dr, mid+1, b, poz, val);
        p->value = max(p->st->value, p->dr->value);
    }
}

void search(nod *p, int a, int b, int left, int right) // a-b cautat
{
    if (a <= left && right <= b)  imax = max(imax, p->value);
    else
    {
        int mid = (left+right)/2;
        if (a <= mid) search(p->st, a, b, left, mid);
        if (mid < b) search(p->dr, a, b, mid+1, right);
    }
}


int main()
{
     int n,m,x,a,b,act;

    f>>n>>m;
    Genesis(R,1,n);

    for(int i=1; i<=n; i++)
    {
        f >> x;
        update(R,1,n,i,x);
    }

    for(int i=1; i<=m; i++)
    {
        f>>act>>a>>b;
        if (act==0){
            imax =-inf;
            search(R,a,b,1,n);
            g<<imax<<"\n";
        }else{

            update(R,1,n,a,b);

        }
    }
    f.close();
    g.close();


    return 0;
}