Cod sursa(job #2988751)

Utilizator biancaivascuBianca Ivascu biancaivascu Data 5 martie 2023 14:14:49
Problema Arbori de intervale Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

#define Maxn 100000
const int root= sqrt(Maxn);
const int bsize=(Maxn+root-1)/root;

int v[Maxn];
int n;
int batog[bsize];

void build()
{
    int i;
    for(i=0; i<n; i++)
    {
        if(v[i]>batog[i/root]) batog[i/root]=v[i];
    }
}
void update(int poz, int x)
{
    v[poz]=x;
    int i, a;
    a=poz/root;
    if(v[poz]==batog[a])
    {
        for(i=a*root; i<a*root+root; i++)
        {
            if(v[i]>batog[a]) batog[a]=v[i];
        }
    }
}
int query(int st, int dr)
{
    int max=0, first, last;
    first=(st+root-1)/root*root;
    last=dr/root*root;
    while(st<=dr && st<=first)
    {
        if(v[st]>max) max=v[st];
        st++;
    }
    while(dr>=st && dr>last)
    {
        if(v[dr]>max) max=v[dr];
        dr--;
    }
    first/=root;
    last/=root;
    while(first<last)
    {
        if(batog[first]>max) max=batog[first];
        first++;
    }
    return max;
}
int main()
{
    ifstream in("arbint.in");
    ofstream out("arbint.out");
    int m, i, type, a, b;
    in>>n>>m;
    for(i=0; i<n; i++)
    {
        in>>v[i];
    }
    build();
    while(m--)
    {
        in>>type>>a>>b;
        if(type==1)
        {
            a--;
            update(a, b);
        }
        else
        {
            a--; b--;
            out<<query(a, b)<<'\n';
        }
    }
    return 0;
}