Cod sursa(job #2252671)

Utilizator qwerty1234qwerty qwerty1234 Data 2 octombrie 2018 22:09:20
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.81 kb
#include <bits/stdc++.h>

using namespace std;

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

const int Nmax = 100005;

int a[Nmax + 5] , Left[Nmax + 5] , Right[Nmax + 5] , aint[Nmax + 5];

int n , Q , rad , nr;

void Citire_Construire()
{
    fin >> n >> Q;
    for(int i = 1 ; i <= n ; i++)
        fin >> a[i];
    rad = sqrt(n);
    nr = (n / rad) + (n % rad > 0);
    for(int i = 1 ; i <= nr ; i++)
    {
        Left[i] = min(n , (i - 1) * rad + 1);
        Right[i] = min(i * rad , n);
        for(int j = Left[i] ; j <= Right[i] ; j++)
            aint[i] = max(aint[i] , a[j]);
    }
}

void Actualizare(int poz , int x)
{
    a[poz] = x;
    for(int i = 1 ; i <= nr ; i++)
        if(Left[i] <= poz && poz <= Right[i])
        {
            aint[i] = -1;
            for(int j = Left[i] ; j <= Right[i] ; j++)
                aint[i] = max(aint[i] , a[j]);
            break;
        }

}

int Rezolvare(int x , int y)
{
    int maxim = -1;
    int st = (1 << 30) , dr = 0;
    for(int i = 1 ; i <= nr ; i++)
    {

        if(x <= Left[i] && y >= Right[i])
            st = min(st , Left[i]) , dr = max(dr , Right[i]) , maxim = max(maxim , aint[i]);
        if(Right[i] > y)
            break;
    }
    if(st == (1 << 30) && !dr)
        st = dr = y; /// intervalul [st , dr] se afla inclus intr-un interval [Left[i] , Right[i]]
    for(int i = x ; i <= st ; i++)
        maxim = max(maxim , a[i]);
    for(int i = dr ; i <= y ; i++)
        maxim = max(maxim , a[i]);
    return maxim;
}

int main()
{
    int op , x , y;
    Citire_Construire();
    while(Q -- )
    {
        fin >> op >> x >> y;
        if(op)
            Actualizare(x , y);
        else fout << Rezolvare(x , y) << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}