Submission #645859

#TimeUsernameProblemLanguageResultExecution timeMemory
645859Alex_tz307Santa Claus (RMI19_santa)C++17
100 / 100
246 ms13592 KiB
#include <bits/stdc++.h> using namespace std; struct node { int sum, minSum; }; struct ST { int n; vector<node> t; ST(int N) : n(N) { int dim = 1; while (dim < n) { dim *= 2; } t.resize(dim * 2); } void update(int x, int lx, int rx, int pos, int v) { if (lx == rx) { t[x].sum += v; t[x].minSum += v; return; } int mid = (lx + rx) / 2; if (pos <= mid) { update(x * 2, lx, mid, pos, v); } else { update(x * 2 + 1, mid + 1, rx, pos, v); } t[x].sum = t[x * 2].sum + t[x * 2 + 1].sum; t[x].minSum = min(t[x * 2].minSum, t[x * 2].sum + t[x * 2 + 1].minSum); } void update(int pos, int v) { update(1, 1, n, pos, v); } int minPref() { return t[1].minSum; } }; void testCase() { int n; cin >> n; vector<int> x(n + 1), h(n+ 1), v(n + 1); for (int i = 1; i <= n; ++i) { cin >> x[i]; } for (int i = 1; i <= n; ++i) { cin >> h[i]; } for (int i = 1; i <= n; ++i) { cin >> v[i]; v[i] += 1; } vector<int> elf(n + 1); multiset<int> presents; for (int i = 1; i <= n; ++i) { if (h[i] == 0) { presents.emplace(v[i]); } else { auto it = presents.lower_bound(v[i]); if (it != presents.end()) { elf[i] = *it; presents.erase(it); } } } int last = 0; for (int i = 1; i <= n; ++i) { if (h[i] == 0) { last = i; } } ST t(n + 1); int r; for (r = 1; r <= n; ++r) { if (h[r] == 0) { t.update(v[r], -1); } else { t.update(v[r], 1); } if (r >= last && t.minPref() >= 0) { break; } } vector<int> sol(n + 1, -1); int l = 1; while (r <= n) { while (l < r) { if (h[l] == 1) { t.update(v[l], -1); if (elf[l]) { t.update(elf[l], 1); } if (t.minPref() < 0) { t.update(v[l], 1); if (elf[l]) { t.update(elf[l], -1); } break; } } l += 1; } sol[r] = 2 * x[r] - x[l]; if (r + 1 <= n) { t.update(v[r + 1], 1); } r += 1; } for (int i = 1; i <= n; ++i) { cout << sol[i] << ' '; } cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int tests; cin >> tests; for (int tc = 0; tc < tests; ++tc) { testCase(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...