1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <iostream> #include <algorithm> #define maxn 100010 #define ll long long using namespace std;
int n;
ll a[maxn], b[maxn];
inline ll F(int x, int y) { return a[x] + b[y - x]; }
struct Queue { int l, r, k; } Q[maxn]; int h, t;
int bs(int x) { int l = Q[t].l, r = Q[t].r, mid, ans = Q[t].r + 1; while (l <= r) { mid = l + r >> 1; if (F(x, mid) >= F(Q[t].k, mid)) ans = mid, r = mid - 1; else l = mid + 1; } return ans; }
ll f[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n; for (int i = 0; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) cin >> b[i]; sort(b + 1, b + n + 1, greater<ll>()); for (int i = 1; i <= n; ++i) b[i] += b[i - 1]; Q[h = t = 1] = { 1, n, 0 }; for (int i = 1; i <= n; ++i) { while (h < t && Q[h].r < i) ++h; f[i] = F(Q[h].k, i); while (h < t && i < Q[t].l && F(i, Q[t].l) >= F(Q[t].k, Q[t].l)) --t; int p = bs(i); Q[t].r = p - 1; if (p <= n) Q[++t] = { p, n, i }; } for (int i = 1; i <= n; ++i) cout << f[i] << " "; return 0; }
|