Submission #219714

#TimeUsernameProblemLanguageResultExecution timeMemory
219714manh9203Two Antennas (JOI19_antennas)C++17
100 / 100
945 ms48108 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long const int N = 2e5 + 5; const int INF = 1e9 + 5; int n, q; int H[N], A[N], B[N]; int L[N], R[N]; int ans[N]; vector<int> queRy[N]; vector<int> add[N], sub[N]; struct node { int D, C, lz; } st[4 * N]; void build(int id, int l, int r) { st[id].lz = INF; if (l == r) { st[id].D = st[id].C = -INF; return; } int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); st[id].D = max(st[id << 1].D, st[id << 1 | 1].D); st[id].C = max(st[id << 1].C, st[id << 1 | 1].C); } void pushDown(int id, int l, int r) { if (st[id].lz != INF) { st[id].D = max(st[id].D, st[id].C - st[id].lz); if (l < r) { st[id << 1].lz = min(st[id << 1].lz, st[id].lz); st[id << 1 | 1].lz = min(st[id << 1 | 1].lz, st[id].lz); } st[id].lz = INF; } } void update(int id, int l, int r, int i, int j, int x) { pushDown(id, l, r); if (l > j || r < i || i > j) { return; } if (l >= i && r <= j) { st[id].lz = x; pushDown(id, l, r); return; } int mid = (l + r) >> 1; update(id << 1, l, mid, i, j, x); update(id << 1 | 1, mid + 1, r, i, j, x); st[id].D = max(st[id << 1].D, st[id << 1 | 1].D); st[id].C = max(st[id << 1].C, st[id << 1 | 1].C); } void updPos(int id, int l, int r, int i, int x) { pushDown(id, l, r); if (l > i || r < i) { return; } if (l == r) { st[id].C = x; return; } int mid = (l + r) >> 1; updPos(id << 1, l, mid, i, x); updPos(id << 1 | 1, mid + 1, r, i, x); st[id].D = max(st[id << 1].D, st[id << 1 | 1].D); st[id].C = max(st[id << 1].C, st[id << 1 | 1].C); } int get(int id, int l, int r, int i, int j) { pushDown(id, l, r); if (l > j || r < i || i > j) { return -INF; } if (l >= i && r <= j) { return st[id].D; } int mid = (l + r) >> 1; return max(get(id << 1, l, mid, i, j), get(id << 1 | 1, mid + 1, r, i, j)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> H[i] >> A[i] >> B[i]; add[min(n + 1, i + A[i])].push_back(i); sub[min(n + 1, i + B[i])].push_back(i); } cin >> q; for (int i = 1; i <= q; i++) { cin >> L[i] >> R[i]; queRy[R[i]].push_back(i); ans[i] = -INF; } build(1, 1, n); for (int i = 1; i <= n; i++) { for (int j : add[i]) { updPos(1, 1, n, j, H[j]); } update(1, 1, n, max(0, i - B[i]), max(0, i - A[i]), H[i]); for (int j: queRy[i]) { ans[j] = max(ans[j], get(1, 1, n, L[j], i)); } for (int j: sub[i]) { updPos(1, 1, n, j, -INF); } } for (int i = 1; i <= n; i++) { add[i].clear(); sub[i].clear(); queRy[i].clear(); } for (int i = 1; i <= n; i++) { add[max(0, i - A[i])].push_back(i); sub[max(0, i - B[i])].push_back(i); } for (int i = 1; i <= q; i++) { queRy[L[i]].push_back(i); } build(1, 1, n); for (int i = n; i >= 1; i--) { for (int j : add[i]) { updPos(1, 1, n, j, H[j]); } update(1, 1, n, min(n + 1, i + A[i]), min(n + 1, i + B[i]), H[i]); for (int j: queRy[i]) { ans[j] = max(ans[j], get(1, 1, n, i, R[j])); } for (int j : sub[i]) { updPos(1, 1, n, j, -INF); } } for (int i = 1; i <= q; i++) { if (ans[i] == -INF) { cout << "-1\n"; } else { 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...