Cod sursa(job #1049383)

Utilizator stefyvoltFMI Stefan Niculae stefyvolt Data 7 decembrie 2013 12:10:20
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.29 kb
#include <iostream>
#include <fstream>

using namespace std;

#define Max(a,b) a>b ? a:b

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

int n, m, segtree[400070];

void afisare(int heap[100])
{
   if(heap[1])cout << "     " << heap[1] << endl;
   else cout << "heap gol";
   if(heap[2]) cout << "   /"; else cout << "   ";
   if(heap[3]) cout << "   \\ ";
   cout << endl;
   if(heap[2]) cout << "  " <<  heap[2]; else cout << "  ";
   if(heap[3]) cout << "     " << heap[3];

   cout << endl;
   if(heap[2] || heap[3])
   {
       if(heap[4]) cout << " /"; else cout << "  ";
       if(heap[5]) cout << " \\";else cout << "  ";

       if(heap[6]) cout << "   /"; else cout << "    ";
       if(heap[7]) cout << " \\";

       cout << endl;

       if(heap[4]) cout << "" <<  heap[4]; else cout << " ";
       if(heap[5]) cout << "   " << heap[5]; else cout << "    ";

       if(heap[6]) cout << " " << heap[6]; else cout << "  ";
       if(heap[7]) cout << "   " << heap[7];
   }

   cout << endl;
}

void actualiz(int poz, int val, int nod=1, int st=1, int dr=n)
{
    if(st == dr)                            // daca am ajuns la frunze
    {
        segtree[nod] = val;                 // nodul unde am ajuns ia valoarea cu care a fost apelata functia
        return;                             // iesim din functie
    }

    int mij = (st+dr)/2;                    // se fac doua apeluri una la stanga (st,mij) si una la dreapta (mij+1,dr)
    if(poz <= mij) actualiz(poz,val,2*nod,st,mij);
    else           actualiz(poz,val,2*nod+1,mij+1,dr);

    segtree[nod] = Max(segtree[2*nod],segtree[2*nod+1]);
}

void citire()
{
    int x;

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

int interog(int inc, int sf, int nod=1, int st=1, int dr=n, int maxim=-1)
{
    if(inc <= st && dr <= sf)
        return Max(maxim, segtree[nod]);

    int mij = (st+dr)/2;                        // interogam in stanga si in dreapta
    if(inc <= mij) return interog(inc,sf,2*nod,st,mij,maxim);
    if(mij < sf)   return interog(inc,sf,2*nod+1,mij+1,dr,maxim);
}

void rezolva()
{
    int tip, a, b;
    for(int i=1; i<=m; i++)
    {
        f >> tip >> a >> b;
        if(!tip) g << interog(a,b) << endl;
        else actualiz(a,b);
    }
}

int main()
{
   citire();
   rezolva();

   return 0;
}