Submission #1260298

#TimeUsernameProblemLanguageResultExecution timeMemory
1260298nguynTwo Antennas (JOI19_antennas)C++20
100 / 100
312 ms37136 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define F first
#define S second
#define pb push_back
#define pii pair<int, int>

const int N = 2e5 + 5;
const int inf = 1e9 + 10;

int n, h[N], q, a[N], b[N];
int res[N];
vector<int> ev[N];
vector<pii> que[N];

struct Node {
    int mx, mn, lazy_mx, lazy_mn, res;

    Node() {
        lazy_mx = mx = - inf;
        lazy_mn = mn = inf;
        res = - inf;
    }

    Node operator + (const Node &o) {
        Node ret = Node();
        ret.mx = max(mx, o.mx);
        ret.mn = min(mn, o.mn);
        ret.res = max(res, o.res);
        return ret;
    }

} st[4 * N];

void down(int id) {
    st[id * 2].lazy_mx = max(st[id * 2].lazy_mx, st[id].lazy_mx);
    st[id * 2].lazy_mn = min(st[id * 2].lazy_mn, st[id].lazy_mn);

    st[id * 2 + 1].lazy_mx = max(st[id * 2 + 1].lazy_mx, st[id].lazy_mx);
    st[id * 2 + 1].lazy_mn = min(st[id * 2 + 1].lazy_mn, st[id].lazy_mn);

    st[id * 2].res = max(st[id * 2].res, st[id * 2].mx - st[id].lazy_mn);
    st[id * 2].res = max(st[id * 2].res, st[id].lazy_mx - st[id * 2].mn);

    st[id * 2 + 1].res = max(st[id * 2 + 1].res, st[id * 2 + 1].mx - st[id].lazy_mn);
    st[id * 2 + 1].res = max(st[id * 2 + 1].res, st[id].lazy_mx - st[id * 2 + 1].mn);

    st[id].lazy_mn = inf;
    st[id].lazy_mx = - inf;
}

void update(int id, int l, int r, int i, int x) {
    if (l == r) {
        if (x == inf) {
            st[id].mx = - inf;
            st[id].mn = inf;
        }
        else {
            st[id].mx = st[id].mn = x;
        }
        return;
    }
    down(id);
    int mid = (l + r) / 2;
    if (i <= mid) update(id * 2, l, mid, i, x);
    else update(id * 2 + 1, mid + 1, r, i, x);
    st[id] = st[id * 2] + st[id * 2 + 1];
}

void update(int id, int l, int r, int u, int v, int x) {
    if (l > v || r < u) return;
    if (u <= l && r <= v) {
        st[id].lazy_mx = max(st[id].lazy_mx, x);
        st[id].lazy_mn = min(st[id].lazy_mn, x);
        st[id].res = max(st[id].res, st[id].mx - x);
        st[id].res = max(st[id].res, x - st[id].mn);
        return;
    }
    int mid = (l + r) / 2;
    down(id);
    update(id * 2, l, mid, u, v, x);
    update(id * 2 + 1, mid + 1, r, u, v, x);
    st[id] = st[id * 2] + st[id * 2 + 1];
}

int get(int id, int l, int r, int u, int v) {
    if (l > v || r < u) return - inf;
    if (u <= l && r <= v) {
        return st[id].res;
    }
    int mid = (l + r) / 2;
    down(id);
    return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> h[i];
        cin >> a[i] >> b[i];
        if (i + a[i] <= n) ev[i + a[i]].pb(i);
        if (i + b[i] + 1 <= n) ev[i + b[i] + 1].pb(- i);
    }
    cin >> q;
    for (int i = 1; i <= q; i++) {
        int l, r; cin >> l >> r;
        que[r].pb({l, i});
    }
    for (int i = 1; i <= n; i++) {
        for (auto x : ev[i]) {
            if (x < 0) {
                update(1, 1, n, -x, inf);
            }
            else {
                update(1, 1, n, x, h[x]);
            }
        }
        if (i - a[i] >= 1) update(1, 1, n, max(1, i - b[i]), i - a[i], h[i]);
        for (auto it : que[i]) {
            res[it.S] = max(-1, get(1, 1, n, it.F, i));
        }
    }
    for (int i = 1; i <= q; i++) {
        cout << res[i] << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...