Cod sursa(job #3187046)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 27 decembrie 2023 08:51:34
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.77 kb
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

constexpr unsigned maxn = 1 << 17;

namespace {
template <unsigned niv>
unsigned buf[niv];

template <unsigned niv = maxn>
void build() {
    if constexpr (niv > 1) {
#pragma GCC unroll 128
        for (unsigned i = 0; i < niv; i += 2)
            buf<niv / 2>[i / 2] = max(buf<niv>[i], buf<niv>[i + 1]);
        build<niv / 2>();
    }
}

template <unsigned niv>
void rebuild(unsigned i) {
    if constexpr (niv > 1) {
        buf<niv / 2>[i / 2] = max(buf<niv>[i], buf<niv>[i ^ 1]);
        rebuild<niv / 2>(i / 2);
    }
}

void update(unsigned i, unsigned x) {
    buf<maxn>[i] = x;
    rebuild<maxn>(i);
}

template <unsigned niv = maxn>
unsigned query(unsigned st, unsigned dr, unsigned r = 0) {
    if constexpr (niv >= 1) {
        if (st == dr) return r;
        if (st % 2) r = max(r, buf<niv>[st++]);
        if (dr % 2) r = max(r, buf<niv>[--dr]);
        return query<niv / 2>(st / 2, dr / 2, r);
    }
    return 0;
}

constexpr int maxbuf = 2e6 + 100;
char rdbuf[maxbuf] = {}, *ip = rdbuf;

char getch() {
    ++ip;
    // while (*ip < '0') ++ip;
    return *ip++;
}

unsigned get_int() {
    unsigned x = 0;
    ++ip;
    // while (*ip < '0') ++ip;
    for (; *ip >= '0'; ++ip) x = 10 * x + *ip - '0';
    return x;
}

// Thanks to:
// https://stackoverflow.com/questions/25892665/performance-of-log10-function-returning-an-int
unsigned digits(unsigned x) {
    static constexpr unsigned char guess[33] = {
        0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
        5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9};
    static constexpr unsigned int tenToThe[] = {
        1,      10,      100,      1000,      10000,
        100000, 1000000, 10000000, 100000000, 1000000000,
    };
    unsigned int digits = guess[32 - __builtin_clz(x)];
    return digits + (x >= tenToThe[digits]);
}

char wrbuf[maxbuf], *op = wrbuf;
void write_int(int x) {
    if (x < 10) {
        *op++ = '0' + x;
        *op++ = '\n';
    } else {
        char *p = (op += digits(x));
        for (int y; x;) {
            y = x / 10;
            *--p = x - 10 * y + '0';
            if (y) {
                x = y / 10;
                *--p = y - 10 * x + '0';
            } else
                break;
        }
        *op++ = '\n';
    }
}
};  // namespace

int main() {
    fread(rdbuf, sizeof(char), maxbuf, fopen("arbint.in", "r"));
    --ip;

    unsigned n = get_int(), q = get_int();

    for (unsigned i = 0; i < n; ++i) buf<maxn>[i] = get_int();

    build();

    for (unsigned a, b; q--;) {
        char t = getch();
        a = get_int(), b = get_int();
        if (t == '1')
            update(a - 1, b);
        else
            write_int(query(a - 1, b));
    }

    fwrite(wrbuf, sizeof(char), op - wrbuf, fopen("arbint.out", "w"));
}