Submission #519541

#TimeUsernameProblemLanguageResultExecution timeMemory
519541JomnoiJust Long Neckties (JOI20_ho_t1)C++17
100 / 100
151 ms16328 KiB
#include <bits/stdc++.h> #define DEBUG 0 using namespace std; const int N = 2e5 + 10; pair <int, int> a[N]; int b[N], p[N]; int ans[N]; class A { public : int a, b; A() : a(), b() {} A(int l, int r) : a(l), b(r) {} A operator+(const A &o) const { return A(max(a, o.a), max(b, o.b)); } }; class SegmentTree { private : int N; vector <A> tree; public : SegmentTree(int n) : N(n) { tree.resize(4 * N, A()); build(1, 1, N); } void build(int idx, int l, int r) { if(l == r) { tree[idx] = A(max(0, a[l].first - b[l]), max(0, a[l + 1].first - b[l])); return; } int mid = (l + r) / 2; build(idx * 2, l, mid); build(idx * 2 + 1, mid + 1, r); tree[idx] = tree[idx * 2] + tree[idx * 2 + 1]; } A query(int idx, int l, int r, int ql, int qr) { if(r < ql or qr < l) { return A(0, 0); } if(ql <= l and r <= qr) { return tree[idx]; } int mid = (l + r) / 2; return query(idx * 2, l, mid, ql, qr) + query(idx * 2 + 1, mid + 1, r, ql, qr); } }; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; for(int i = 1; i <= n + 1; i++) { cin >> a[i].first; p[i] = i; a[i].second = i; } for(int i = 1; i <= n; i++) { cin >> b[i]; } sort(a + 1, a + n + 2); sort(b + 1, b + n + 1); SegmentTree ST(n); for(int i = 1; i <= n + 1; i++) { ans[a[i].second] = max(ST.query(1, 1, n, 1, i - 1).a, ST.query(1, 1, n, i, n).b); } for(int i = 1; i <= n + 1; i++) { cout << ans[p[i]] << " "; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...