Cod sursa(job #2289235)

Utilizator cosceexcosceex cosceex Data 24 noiembrie 2018 12:01:32
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <bits/stdc++.h>
#define nmax 200005
using namespace std;

int v[nmax],a[4*nmax],n,m;
int poz;
int start,stop;

ifstream f("arbint.in");
ofstream g("arbint.out");

void build(int l,int r, int nod)
{
    if(l==r)
        a[nod]=v[l];
    else
    {
        int m=(l+r)/2;
        build(1,m,2*nod);
        build(m+1,r,2*nod+1);
        a[nod]=max(a[2*nod],a[2*nod+1]);
    }
}

void update(int l,int r, int nod)
{
    if(l==r)
        a[nod]=v[l];
    else
    {
        int m=(l+r)/2;
        if(poz<=m)  update(l,m,2*nod);
        else update(m+1,r,2*nod+1);

        a[nod]=max(a[2*nod],a[2*nod+1]);
    }
}

int query(int l , int r, int nod)
{
    if(start<=l&&stop>=r)
        return a[nod];
    else
    {
        int m=(l+r)/2;
        int max1=INT_MIN,max2=INT_MIN;
        if(start<=m)
            max1=query(l,m,2*nod);
        if(m+1<=stop)
            max2=query(m+1,r,2*nod+1);
        return max(max1,max2);
    }
}

int main()
{
    f>>n>>m;
    for(int i=1;i<=n;++i)
        f>>v[i];
    build(1,n,1);
    int op,x,y;
    for(int i=1;i<=n;i++)
    {
        f>>op>>x>>y;
        if(op==0)
        {
            start=x;
            stop=y;
            g<<query(1,n,1)<<'\n';
        }
        else
        {
            v[x]=y;
            poz=x;
            update(1,n,1);
        }
    }
    return 0;
}