Submission #1263700

#TimeUsernameProblemLanguageResultExecution timeMemory
1263700minggaTwo Antennas (JOI19_antennas)C++20
100 / 100
398 ms53756 KiB
// Author: caption_mingle
#include "bits/stdc++.h"

using namespace std;

#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const ll inf = 2e18;
const int N = 2e5 + 7;
int n, q;
ll ans[N];

struct antennas {
    int h, a, b;
} d[N];

struct SEGTREE {
    struct node {
        ll mx, mn, val;
        node() {
            mx = -inf;
            mn = inf;
            val = -inf;
        }
        node(ll x, ll y, ll z) {
            mx = x, mn = y, val = z;
        }
        node operator + (const node& o) {
            int mx1 = max(mx, o.mx);
            int mn1 = min(mn, o.mn);
            int val1 = max(val, o.val);
            return node(mx1, mn1, val1);
        }
    };
    vector<node> st;
    vector<pair<ll, ll>> lazy;
    SEGTREE(int n) {
        st.resize(n * 4 + 4, node());
        lazy.resize(n * 4 + 4, {-inf, inf});
    }
    void push(int i) {
        auto [mx, mn] = lazy[i];
        lazy[i] = {-inf, inf};
        lazy[i * 2].fi = max(mx, lazy[i * 2].fi);
        lazy[i * 2].se = min(mn, lazy[i * 2].se);
        lazy[i * 2 + 1].fi = max(mx, lazy[i * 2 + 1].fi);
        lazy[i * 2 + 1].se = min(mn, lazy[i * 2 + 1].se);
        st[i * 2].val = max({st[i * 2].val, st[i * 2].mx - mn, mx - st[i * 2].mn});
        st[i * 2 + 1].val = max({st[i * 2 + 1].val, st[i * 2 + 1].mx - mn, mx - st[i * 2 + 1].mn});
    }
    void update(int i, int l, int r, int u, ll x) {
        if(l > u or r < u) return ;
        if(l == r) {
            if(x == inf) st[i].mx = -inf, st[i].mn = inf;
            else st[i].mx = st[i].mn = x;
            return;
        }
        int m = (l + r) >> 1;
        push(i);
        update(i * 2, l, m, u, x);
        update(i * 2 + 1, m + 1, r, u, x);
        st[i] = st[i * 2] + st[i * 2 + 1];
    }
    void update(int i, int l, int r, int u, int v, ll x) {
        if(l > v or r < u) return ;
        if(u <= l and r <= v) {
            lazy[i].fi = max(lazy[i].fi, x);
            lazy[i].se = min(lazy[i].se, x);
            st[i].val = max({st[i].val, st[i].mx - x, x - st[i].mn});
            // cerr << l << ' ' << r << ' ' << lazy[i].fi << ' ' << lazy[i].se << ' ' << st[i].mx << ' ' << st[i].val << ln;
            return;
        }
        int m = (l + r) >> 1;
        push(i);
        update(i * 2, l, m, u, v, x);
        update(i * 2 + 1, m + 1, r, u, v, x);
        st[i] = st[i * 2] + st[i * 2 + 1];
    }
    node get(int i, int l, int r, int u, int v) {
        if(l > v or r < u) return node();
        if(u <= l and r <= v) {
            return st[i];
        }
        int m = (l + r) >> 1;
        push(i);
        return get(i * 2, l, m, u, v) + get(i * 2 + 1, m + 1, r, u, v);
    }
    void update(int u, ll x) {
        update(1, 1, n, u, x);
    }
    void update(int l, int r, ll x) {
        update(1, 1, n, l, r, x);
    }
    ll get(int l, int r) {
        return get(1, 1, n, l, r).val;
    }
};

vector<pair<int, int>> ev[N], que[N];


signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    #define task ""
    if(fopen(task ".INP", "r")) {
        freopen(task ".INP", "r", stdin);
        freopen(task ".OUT", "w", stdout);
    }
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int h, a, b; cin >> h >> a >> b;
        d[i] = {h, a, b};
        if(i + a <= n) ev[i + a].pb({i, 1});
        if(i + b + 1 <= n) ev[i + b + 1].pb({i, -1});
    }
    cin >> q;
    for(int i = 1; i <= q; i++) {
        int l, r; cin >> l >> r;
        que[r].pb({l, i});
    }
    SEGTREE st(n);
    for(int i = 1; i <= n; i++) {
        for(auto [x, sign] : ev[i]) {
            if(sign == 1) st.update(x, d[x].h);
            else st.update(x, inf);
        }
        if(i - d[i].a >= 1) {
            st.update(max(1, i - d[i].b), i - d[i].a, d[i].h);
        }
        for(auto [l, id] : que[i]) {
            ans[id] = st.get(l, i);
            if(ans[id] < 0) ans[id] = -1;
        }
    }
    for(int i = 1; i <= q; i++) cout << ans[i] << ln;
    cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}

Compilation message (stderr)

antennas.cpp: In function 'int main()':
antennas.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |         freopen(task ".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         freopen(task ".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...