Cod sursa(job #1086507)

Utilizator niktudyNica Tudor niktudy Data 18 ianuarie 2014 11:10:16
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <fstream>
using namespace std;
ifstream fin ("arbint.in");
ofstream fout ("arbint.out");
int h[400000],n,cx,m;
void funct (int k)
{
    if (k==1) return;
    if (k%2==0)
        if (h[k]>h[k+1]) h[k>>1]=h[k];
        else h[k>>1]=h[k+1];
    else
        if (h[k]>h[k-1]) h[k>>1]=h[k];
        else h[k>>1]=h[k-1];
    funct (k>>1);
}
void constr (int k)
{
    if (k%2==0)
        if (h[k]>h[k+1]) h[k>>1]=h[k];
        else h[k>>1]=h[k+1];
    else
        if (h[k]>h[k-1]) h[k>>1]=h[k];
        else h[k>>1]=h[k-1];
}
void buildh ()
{
    int x=1,i;
    while (x<=n)
        x=x<<1;
    for (i=x;i<=x+n-1;i++)
        fin>>h[i];
    cx=x;
    while (x>1)
    {
        for (i=x;i<x<<1&&x<cx+n;i++)
            constr (i);
        x=x>>1;
    }
}
int cautare (int st,int dr,int a,int b,int nod)
{
    int c=0,d=0;
    if (a<=st&&b>=dr) return h[nod];
    {
        if (a<=(st+dr)/2) c=cautare (st,(st+dr)/2,a,b,nod*2);
        if ((st+dr)/2<b) d=cautare ((st+dr)/2+1,dr,a,b,nod*2+1);
    }
    if (c>d) return c;
    else return d;
}
int main()
{
    int i,o,a,b,j;
    fin>>n>>m;
    buildh ();
    for (i=1;i<=m;i++)
    {
        fin>>o>>a>>b;
        if (o==1)
        {
            h[cx+a-1]=b;
            funct (cx+a-1);
        }
        else
        {
            fout<<cautare (1,cx-1,a,b,1)<<'\n';
        }
    }
    fin.close ();
    fout.close();
    return 0;
}