Cod sursa(job #1828893)

Utilizator tudormaximTudor Maxim tudormaxim Data 13 decembrie 2016 23:55:43
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.76 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

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

const int maxn = 1e5 + 5;
int Aint[maxn << 1], n;

class Scanner {
public:
    inline Scanner& operator >> (int &val) {
        while (!(Buff[pos] >= '0' && Buff[pos] <= '9')) {
            Advance();
        }
        val = 0;
        while (Buff[pos] >= '0' && Buff[pos] <= '9') {
            val = (val << 1) + (val << 3) + (Buff[pos] - '0');
            Advance();
        }
        return *this;
    }
private:
    char Buff[1 << 19];
    int pos = (1 << 19) - 1;
    void Advance() {
        if (++pos == (1 << 19)) {
            pos = 0;
            f.read(Buff, 1 << 19);
        }
    }
} fin;
void Build() {
    for (int i = n - 1; i >= 0; i--) {
        Aint[i] = max(Aint[i << 1], Aint[i << 1 | 1]);
    }
}

void Update(int pos, int val) {
    int i;
    pos += n - 1;
    Aint[pos] = val;
    while (pos > 1) {
        i = pos >> 1;
        Aint[i] = max(Aint[pos], Aint[pos ^ 1]);
        pos = i;
    }
}

int Query(int left, int right) {
    int ans = -1;
    left += n - 1;
    right += n - 1;
    while (left <= right) {
        ans = max(ans, max(Aint[left], Aint[right]));
        left = (left + 1) >> 1;
        right = (right - 1) >> 1;
    }
    return ans;
}

int main() {
    ios_base :: sync_with_stdio(false);
    int m, x, y, op, i;
    fin >> n >> m;
    for (i = 1; i <= n; i++) {
        fin >> Aint[i + n - 1];
    }
    Build();
    while (m--) {
        fin >> op >> x >> y;
        if (op == 1) {
            Update(x, y);
        } else {
            fout << Query(x, y) << "\n";
        }
    }
    f.close();
    fout.close();
    return 0;
}