Cod sursa(job #2763765)

Utilizator stefantagaTaga Stefan stefantaga Data 16 iulie 2021 17:57:10
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("arbint.in");
ofstream g("arbint.out");
int max1(int a,int b)
{
    if (a>b)
    {
        return a;
    }
    return b;
}
int n,m,v[100005],maxi[100005],bucket,i,j,maxim,locst,locdr,st,dr,loc,tip,poz,val;
int main()
{
    f>>n>>m;
    for (i=1;i<=n;i++)
    {
        f>>v[i];
    }
    bucket=sqrt(n);
    for (i=1;i<=n;i++)
    {
        loc=(i-1)/bucket;
        maxi[loc]=max1(maxi[loc],v[i]);
    }
    for (i=1;i<=m;i++)
    {
        f>>tip;
        if (tip==0)
        {
            f>>st>>dr;
            locst=(st-1)/bucket;
            locdr=(dr-1)/bucket;
            if (locst==locdr)
            {
                for (j=st;j<=dr;j++)
                {
                    maxim=max(maxim,v[j]);
                }
            }
            else
            {
                maxim=0;
            for (j=st;j<=(locst+1)*bucket;j++)
            {
                maxim=max(maxim,v[j]);
            }
            for (j=dr;j>=locdr*bucket+1;j--)
            {
                maxim=max(maxim,v[j]);
            }
            for (j=locst+1;j<locdr;j++)
            {
                maxim=max(maxim,maxi[j]);
            }
            g<<maxim<<'\n';
            }
        }
        else
        {
            f>>poz>>val;
            v[poz]=val;
            maxi[loc]=0;
            loc=(poz-1)/bucket;
            for (j=bucket*loc+1;j<=bucket*(loc+1);j++)
            {
                maxi[loc]=max1(maxi[loc],v[j]);
            }
        }
    }
    return 0;
}