Cod sursa(job #2090303)

Utilizator MarinPeptenaruMarin Vasile Peptenaru MarinPeptenaru Data 17 decembrie 2017 21:08:23
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
#include <bits/stdc++.h>

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