Cod sursa(job #2272307)

Utilizator adimiclaus15Miclaus Adrian Stefan adimiclaus15 Data 29 octombrie 2018 22:50:27
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("arbint.in");
ofstream g("arbint.out");
int n,m,a[100005],bucata[505],index=-1,k;
void update(int poz,int val)
{
    int i,start;
    a[poz]=val;
    start=k*(poz/k);
    bucata[poz/k]=-1;
    for(i=start;i<=start+k-1;i++)
    {
        bucata[poz/k]=max(bucata[poz/k],a[i]);
    }
}
int query(int st,int dr)
{
    int maxim=-1;
    while(st<dr && st%k!=0 && st!=0)
    {
        maxim=max(maxim,a[st]);
        st++;
    }
    while(st+k<=dr)
    {
        maxim=max(maxim,a[st/k]);
        st=st+k;
    }
    while(st<=dr)
    {
        maxim=max(maxim,a[st]);
        st++;
    }
    return maxim;
}
int main()
{
    int i,st,dr,val,op,poz;
    f>>n>>m;
    k=sqrt(n);
    for(i=0;i<n;i++)
    {
        f>>a[i];
        if(i%k==0)
        {
            index++;
            bucata[index]=-1;
        }
        bucata[index]=max(bucata[index],a[i]);
    }
    while(m--)
    {
        f>>op;
        if(op==0)
        {
            f>>st>>dr;
            st--;
            dr--;
            g<<query(st,dr)<<"\n";
        }
        else
        {
            f>>poz>>val;
            poz--;
            update(poz,val);
        }
    }
    return 0;
}