Submission #872576

#TimeUsernameProblemLanguageResultExecution timeMemory
872576serifefedartarTwo Antennas (JOI19_antennas)C++17
100 / 100
671 ms86252 KiB
#include <bits/stdc++.h> using namespace std; #define fast ios::sync_with_stdio(0);cin.tie(0); #define s second #define f first typedef long long ll; const ll MOD = 1e9+9; const ll LOGN = 20; const ll MAXN = 2e5 + 100; struct Ant { ll H, A, B; Ant() { } void read() { cin >> H >> A >> B; } }; vector<Ant> antenna; vector<pair<int,int>> query; ll sg[4*MAXN], negative[4*MAXN], positive[4*MAXN], lazy[4*MAXN], ans[MAXN]; vector<int> add[2*MAXN], rem[2*MAXN]; vector<pair<int,int>> qs[2*MAXN]; int n, q; void push(int k, int a, int b) { sg[k] = max(sg[k], negative[k] + lazy[k]); if (a != b) { lazy[2*k] = max(lazy[2*k], lazy[k]); lazy[2*k+1] = max(lazy[2*k+1], lazy[k]); } lazy[k] = 0; } void update_pos(int k, int a, int b, int q_l, int q_r, ll val) { push(k, a, b); if (b < q_l || a > q_r) return ; if (q_l <= a && b <= q_r) { lazy[k] = val; push(k, a, b); return ; } update_pos(2*k, a, (a+b)/2, q_l, q_r, val); update_pos(2*k+1, (a+b)/2+1, b, q_l, q_r, val); sg[k] = max(sg[k], max(sg[2*k], sg[2*k+1])); } void update(int k, int a, int b, int plc, ll val) { push(k, a, b); if (b < plc || a > plc) return ; if (a == b) { negative[k] = val; return ; } update(2*k, a, (a+b)/2, plc, val); update(2*k+1, (a+b)/2+1, b, plc, val); negative[k] = max(negative[2*k], negative[2*k+1]); } ll qry(int k, int a, int b, int q_l, int q_r) { push(k, a, b); if (b < q_l || a > q_r) return -1e12; if (q_l <= a && b <= q_r) return sg[k]; return max(qry(2*k, a, (a+b)/2, q_l, q_r), qry(2*k+1, (a+b)/2+1, b, q_l, q_r)); } void solve() { for (int i = 0; i < 4*MAXN; i++) sg[i] = -1e12, negative[i] = -1e12, lazy[i] = 0; for (int i = 0; i < 2*MAXN; i++) add[i].clear(), rem[i].clear(), qs[i].clear(); for (int i = 0; i < q; i++) qs[query[i].s].push_back({query[i].f, i}); for (int i = 1; i <= n; i++) add[i+antenna[i].A].push_back(i), rem[i+antenna[i].B+1].push_back(i); for (int i = 1; i <= n; i++) { for (auto u : rem[i]) update(1, 1, n, u, -1e12); for (auto u : add[i]) update(1, 1, n, u, -antenna[u].H); if (i - antenna[i].A >= 1) update_pos(1, 1, n, max(1LL, i - antenna[i].B), max(1LL, i - antenna[i].A), antenna[i].H); for (auto u : qs[i]) ans[u.s] = max(ans[u.s], qry(1, 1, n, u.f, i)); } } int main() { fast cin >> n; antenna = vector<Ant>(n+1); for (int i = 1; i <= n; i++) antenna[i].read(); int l, r; cin >> q; for (int i = 0; i < q; i++) { cin >> l >> r; query.push_back({l, r}); } for (int i = 0; i < q; i++) ans[i] = -1; solve(); for (int i = 1; i < n+1-i; i++) swap(antenna[i], antenna[n+1-i]); for (int i = 0; i < q; i++) { int new_s = n + 1 - query[i].f; int new_f = n + 1 - query[i].s; query[i].f = new_f; query[i].s = new_s; } solve(); 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...