Cod sursa(job #2403260)

Utilizator kywyPApescu tiGEriu kywy Data 11 aprilie 2019 13:24:00
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include<cstdio>
#include<algorithm>
using namespace std;

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

int arbore[200007], n, m, o, a, b;

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

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

int main()
{
    fscanf(in, "%d%d", &n, &m);
    for(int i=1; i<=n; ++i)
    {
        fscanf(in, "%d", &b);
        a=i;
        update(1, n, 1);

    }

    //printf("%d", arbore[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, 1));
        }
        if(o==1)
        {
            update(1, n, 1);
        }
    }
}