Cod sursa(job #1919725)

Utilizator SmitOanea Smit Andrei Smit Data 9 martie 2017 20:47:40
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>
#define INF 2000000000

using namespace std;

int H,nrbuc;
int n,m;
int a[100003],buc[100003],b[403];

int Query(int x,int y)
{
    int poz,maxim;
    maxim=-INF;
    poz=buc[x] + 1;
    while(poz<buc[y])
    {
        maxim=max(maxim,b[poz]);
        poz++;
    }
    poz=x;
    while(buc[poz]==buc[x])
    {
        maxim=max(maxim,a[poz]);
        poz++;
    }
    poz=y;
    while(buc[poz]==buc[y])
    {
        maxim=max(maxim,a[poz]);
        poz--;
    }
    return maxim;
}

void Update(int poz,int x)
{
    a[poz]=x;
    if(b[buc[poz]]>x)
        b[buc[poz]]=x;
}

int main()
{
    int i,x,y,op;
    ifstream fin("arbint.in");
    ofstream fout("arbint.out");
    fin>>n>>m;
    for(i=1;i<=n;++i)
        fin>>a[i];
    H=int(sqrt(n));
    for(i=1;i<=H+1;++i)
        b[i]=-INF;
    for(i=1;i<=n;++i)
    {
        buc[i]=i/H + 1;
        if(i%H==0)  buc[i]--;
        b[buc[i]]=max(b[buc[i]],a[i]);
    }
    nrbuc=buc[n];
    ///sol
    for(i=1;i<=m;++i)
    {
        fin>>op>>x>>y;
        if(op==0)
            fout<<Query(x,y)<<"\n";
        else
            Update(x,y);
    }
    return 0;
}