Cod sursa(job #1877723)

Utilizator tudormaximTudor Maxim tudormaxim Data 13 februarie 2017 18:11:01
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.05 kb
#include <bits/stdc++.h>
using namespace std;

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

const int maxn = 1e5 + 5;
int V[maxn], B[maxn >> 8], rad;

class Scanner {
private:
    char Buff[1 << 17];
    int i = (1 << 17) - 1;
    void Advance() {
        if (++i == (1 << 17)) {
            f.read(Buff, 1 << 17);
            i = 0;
        }
    }
public:
    inline Scanner& operator >> (int &val) {
        while (!isdigit(Buff[i])) {
            Advance();
        }
        val = 0;
        while (isdigit(Buff[i])) {
            val = (val << 1) + (val << 3) + (Buff[i] - '0');
            Advance();
        }
        return *this;
    }
}fin;

inline int Bucket (int n) {
    return (n >> 8);
}

inline int First (int n) {
    return (n << 8);
}

void Update (int pos, int val) {
    int i = Bucket(pos), j;
    if (V[pos] == B[i]) {
        V[pos] = 0;
        B[i] = 0;
        for (j = First(i); j < First(i + 1); j++) {
            B[i] = max(B[i], V[j]);
        }
    }
    V[pos] = val;
    B[i] = max(B[i], val);
}

int Query (int x, int y) {
    int ans = 0, i;
    int left = Bucket(x);
    int right = Bucket(y);
    for (i = left + 1; i < right; i++) {
        ans = max(ans, B[i]);
    }
    if (ans < B[left]) {
        for (i = x; i <= y && i < First(left + 1); i++) {
            ans = max(ans, V[i]);
        }
    }
    if (ans < B[right]) {
        for (i = max(First(right), x); i <= y; i++) {
            ans = max(ans, V[i]);
        }
    }
    return ans;
}

int main () {
    ios_base :: sync_with_stdio (false);
    int n, m, i, type, a, b;
    fin >> n >> m;
    for (i = 0; i < n; i++) {
        fin >> V[i];
        if (V[i] > B[Bucket(i)]) {
            B[Bucket(i)] = V[i];
        }
    }
    while (m--) {
        fin >> type >> a >> b;
        if (type == 1) {
            a--;
            Update(a, b);
        } else {
            a--, b--;
            fout << Query(a, b) << "\n";
        }
    }
    f.close();
    fout.close();
    return 0;
}