Cod sursa(job #2610759)

Utilizator tanasaradutanasaradu tanasaradu Data 5 mai 2020 16:59:49
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.14 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin ("arbint.in");
ofstream fout ("arbint.out");


const int SQRT = 1000;

const int NMAX = 100005;

int a[NMAX] , Left[SQRT] , Right[SQRT] , n , arb[SQRT]  , Q , dim , R;

inline void READ_BUILD()
{
    fin >> n >> Q;
    for(int i = 1 ; i <= n ; i++)
        fin >> a[i];
    R = sqrt(n);
    dim = (n / R) + 1;
    for(int i = 1 ; i * R <= n ; i++)
    {
        Left[i] = (i - 1) * R + 1;
        Right[i] = i * R;
        arb[i] = - 1;
        for(int j = Left[i] ; j <= Right[i] ; j++)
            arb[i] = max(arb[i] , a[j]);
    }
}

inline void UPDATE(int X , int Y)
{
    a[X] = Y;
    for(int i = 1 ; i * R <= n ; i++)
        if(Left[i] <= X && X <= Right[i])
    {
        arb[i] = - 1;
        for(int j = Left[i] ; j <= Right[i] ; j++)
            arb[i] = max(arb[i] , a[j]);
        return;
    }
}

inline int QUERY(int X , int Y)
{
    int st = (1 << 30) , dr = - 1 , MAX = - 1;
    for(int i = 1 ; i <= dim ; i++)
    {
        if(X <= Left[i] && Right[i] <= Y)
        {
            if(st > Left[i])
                st = Left[i]; /// cel mai din stanga capat
            if(dr < Right[i]) /// cel mai din dreapta capat
                dr = Right[i];
            MAX = max(MAX , arb[i]);
        }
        if(Right[i] > Y)
            break;
    }
    if(st == (1 << 30) && dr == - 1) /// intervalul [X , Y] se afla deja intr-o "bucata" de sqrt
    {
        for(int i = X ; i <= Y ; i++)
            MAX = max(MAX , a[i]);
    }
    else /// raman "bucati" de dimensiune < sqrt neactualizare (intervalul [X , Y] nu se afla complet intr-o "bucata" de sqrt
    {
        for(int i = X ; i <= st ; i++)
            MAX = max(MAX , a[i]);
        for(int i = dr ; i <= Y ; i++)
            MAX = max(MAX ,a[i]);
    }
    return MAX;
}

inline void SOLVE()
{
    int op , x , y;
    while(Q -- )
    {
        fin >> op >> x >> y;
        if(op == 1)
            UPDATE(x , y);
        else fout << QUERY(x , y) << "\n";
    }
}

int main()
{
    READ_BUILD();
    SOLVE();
    fin.close();
    fout.close();
    return 0;
}