Cod sursa(job #2763761)

Utilizator stefantagaTaga Stefan stefantaga Data 16 iulie 2021 17:49:24
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 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;
            maxim=0;
            for (j=st;j<(locst+1)*bucket;j++)
            {
                maxim=max(maxim,v[j]);
            }
            for (j=dr;j>=locdr*bucket;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;j<bucket*(loc+1);j++)
            {
                maxi[loc]=max1(maxi[loc],v[j]);
            }
        }
    }
    return 0;
}