Cod sursa(job #3124871)

Utilizator Mircea08Tomita Mircea Stefan Mircea08 Data 30 aprilie 2023 13:58:59
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.47 kb
#include <bits/stdc++.h>
class InParser {
private:
    FILE *fin;
    char *buff;
    short pos = 4095;
    inline char get_ch() {
        ++ pos;
        if (pos == 4096) {
            pos = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[pos];
    }
public:
    InParser(const char *file) {
        fin = fopen(file, "r");
        buff = new char[4096]();
    }
    InParser& operator >> (int &x) {
        char ch = get_ch();
        while (!isdigit(ch))
            ch = get_ch();
		x = ch - '0';
        ch = get_ch();
        while (isdigit(ch)) {
            x = x * 10 + ch - '0';
            ch = get_ch();
        }
        return *this;
    }
};
int n, queries, *aint, *v, i;
inline void build(int node, int nleft, int nright) {
    if (nleft == nright)
        aint[node] = v[nleft];
    else {
        int mid = (nleft + nright) >> 1;
        build(node << 1, nleft, mid);
        build((node << 1) + 1, mid + 1, nright);
        aint[node] = std :: max(aint[node << 1], aint[(node << 1) + 1]);
    }
}
int left, right;
inline int query(int node, int nleft, int nright) {
    if (left > nright or right < nleft)
        return -1;
    if (left <= nleft and right >= nright)
        return aint[node];
    int mid = (nleft + nright) >> 1;
    return std :: max(query(node << 1, nleft, mid), query((node << 1) + 1, mid + 1, nright));
}
int update_pos, val;
inline void update(int node, int nleft, int nright) {
    if (nleft == nright)
        aint[node] = val;
    else {
        int mid = (nleft + nright) >> 1;
        if (update_pos <= mid)
            update(node << 1, nleft, mid);
        else
            update((node << 1) + 1, mid + 1, nright);
        aint[node] = std :: max(aint[node << 1], aint[(node << 1) + 1]);
    }
}
int x, y, op;
int main() {
    std :: ios_base :: sync_with_stdio(0);
    InParser fin("arbint.in");
    fin >> n >> queries;
    v = (int*)calloc(n + 2, sizeof(int));
    aint = (int*)calloc((n << 2) + 2, sizeof(int));
    for (i = 1; i <= n; ++ i)
        fin >> v[i];
    build(1, 1, n);
    std :: ofstream fout("arbint.out");
    while (queries) {
        -- queries;
        fin >> op >> x >> y;
        if (op == 1) {
            update_pos = x;
            val = y;
            update(1, 1, n);
        }
        else {
            left = x;
            right = y;
            fout << query(1, 1, n) << '\n';
        }
    }
    fout.close();
    return 0;
}