Cod sursa(job #2696750)

Utilizator andrei_C1Andrei Chertes andrei_C1 Data 16 ianuarie 2021 14:54:49
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.13 kb
#include <bits/stdc++.h>
using namespace std;
//ifstream fin("arbint.in");
//ofstream fout("arbint.out");
class InParser {
private :
    FILE* fin;
    char* buf;
    const int lim = 1 << 16;
    int pos;
    inline void next() {
        if(++pos == lim){
            fread(buf, 1, lim, stdin);
            pos = 0;
        }
    }
public :
    InParser (const char *file){
        fin = freopen(file, "r", stdin);
        pos = lim - 1;
        buf = new char[lim]();
    }
    InParser& operator >> (char &x){
        fscanf(fin, "%c", &x);
        return *this;
    }
    template<typename T> InParser& operator >> (T &x){
        x = 0;
        int sgn = 1;
        for(; buf[pos] < '0' || buf[pos] > '9'; next()){
            if(buf[pos]=='-'){
                sgn = -1;
            }
        }
        for(; buf[pos] >= '0' && buf[pos] <= '9'; next()){
            x = x * 10 + buf[pos] - '0';
        }
        x *= sgn;
        return *this;
    }
};
class OutParser {
private:
    FILE *fout;
    char *buf;
    int pos;
    const int lim = 1 << 16;
    void write_ch(char ch) {
        if (pos == lim) {
            fwrite(buf, 1, lim, fout);
            pos = 0;
            buf[pos++] = ch;
        } else {
            buf[pos++] = ch;
        }
    }
public:
    OutParser(const char* file) {
        fout = freopen(file, "w", stdout);
        buf = new char[lim]();
        pos = 0;
    }
    ~OutParser() {
        fwrite(buf, 1, pos, fout);
        fclose(fout);
    }
    template<class T> OutParser& operator << (T x) {
        if(x < 0) {
            write_ch('-');
            x = -x;
        }
        if (x <= 9) {
            write_ch(x + '0');
        } else {
            (*this) << (x / 10);
            write_ch(x % 10 + '0');
        }
        return *this;
    }
    OutParser& operator << (char x) {
        write_ch(x);
        return *this;
    }
    OutParser& operator << (const char *x) {
        while (*x) {
            write_ch(*x);
            ++x;
        }
        return *this;
    }
};
InParser fin("arbint.in");
OutParser fout("arbint.out");
const int INF = 1e9;
int n, m;
vector<int>tree;
void build() {
    tree = vector<int>(2 * n + 4);
    for(int i = 0, x; i < n; i++) {
        fin >> x;
        tree[i + n] = x;
    }
    for(int i = n - 1; i > 0; i--) {
        tree[i] = max(tree[i << 1], tree[(i << 1) + 1]);
    }
}
void update(int k, int value) {
    k += n;
    tree[k] = value;
    for(k >>= 1; k > 0; k >>= 1) {
        tree[k] = max(tree[k << 1], tree[(k << 1) + 1]);
    }
}
int query(int a, int b) {
    int ret = -INF;
    a += n; b += n;
    for(; a <= b; a >>= 1, b >>= 1) {
        if(a & 1) {
            ret = max(ret, tree[a++]);
        }
        if(!(b & 1)) {
            ret = max(ret, tree[b--]);
        }
    }
    return ret;
}
int main() {
    fin >> n >> m;
    build();
    for(int c, a, b; m--; ) {
        fin >> c >> a >> b;
        a--; b--;
        if(c == 0) {
            fout << query(a, b) << '\n';
        } else {
            update(a, b + 1);
        }
    }
    return 0;
}