Cod sursa(job #1994957)

Utilizator Y0da1NUME JMECHER Y0da1 Data 26 iunie 2017 18:13:15
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;
int n, m, x, pos, a, b, maxim;
int v[400008];
void update2(int st,int dr,int nod)
{
    if (st==dr)
    {
        v[nod]=x;
        return;
    }
    int mij=(st+dr)/2;
    if (mij<pos)
        update2(mij+1,dr,2*nod+1);
       else
            update2(st,mij,2*nod);
    v[nod]=max(v[2*nod],v[2*nod+1]);
}
void update(int st, int dr)
{
    stack <int> stiva;
    int nod = 1, mij;
    while(st<dr)
    {
        mij=(st+dr)/2;
        stiva.push(nod);
        if (mij<pos)
        {
            st = mij +1;
            nod = 2*nod +1;
        }
        else
        {
            dr = mij;
            nod = nod *2;
        }
    }
    if (st==dr)
    {
        v[nod]=x;
        //return;
    }
    //for (int i=1;i<=4*n+1;++i)
    //    cout<<v[i]<<" ";
    ///cout<<endl;
    while(!stiva.empty())
    {
        v[stiva.top()]=max(v[stiva.top()*2], v[stiva.top()*2 + 1]);
        stiva.pop();
    }
    //for (int i=1;i<=4*n+1;++i)
    //    cout<<v[i]<<" ";
    //cout<<endl;
    //cout<<endl;
}
void query (int st, int dr, int nod)
{
    //cout<<nod<<"\n";
    if (a <=st && b>=dr)
    {
        maxim=max(maxim,v[nod]);
        return;
    }
    int mij=(st+dr)/2;
    if (b>mij)
        query(mij+1,dr,2*nod+1);
    if (a<=mij)
        query(st,mij,2*nod);
}
int main()
{
    ifstream in ("arbint.in");
    ofstream out ("arbint.out");
    int i;
    char c;
    in>>n>>m;

    for (i=1;i<=n;++i)
    {
        pos=i;
        in>>x;
        update(1, n);//, 1);
    }
    for(i=0;i<m;++i)
    {
        in>>c>>a>>b;
        if(c=='0')
        {
            maxim = -1;
            query(1, n, 1);
            out<<maxim<<"\n";
        }

        if(c=='1')
        {
            x=b;
            pos = a;
            update(1, n);
        }
            //ce trebe facut
    }
}