Submission #578401

#TimeUsernameProblemLanguageResultExecution timeMemory
578401JohannTwo Dishes (JOI19_dishes)C++14
0 / 100
767 ms119160 KiB
#include <bits/stdc++.h> using namespace std; #define pii pair<int,int> #define tiii tuple<int,int,int> #define vi vector<int> #define vb vector<bool> #define vpii vector<pii> #define vtiii vector<tiii> #define sz(x) (int) (x).size() #define all(x) (x).begin(), (x).end() struct SegTree { vi arr; int size; void init(vi & H) { size = 1; while (size < sz(H)) size *= 2; arr.assign(2 * size, -1); } void update(int i, int v, int x, int lx, int rx ) { if (rx - lx == 1) { arr[x] = max(arr[x], v); return; } int m = (lx + rx) / 2; if (i < m) update(i, v, 2 * x, lx, m); else update(i, v, 2 * x + 1, m, rx); arr[x] = max(arr[2 * x], arr[2 * x + 1]); } void update(int i, int v) { update(i, v, 1, 0, size); } int query(int l, int r, int x, int lx, int rx ) { if (rx <= l || r <= lx) return -1; if (l <= lx && rx <= r) return arr[x]; int m = (lx + rx) / 2; int a = query(l, r, 2 * x, lx, m); int b = query(l, r, 2 * x + 1, m, rx); return max(a,b); } int query(int l, int r) { return query(l, r, 1, 0, size); } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vi H(n), A(n), B(n); for (int i = 0; i < n; ++i) { cin >> H[i] >> A[i] >> B[i]; } int q; cin >> q; vtiii Q(q); for (int i = 0,l,r; i < q; ++i) { cin >> l >> r; --l; --r; Q[i] = { l, r, i }; } vi ans(q, -1); priority_queue<pii, vpii, greater<pii>> activate; priority_queue<pii, vpii, greater<pii>> deactivate; SegTree seg; seg.init(H); sort(all(Q), [&](tiii & a, tiii & b){ return get<1>(a) > get<1>(b); }); // Niedrige Endpunkte nach hinten, dann kann man den Vektor als queue verwenden vb active(n, false); for (int i = 0; i < n; ++i) { activate.push({i + A[i], i}); deactivate.push({i + B[i], i}); while (!activate.empty() && activate.top().first == i) { active[activate.top().second] = true; activate.pop(); } for (int l = max(0, i - B[i]); l <= i - A[i] && l < n; ++l) { if (active[l]) { if (abs(H[l] - H[i]) == 913359940) { cout << "Stop! " << l << " & " << i << endl; } seg.update(l, abs(H[l] - H[i])); } } while (!Q.empty() && get<1>(Q.back()) == i) { int l, r, idx; tie(l,r,idx) = Q.back(); Q.pop_back(); ans[idx] = seg.query(l,r); } while(!deactivate.empty() && deactivate.top().first == i) { active[deactivate.top().second] = false; deactivate.pop(); } } for (int x : ans) cout << x << "\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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...