Cod sursa(job #2769033)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 13 august 2021 11:10:01
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.07 kb
#include <iostream>
#include <cstdio>

using namespace std;

typedef pair < int, int > PII;

const int NMAX = 1e5 + 1, DIM = 317;
const int INF = 1e9 + 1;

int N, A[NMAX];

PII Segment[(DIM + 5)];
int M, Id[NMAX], Max[(DIM + 5)];

int Q;

namespace InParser
{
static const int BSIZE = (1 << 16);

static int pos = (BSIZE - 1);
static char buff[BSIZE];

static inline char Char ()
{
    ++pos;

    if(pos == BSIZE)
    {
        pos = 0;

        int n = fread(buff, 1, BSIZE, stdin);

        if(n != BSIZE)
            for(int i = n; i < BSIZE; ++i)
                buff[i] = 0;
    }

    return buff[pos];
}

inline int Int ()
{
    int ans = 0, sign = 1;

    for( ; ; )
    {
        char Ch = Char();

        if(Ch == '-')
        {
            sign = -1;

            break;
        }

        if(Ch >= '0' && Ch <= '9')
        {
            ans = (int)(Ch - '0');

            break;
        }
    }

    for( ; ; )
    {
        char Ch = Char();

        if(Ch >= '0' && Ch <= '9')
            ans = ans * 10 + (int)(Ch - '0');
        else
            break;
    }

    return (ans * sign);
}
};

static inline int Find (int pos)
{
    return ((pos + DIM - 1) / DIM);
}

static inline void Initialize ()
{
    for(int i = 1; i <= M; ++i)
        Max[i] = -INF;

    return;
}

static inline int my_max (int a, int b)
{
    return ((a > b) ? a : b);
}

static inline void Read ()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr);

    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);

    N = InParser :: Int(), Q = InParser :: Int();

    M = Find(N);

    Initialize();

    for(int i = 1; i <= N; ++i)
    {
        A[i] = InParser :: Int();

        Id[i] = Find(i), Max[Id[i]] = my_max(Max[Id[i]], A[i]);

        if(Id[i] != Id[i - 1])
            Segment[Id[i]].first = i;

        Segment[Id[i]].second = i;
    }

    return;
}

static inline int Query (int a, int b)
{
    int ans = -INF;

    if(Id[a] == Id[b])
    {
        for(int i = a; i <= b; ++i)
            ans = my_max(ans, A[i]);

        return ans;
    }

    for(int i = a; i <= Segment[Id[a]].second; ++i)
        ans = my_max(ans, A[i]);

    for(int i = Segment[Id[b]].first; i <= b; ++i)
        ans = my_max(ans, A[i]);

    for(int i = (Id[a] + 1); i < Id[b]; ++i)
        ans = my_max(ans, Max[i]);

    return ans;
}

static inline void Update (int pos, int Val)
{
    A[pos] = Val;

    Max[Id[pos]] = -INF;

    for(int i = Segment[Id[pos]].first; i <= Segment[Id[pos]].second; ++i)
        Max[Id[pos]] = my_max(Max[Id[pos]], A[i]);

    return;
}

static inline void TestCase ()
{
    short int Type = InParser :: Int();
    int a = InParser :: Int(), b = InParser :: Int();

    if(Type == 0)
        printf("%d\n", Query(a, b));
    else
        Update(a, b);

    return;
}

static inline void Solve ()
{
    while(Q--)
        TestCase();

    return;
}

int main()
{
    Read();

    Solve();

    return 0;
}