Cod sursa(job #2101076)

Utilizator MarinPeptenaruMarin Vasile Peptenaru MarinPeptenaru Data 6 ianuarie 2018 20:00:49
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("arbint.in");
ofstream out("arbint.out");
const int nx=100002;
int v[nx],n,arb[4*nx];
int op,a,b,nrop,mx=INT_MIN;
void build (int nod, int st, int dr)
{
    if(st==dr)
        arb[nod]=v[st];
    else if(st<dr)
    {
        int m=(st+dr)/2;
        int nod1=2*nod;
        int nod2=2*nod+1;
        build(nod1,st,m);
        build(nod2,m+1,dr);
        arb[nod]=max(arb[nod1],arb[nod2]);
    }
}
void op0 (int nod, int st, int dr, int a, int b)
{
    if(st==a && dr==b)
        mx=max(mx,arb[nod]);
    else if(st<dr)
    {
        int m=(st+dr)/2;
        int nod1=2*nod;
        int nod2=2*nod+1;
        if(a<=m)
        {
            if(b<=m) op0(nod1,st,m,a,b);
            else
            {
                op0(nod1,st,m,a,m);
                op0(nod2,m+1,dr,m+1,b);
            }
        }
        else return op0(nod2,m+1,dr,a,b);
    }
}
void op1 (int nod, int st , int dr, int poz, int val)
{
    if(st==dr)
    {
        arb[nod]=val;
        v[poz]=val;
    }
    else if(st<dr)
    {
        int m=(st+dr)/2;
        int nod1=nod*2;
        int nod2=nod*2+1;
        if(poz<=m) op1(nod1,st,m,poz,val);
        else op1(nod2,m+1,dr,poz,val);
        arb[nod]=max(arb[nod1],arb[nod2]);
    }
}
int main()
{
    in>>n>>nrop;
    for(int i=1; i<=n; i++)
        in>>v[i];
    build(1,1,n);
    for(;nrop;nrop--)
    {
        in>>op>>a>>b;
        if(op==0)
        {
            mx=INT_MIN;
            op0(1,1,n,a,b);
            out<<mx<<'\n';
        }
        else op1(1,1,n,a,b);
    }
    return 0;
}