Cod sursa(job #1877718)

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

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

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

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";
        }
    }
    fin.close();
    fout.close();
    return 0;
}