Submission #532815

#TimeUsernameProblemLanguageResultExecution timeMemory
5328158e7Two Antennas (JOI19_antennas)C++17
100 / 100
866 ms46656 KiB
//Challenge: Accepted #include <bits/stdc++.h> using namespace std; #ifdef zisk void debug(){cout << endl;} template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);} template<class T> void pary(T l, T r) { while (l != r) cout << *l << " ", l++; cout << endl; } #else #define debug(...) 0 #define pary(...) 0 #endif #define ll long long #define maxn 200005 #define pii pair<int, int> #define ff first #define ss second #define io ios_base::sync_with_stdio(0);cin.tie(0); const ll inf = 1<<30; struct beats{ ll seg[4*maxn], tag[4*maxn], mi[4*maxn]; void push(int cur, int l, int r) { if (tag[cur] == -inf) return; seg[cur] = max(seg[cur], tag[cur] - mi[cur]); if (r - l > 1) { tag[cur*2] = max(tag[cur*2], tag[cur]); tag[cur*2+1] = max(tag[cur*2+1], tag[cur]); } tag[cur] = -inf; } void pull(int cur, int l, int r) { int m = (l + r) / 2; if (r - l > 1) { push(cur*2, l, m); push(cur*2+1, m, r); seg[cur] = max(seg[cur], max(seg[cur*2], seg[cur*2+1])); mi[cur] = min(mi[cur*2], mi[cur*2+1]); } } void init(int cur, int l, int r) { if (r <= l) return; tag[cur] = -inf; seg[cur] = -inf; if (r - l == 1) { mi[cur] = inf; return; } int m = (l + r) / 2; init(cur*2, l, m), init(cur*2+1, m, r); pull(cur, l, r); } void modify(int cur, int l, int r, int ind, ll val) { if (r <= l) return; push(cur, l, r); if (r - l == 1) { mi[cur] = val; return; } int m = (l + r) / 2; if (ind < m) modify(cur*2, l, m, ind, val); else modify(cur*2+1, m, r, ind, val); pull(cur, l, r); } void chmax(int cur, int l, int r, int ql, int qr, ll val) { if (r <= l || ql >= r || qr <= l) return; if (ql <= l && qr >= r) { tag[cur] = max(tag[cur], val); return; } int m = (l + r) / 2; chmax(cur*2, l, m, ql, qr, val); chmax(cur*2+1, m, r, ql, qr, val); pull(cur, l, r); } ll query(int cur, int l, int r, int ql, int qr) { if (r <= l || ql >= r || qr <= l) return -inf; push(cur, l, r); if (ql <= l && qr >= r) { pull(cur, l, r); //debug(l, r, seg[cur]); return seg[cur]; } int m = (l + r) / 2; return max(query(cur*2, l, m, ql, qr), query(cur*2+1, m, r, ql, qr)); } } tr; ll h[maxn], ans[maxn]; int a[maxn], b[maxn]; vector<pii> que[maxn], ev[maxn]; void solve(int n) { tr.init(1, 0, n); for (int i = 0;i < n;i++) ev[i].clear(); for (int i = 0;i < n;i++) { //debug(i); for (auto [id, ch]:ev[i]) { //debug("mod", id, ch); if (ch == 1) tr.modify(1, 0, n, id, h[id]); else tr.modify(1, 0, n, id, inf); } int l = max(0, i - b[i]), r = max(-1, i - a[i]); //debug("chmax", l, r, h[i]); tr.chmax(1, 0, n, l, r+1, h[i]); for (auto [lef, id]:que[i]) { //debug("que", id, tr.query(1, 0, n, lef, i+1)); ans[id] = max(ans[id], tr.query(1, 0, n, lef, i+1)); } l = min(n, i+a[i]), r = min(n, i + b[i] + 1); ev[l].push_back({i, 1}), ev[r].push_back({i, -1}); } } int main() { io int n; cin >> n; for (int i = 0;i < n;i++) { cin >> h[i] >> a[i] >> b[i]; } int q; cin >> q; for (int i = 0;i < q;i++) { ans[i] = -1; int l, r; cin >> l >> r; r--, l--; que[r].push_back({l, i}); } solve(n); for (int i = 0;i < n;i++) h[i] = -h[i]; solve(n); for (int i = 0;i < q;i++) cout << ans[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...