Cod sursa(job #2602898)

Utilizator AnduRazvanMindrescu Andu AnduRazvan Data 18 aprilie 2020 01:35:20
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.9 kb
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

int n,m,a[100001],s[200001],op,x,y,poz;

void Creare(int nod, int st, int dr)
{
    int mij = st + (dr - st) / 2;
    if(st == dr)
        s[nod] = a[st];
    else
    {
        Creare(nod * 2, st, mij);
        Creare(nod * 2 + 1, mij + 1, dr);
        s[nod] = max(s[nod * 2], s[nod * 2 + 1]);
    }
}

int Maxim(int nod, int st, int dr, int pozs, int pozd)
{
    if(st >= pozs && dr <= pozd)
        return s[nod];
    else if(st>pozd)
        return 0;
    else if(dr<pozs)
        return 0;
    else
    {
        int mij=st+(dr-st)/2;

        if(mij < pozs)
            return Maxim(nod * 2 + 1, mij + 1, dr, pozs, pozd);

        else if(mij >= pozd)
            return Maxim(nod * 2 , st, mij, pozs, pozd);

        return max(Maxim(nod * 2, st, mij, pozs, mij), Maxim(nod * 2 + 1, mij + 1, dr, mij  + 1, pozd));
    }
}
int Find(int nod, int st, int dr, int x)
{
    if(st == dr && st == x)
        return nod;
    else
    {
        int mij = st + (dr - st) / 2;
        if(mij < x)
            return Find(nod * 2 + 1, mij + 1, dr, x);
        else
            return Find(nod * 2, st, mij, x);

    }
}
void Actualizare(int poz)
{
    s[poz / 2] = max(s[poz / 2 * 2], s[poz / 2 * 2 + 1]);
    if(poz != 1)
        Actualizare(poz / 2);
}
int main()
{
    FILE *f, *g;
    f = fopen("arbint.in","r");
    g = fopen("arbint.out","w");

    fscanf(f,"%d %d",&n,&m);
    int i;
    for(i = 1; i <= n; i++)
        fscanf(f, "%d", &a[i]);

    Creare(1, 1, n);
    for(i = 1; i <= m; i++)
    {
        fscanf(f,"%d %d %d", &op, &x, &y);
        if(op == 0) fprintf(g, "%d\n", Maxim(1, 1, n, x, y));
        if(op == 1)
            {
                poz = Find(1, 1, n, x);
                s[poz] = y;
                Actualizare(poz);
            }
    }
    return 0;
}