Cod sursa(job #967223)

Utilizator Impaler_009Mihai Nitu Impaler_009 Data 27 iunie 2013 13:03:20
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout ("arbint.out");

int t[1<<18],a,b,s;

void create_tree (int nod, int st, int dr)
{

    while (st!=dr)
    {
        t[nod]= max (t[nod],b);
        int m=(st+dr)>>1;
        if (a<=m)
        {
            nod=nod<<1;
            dr=m;
        }
        else
        {
            nod=(nod<<1)+1;
            st=m+1;
        }
    }
    t[nod]= max (t[nod],b);
}

void update (int nod, int st, int dr)
{
    if (st==dr) t[nod]=b;
    else
    {
        int m=(st+dr)>>1;
        if (a<=m)
        update (nod<<1,st,m);
        else
        update ((nod<<1)+1,m+1,dr);
        t[nod] = max (t[nod<<1],t[(nod<<1)+1]);
    }
}

void query (int nod, int st, int dr)
{
    if (st<a||b<dr)
    {
        int m=(st+dr)>>1;
        int x=0,y=0;
        if (a<=m) query(nod<<1,st,m);
        if (b>m) query ((nod<<1)+1,m+1,dr);
        if (x<y) x=y;
    }
    else  s=max(s,t[nod]);
}

int main()
{
    int n,m,i,x; bool op;
    fin>>n>>m;
    for (a=1;a<=n;a++) {fin>>b; create_tree (1,1,n);}
    for (i=1;i<=m;i++)
    {
        fin>>op>>a>>b;
        if (op==0)
        {s=0;
        query(1,1,n);
        fout<<s<<"\n";}
        else update (1,1,n);
    }
    return 0;
}