/* -*- coding: utf-8 -*- * * 1950.cc: No.1950 片道きゃっちぼーる - yukicoder */ #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; /* typedef */ typedef vector vi; typedef vector vb; typedef vector vvi; typedef stack si; typedef queue qi; /* global variables */ int xs[MAX_N], as[MAX_N]; vi nbrs[MAX_N], gnbrs[MAX_N]; int gids[MAX_N], gys[MAX_N], pns[MAX_N]; /* subroutines */ void scc_visit(const vi *nbrs, int v, vvi& scc, si& S, vb &inS, vi& low, vi& num, int& time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; const vi& nbrv = nbrs[v]; for (vi::const_iterator vit = nbrv.begin(); vit != nbrv.end(); vit++) { const int& w = *vit; if (num[w] == 0) { scc_visit(nbrs, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(vi()); for (;;) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void calc_scc(const int n, const vi *nbrs, vvi& scc) { vi num(n), low(n); si S; vb inS(n); int time = 0; for (int u = 0; u < n; u++) if (num[u] == 0) scc_visit(nbrs, u, scc, S, inS, low, num, time); } /* main */ int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", xs + i); for (int i = 0; i < n; i++) scanf("%d", as + i); for (int i = 0; i < n; i++) { int x0 = xs[i] - as[i], x1 = xs[i] + as[i]; int j0 = lower_bound(xs, xs + n, x0) - xs; int j1 = lower_bound(xs, xs + n, x1) - xs; if (j0 < n && xs[j0] == x0) nbrs[j0].push_back(i); if (j1 < n && xs[j1] == x1) nbrs[j1].push_back(i); } vvi scc; calc_scc(n, nbrs, scc); int gn = scc.size(); for (int id = 0; id < gn; id++) for (auto u: scc[id]) { gids[u] = id; gys[id] = max(gys[id], xs[u] + as[u]); } for (int u = 0; u < n; u++) { int gu = gids[u]; for (auto v: nbrs[u]) { int gv = gids[v]; if (gu != gv) gnbrs[gu].push_back(gv), pns[gv]++; } } qi q; for (int u = 0; u < gn; u++) if (pns[u] == 0) q.push(u); while (! q.empty()) { int u = q.front(); q.pop(); for (auto v: gnbrs[u]) { gys[v] = max(gys[v], gys[u]); if (--pns[v] == 0) q.push(v); } } for (int i = 0; i < n; i++) printf("%d\n", gys[gids[i]] - xs[i]); return 0; }