Submission #1018132

#TimeUsernameProblemLanguageResultExecution timeMemory
1018132MilosMilutinovicSanta Claus (RMI19_santa)C++14
20 / 100
1069 ms9564 KiB
#include <bits/stdc++.h> using namespace std; struct node { int sum; int mn; }; node Merge(node a, node b) { node ret; ret.sum = a.sum + b.sum; ret.mn = min(a.mn, a.sum + b.mn); return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; while (tt--) { int n; cin >> n; vector<int> x(n); for (int i = 0; i < n; i++) { cin >> x[i]; } vector<int> h(n); for (int i = 0; i < n; i++) { cin >> h[i]; } vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } auto xs = v; sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); for (int i = 0; i < n; i++) { v[i] = (int) (lower_bound(xs.begin(), xs.end(), v[i]) - xs.begin()); } vector<node> st(8 * n); function<void(int, int, int)> Build = [&](int id, int l, int r) { st[id].sum = 0; st[id].mn = 0; if (l == r) { return; } int mid = (l + r) >> 1; Build(id * 2, l, mid); Build(id * 2 + 1, mid + 1, r); }; function<void(int, int, int, int, int)> Modify = [&](int id, int l, int r, int i, int v) { if (l == r) { st[id].sum += v; st[id].mn += v; return; } int mid = (l + r) >> 1; if (i <= mid) { Modify(id * 2, l, mid, i, v); } else { Modify(id * 2 + 1, mid + 1, r, i, v); } st[id] = Merge(st[id * 2], st[id * 2 + 1]); }; auto Check = [&](int from, int to) { Build(1, 0, n - 1); for (int i = 0; i <= to; i++) { if (x[i] >= from && h[i] == 1) { Modify(1, 0, n - 1, v[i], +1); } } for (int i = to; i >= 0; i--) { if (x[i] >= from && h[i] == 1) { continue; } if (h[i] == 0) { Modify(1, 0, n - 1, v[i], -1); } else { Modify(1, 0, n - 1, v[i], +1); } if (st[1].mn < 0) { return false; } } return true; }; int limit = 0; for (int i = 0; i < n; i++) { if (h[i] == 0) { limit = i; } } for (int i = 0; i < n; i++) { if (i < limit) { cout << -1 << " "; continue; } int low = 0, high = i, p = -1; while (low <= high) { int mid = (low + high) >> 1; if (Check(x[mid], i)) { p = mid; low = mid + 1; } else { high = mid - 1; } } if (p == -1) { cout << -1 << " "; } else { cout << 2 * x[i] - x[p] << " "; } } cout << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...