Cod sursa(job #2947706)

Utilizator TraianQTraianQ TraianQ Data 26 noiembrie 2022 17:04:51
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>
#include <iostream>
#include <cmath>
using namespace std;
int bloc[1005],v[1000005];
int val;
int n,q;
int GetBlock(int poz)
{
    return poz/val;
}
int Query(int a,int b)
{
    a--;
    b--;
    int poz1=GetBlock(a),poz2=GetBlock(b),maxx=0;
    for(int i=poz1+1;i<poz2;i++)
        maxx=max(maxx,bloc[i]);
    int np=GetBlock(a)*val+val-1;
    for(int j=min(np,b);j>=a;j--)
        maxx=max(maxx,v[j]);
    ///cout << maxx << '\n';
    np=min(GetBlock(b)*val,n);
    for(int j=max(np,a);j<=b;j++)
        maxx=max(maxx,v[j]);
    ///cout << np << ' ' << b << ' ' << maxx << '\n';
    return maxx;
}
void Update(int a,int b)
{
    a--;
    v[a]=b;
    bloc[GetBlock(a)]=0;
    for(int j=GetBlock(a)*val;j<GetBlock(a)*val+val;j++)
        bloc[GetBlock(a)]=max(bloc[GetBlock(a)],v[j]);
}
int main()
{
    ifstream cin("arbint.in");
    ofstream cout("arbint.out");
    cin>>n>>q;
    val=sqrt(n);
    for(int i=0;i<n;i++)
    {
        cin>>v[i];
        bloc[i/val]=max(bloc[i/val],v[i]);
    }
    for(int Q=1;Q<=q;Q++)
    {
        int cer,a,b;
        cin>>cer>>a>>b;
        if(cer==0)
        {
            cout<<Query(a,b)<<'\n';
        }
        else
        {
            Update(a,b);
        }
    }
    return 0;
}