Cod sursa(job #2572314)

Utilizator huza_lisandraHuza Lisandra huza_lisandra Data 5 martie 2020 12:27:31
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.78 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int n,m,v[100005],maxx;
pair<pair<int,int>, int> arb[100000];
void construct(int nod, int st, int dr)
{
    int mij;
    arb[nod].first.first=st;
    arb[nod].first.second=dr;
    if(st!=dr)
    {
        mij=(st+dr)/2;
        construct(2*nod,st,mij);
        construct(2*nod+1,mij+1,dr);
        arb[nod].second=max(arb[2*nod].second,arb[2*nod+1].second);
    }
    else
        arb[nod].second=v[st];

}
void maxim(int nod, int a, int b)
{
    int mij,st,dr,val;
    st=arb[nod].first.first;
    dr=arb[nod].first.second;
    if(a<=st and dr<=b)
    {
        val=arb[nod].second;
        maxx=max(val,maxx);
    }
    else
    {
       mij=(st+dr)/2;
       if(a<=mij)
            maxim(2*nod,a,b);
       if(b>mij)
            maxim(2*nod+1,a,b);
    }
}
void inloc(int nod, int a, int b)
{
    int mij,st,dr;
    st=arb[nod].first.first;
    dr=arb[nod].first.second;
    if(st==dr and st==a)
        arb[nod].second=b;
    else
    {
        mij=(st+dr)/2;
        if(a<=mij)
        {
            inloc(2*nod,a,b);
            arb[nod].second=max(arb[2*nod].second,b);
        }
        if(a>mij)
        {
            inloc(2*nod+1,a,b);
            arb[nod].second=max(arb[2*nod+1].second,b);
        }
    }


}
int main()
{
    int i,j,p,x,y;
    fin>>n>>m;
    for(i=1;i<=n;i++)
        fin>>v[i];
    construct(1,1,n);
    for(i=1;i<=m;i++)
    {
        fin>>p>>x>>y;
        if(p==0)
        {
            maxx=0;
            maxim(1,x,y);
            fout<<maxx<<endl;
        }
        else
            inloc(1,x,y);
    }
    fin.close();
    fout.close();
    return 0;
}