Mai intai trebuie sa te autentifici.

Cod sursa(job #2401097)

Utilizator kywyPApescu tiGEriu kywy Data 9 aprilie 2019 13:42:04
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include<cstdio>
#include <algorithm>

using namespace std;

FILE* in=fopen("arbint.in", "r");
FILE* out=fopen("arbint.out", "w"); //taxe

int v[100007];
int arbore[200007];

void update (int st,int dr,int pos,int val,int cpos)
{
    if (st>pos || dr<pos || st>dr)
    {
        return;
    }
    if (st==dr)
    {
        arbore[cpos]=val;
        return;
    }
    int mij=(st+dr)/2;
    update (st,mij,pos,val,2*cpos);
    update (mij+1,dr,pos,val,2*cpos+1);
    arbore[cpos]=max(arbore[2*cpos],arbore[2*cpos+1]);
}

int query(int st, int dr, int a, int b, int pos)
{
    if (st==a && dr==b)
    {
        return arbore[pos];
    }
    int mij=(st+dr)/2;
    if (b<=mij)
    {
        return query(st,mij,a,b,2*pos);
    }
    else
    {
        if (a>mij)
        {
            return query(mij+1,dr,a,b,2*pos+1);
        }
        else
        {
            return max(query(st,mij,a,mij,2*pos),query(mij+1,dr,mij+1,b,2*pos+1));
        }
    }
}

int main()
{
    int n, m, o, a, b;
    fscanf(in, "%d%d", &n, &m);
    for(int i=1; i<=n; ++i)
    {
        fscanf(in, "%d\n", &v[i]);
        update(1, n, i, v[i], 1);
    }
    for(int i=1; i<=m; ++i)
    {
        fscanf(in, "%d%d%d", &o, &a, &b);
        if(o==0) fprintf(out, "%d\n", query(1, n, a, b, 1));
        else update(1, n, a, b, 1);
    }
}