#line 2 "library/other/template.hpp" #define _CRT_SECURE_NO_WARNINGS #ifndef __clang__ #ifdef ONLINE_JUDGE #pragma GCC target("avx512f") #elif defined EVAL #else #pragma GCC target("avx2") #endif #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i, n) for (int i = 0; i < int(n); i++) #define REP(i, n) for (int i = 1; i <= int(n); i++) #define all(V) V.begin(), V.end() using i128 = __int128_t; using u128 = __uint128_t; using uint = unsigned int; using lint = long long; using ulint = unsigned long long; using IP = std::pair; using LP = std::pair; constexpr int INF = INT_MAX / 2; constexpr lint LINF = LLONG_MAX / 2; constexpr double eps = DBL_EPSILON; constexpr double PI = 3.141592653589793238462643383279; template class prique : public std::priority_queue, std::greater> { }; int popcount(uint x) { #if __cplusplus >= 202002L return std::popcount(x); #else #ifndef __clang__ return __builtin_popcount(x); #endif #endif x = (x & 0x55555555) + (x >> 1 & 0x55555555); x = (x & 0x33333333) + (x >> 2 & 0x33333333); x = (x & 0x0f0f0f0f) + (x >> 4 & 0x0f0f0f0f); x = (x & 0x00ff00ff) + (x >> 8 & 0x00ff00ff); return (x & 0x0000ffff) + (x >> 16 & 0x0000ffff); } template inline constexpr decltype(auto) lambda_fix(F&& f) { return [f = std::forward(f)](auto&&... args) { return f(f, std::forward(args)...); }; } template constexpr std::vector make_vec(size_t n) { return std::vector(n); } template constexpr auto make_vec(size_t n, Args&&... args) { return std::vector(args...))>( n, make_vec(std::forward(args)...)); } template constexpr inline bool chmax(T& lhs, const U& rhs) noexcept { if (lhs < rhs) { lhs = rhs; return true; } return false; } template constexpr inline bool chmin(T& lhs, const U& rhs) noexcept { if (lhs > rhs) { lhs = rhs; return true; } return false; } constexpr inline lint gcd(lint a, lint b) noexcept { while (b) { lint c = a; a = b; b = c % b; } return a; } inline lint lcm(lint a, lint b) noexcept { return a / gcd(a, b) * b; } constexpr bool isprime(lint n) noexcept { if (n == 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } template constexpr T mypow(T a, lint b) noexcept { T res(1); while (true) { if (b & 1) res *= a; b >>= 1; if (!b) break; a *= a; } return res; } constexpr lint modpow(lint a, lint b, lint m) noexcept { a %= m; lint res(1); while (b) { if (b & 1) { res *= a; res %= m; } a *= a; a %= m; b >>= 1; } return res; } template constexpr void printArray(const std::vector& vec, char split = ' ') { rep(i, vec.size()) { std::cout << vec[i]; std::cout << (i == (int)vec.size() - 1 ? '\n' : split); } } template constexpr void printArray(InputIter l, InputIter r, char split = ' ') { auto rprev = std::prev(r); for (auto i = l; i != r; i++) { std::cout << *i; std::cout << (i == rprev ? '\n' : split); } } LP extGcd(lint a, lint b) noexcept { if (b == 0) return {1, 0}; LP s = extGcd(b, a % b); std::swap(s.first, s.second); s.second -= a / b * s.first; return s; } LP ChineseRem(const lint& b1, const lint& m1, const lint& b2, const lint& m2) noexcept { auto p = extGcd(m1, m2); lint g = gcd(m1, m2), l = m1 / g * m2; lint tmp = (b2 - b1) / g * p.first % (m2 / g); lint r = (b1 + m1 * tmp + l) % l; return {r, l}; } int LCS(const std::string& a, const std::string& b) { auto dp = make_vec(a.size() + 1, b.size() + 1); rep(i, a.size()) { rep(j, b.size()) { chmax(dp[i + 1][j], dp[i][j]); chmax(dp[i][j + 1], dp[i][j]); if (a[i] == b[j]) chmax(dp[i + 1][j + 1], dp[i][j] + 1); } chmax(dp[i + 1][b.size()], dp[i][b.size()]); } rep(j, b.size()) chmax(dp[a.size()][j + 1], dp[a.size()][j]); return dp[a.size()][b.size()]; } template ::value, std::nullptr_t> = nullptr> void compress(std::vector& vec) { auto tmp = vec; std::sort(all(tmp)); tmp.erase(std::unique(all(tmp)), tmp.end()); for (T& i : vec) i = std::lower_bound(all(tmp), i) - tmp.begin(); } template void compress(T* l, T* r) { std::vector tmp(l, r); std::sort(all(tmp)); tmp.erase(std::unique(all(tmp)), tmp.end()); for (auto i = l; i < r; i++) { *i = std::lower_bound(all(tmp), *i) - tmp.begin(); } } template void compress(InputIter l, InputIter r) { std::vector tmp(l, r); std::sort(all(tmp)); tmp.erase(std::unique(all(tmp)), tmp.end()); for (auto i = l; i < r; i++) { *i = std::lower_bound(all(tmp), *i) - tmp.begin(); } } #line 3 "library/data-structure/SegTree.hpp" template class SegTree { protected: unsigned int n = 1, rank = 0; std::vector node; T ident; public: SegTree(unsigned int m, T e_) : ident(e_) { while (n < m) { n *= 2; rank++; } node.resize(2 * n, ident); } SegTree(unsigned int m, T init, T e_) : ident(e_) { while (n < m) { n *= 2; rank++; } node.resize(2 * n, ident); for (unsigned int i = n; i < 2 * n; i++) node[i] = init; for (unsigned int i = n - 1; i > 0; i--) node[i] = nodef(node[i << 1], node[i << 1 | 1]); } template SegTree(const std::vector& initvec, T e_) : ident(e_) { unsigned int m = initvec.size(); while (n < m) { n *= 2; rank++; } node.resize(2 * n, ident); for (unsigned int i = n; i < 2 * n; i++) { if (i - n < m) node[i] = initvec[i - n]; } for (unsigned int i = n - 1; i > 0; i--) node[i] = nodef(node[i << 1], node[i << 1 | 1]); } void update(int i, T x) { i += n; node[i] = x; while (i != 1) { i >>= 1; node[i] = nodef(node[2 * i], node[2 * i + 1]); } } T query(int l, int r) const { l += n; r += n; T ls = ident, rs = ident; while (l < r) { if (l & 1) ls = nodef(ls, node[l++]); if (r & 1) rs = nodef(node[--r], rs); l >>= 1; r >>= 1; } return nodef(ls, rs); } const T& operator[](const int& x) const { return node[n + x]; } T queryForAll() const { return node[1]; } private: template int max_right(int st, F& check, T& acc, int k, int l, int r) const { if (l + 1 == r) { acc = nodef(acc, node[k]); return check(acc) ? -1 : k - n; } int m = (l + r) >> 1; if (m <= st) return max_right(st, check, acc, (k << 1) | 1, m, r); if (st <= l && check(nodef(acc, node[k]))) { acc = nodef(acc, node[k]); return -1; } int vl = max_right(st, check, acc, k << 1, l, m); if (vl != -1) return vl; return max_right(st, check, acc, (k << 1) | 1, m, r); } public: template int max_right(int st, F check) const { T acc = ident; return max_right(st, check, acc, 1, 0, n); } template int max_right(int st) const { T acc = ident; return max_right(st, check, acc, 1, 0, n); } }; namespace { lint RSQ_nodef(const lint& lhs, const lint& rhs) { return lhs + rhs; } lint RMiQ_nodef(const lint& lhs, const lint& rhs) { return std::min(lhs, rhs); } lint RMaQ_nodef(const lint& lhs, const lint& rhs) { return std::max(lhs, rhs); } } // namespace class RSQ : public SegTree { using Base = SegTree; public: template RSQ(Args&&... args) : Base(std::forward(args)..., 0) {} }; class RMiQ : public SegTree { using Base = SegTree; public: template RMiQ(Args&&... args) : Base(std::forward(args)..., LINF) {} }; class RMaQ : public SegTree { using Base = SegTree; public: template RMaQ(Args&&... args) : Base(std::forward(args)..., -LINF) {} }; /** * @title Segment Tree */ #line 4 "library/data-structure/IntervalSegTree.hpp" template class IntervalSegTree : public SegTree { using Base = SegTree; using Base::ident; using Base::n; using Base::node; using Base::rank; std::vector lazy; std::vector lazyflag; std::vector width; void eval(int k) { for (int i = rank; i > 0; i--) { int nk = k >> i; if (lazyflag[nk]) { updf(node[2 * nk], lazy[nk], width[2 * nk]); updf(node[2 * nk + 1], lazy[nk], width[2 * nk + 1]); if (lazyflag[2 * nk]) lazyf(lazy[2 * nk], lazy[nk]); else lazy[2 * nk] = lazy[nk]; if (lazyflag[2 * nk + 1]) lazyf(lazy[2 * nk + 1], lazy[nk]); else lazy[2 * nk + 1] = lazy[nk]; lazyflag[2 * nk] = lazyflag[2 * nk + 1] = true; lazyflag[nk] = false; } } } public: IntervalSegTree(unsigned int m, T e_) : Base(m, T(), e_) { lazy.resize(2 * n); lazyflag.resize(2 * n); width.resize(2 * n); width[1] = n; for (unsigned int i = 2; i < 2 * n; i++) { width[i] = width[i >> 1] >> 1; } } IntervalSegTree(unsigned int m, T init, T e_) : Base(m, init, e_) { lazy.resize(2 * n); lazyflag.resize(2 * n); width.resize(2 * n); width[1] = n; for (unsigned int i = 2; i < 2 * n; i++) { width[i] = width[i >> 1] >> 1; } } IntervalSegTree(const std::vector& initvec, T e_) : Base(initvec, e_) { lazy.resize(2 * n); lazyflag.resize(2 * n); width.resize(2 * n); width[1] = n; for (unsigned int i = 2; i < 2 * n; i++) { width[i] = width[i >> 1] >> 1; } } void update(int i, U x) { i += n; eval(i); updf(node[i], x, width[i]); if (lazyflag[i]) lazyf(lazy[i], x); else { lazyflag[i] = true; lazy[i] = x; } while (i /= 2) node[i] = nodef(node[2 * i], node[2 * i + 1]); } void update(int l, int r, U x) { l += n, r += n; int nl = l, nr = r; while (!(nl & 1)) nl >>= 1; while (!(nr & 1)) nr >>= 1; nr--; eval(nl), eval(nr); while (l < r) { if (l & 1) { updf(node[l], x, width[l]); if (lazyflag[l]) lazyf(lazy[l], x); else { lazyflag[l] = true; lazy[l] = x; } l++; } if (r & 1) { r--; updf(node[r], x, width[r]); if (lazyflag[r]) lazyf(lazy[r], x); else { lazyflag[r] = true; lazy[r] = x; } } l >>= 1, r >>= 1; } while (nl >>= 1) node[nl] = nodef(node[2 * nl], node[2 * nl + 1]); while (nr >>= 1) node[nr] = nodef(node[2 * nr], node[2 * nr + 1]); } T query(int l, int r) { l += n, r += n, eval(l); eval(r - 1); T ls = ident, rs = ident; while (l < r) { if (l & 1) ls = nodef(ls, node[l++]); if (r & 1) rs = nodef(node[--r], rs); l >>= 1, r >>= 1; } return nodef(ls, rs); } T operator[](const int& x) { eval(n + x); return node[n + x]; } private: template int max_right(int st, F& check, T& acc, int k, int l, int r) { eval(k); if (l + 1 == r) { acc = nodef(acc, node[k]); return check(acc) ? -1 : k - n; } int m = (l + r) >> 1; if (m <= st) return max_right(st, check, acc, (k << 1) | 1, m, r); if (st <= l && check(nodef(acc, node[k]))) { acc = nodef(acc, node[k]); return -1; } int vl = max_right(st, check, acc, k << 1, l, m); if (vl != -1) return vl; return max_right(st, check, acc, (k << 1) | 1, m, r); } template int min_left(int st, F& check, T& acc, int k, int l, int r) { eval(k); if (l + 1 == r) { acc = nodef(node[k], acc); return check(acc) ? -1 : k - n + 1; } int m = (l + r) >> 1; if (st <= m) return min_left(st, check, acc, k << 1, l, m); if (r <= st && check(nodef(node[k], acc))) { acc = nodef(node[k], acc); return -1; } int vr = min_left(st, check, acc, (k << 1) | 1, m, r); if (vr != -1) return vr; return min_left(st, check, acc, k << 1, l, m); } public: template int max_right(int st, F check) { T acc = ident; return max_right(st, check, acc, 1, 0, n); } template int max_right(int st) { T acc = ident; return max_right(st, check, acc, 1, 0, n); } template int min_left(int st, F check) { T acc = ident; return min_left(st, check, acc, 1, 0, n); } template int min_left(int st) { T acc = ident; return min_left(st, check, acc, 1, 0, n); } }; namespace { lint RAQRSQ_nodef(const lint& a, const lint& b) { return a + b; } void RAQRSQ_lazyf(lint& a, const lint& b) { a += b; } void RAQRSQ_updf(lint& a, const lint& b, const unsigned int& width) { a += width * b; } lint RAQRMiQ_nodef(const lint& a, const lint& b) { return std::min(a, b); } void RAQRMiQ_lazyf(lint& a, const lint& b) { a += b; } void RAQRMiQ_updf(lint& a, const lint& b, const unsigned int& width) { a += b; } lint RAQRMaQ_nodef(const lint& a, const lint& b) { return std::max(a, b); } void RAQRMaQ_lazyf(lint& a, const lint& b) { a += b; } void RAQRMaQ_updf(lint& a, const lint& b, const unsigned int& width) { a += b; } lint RUQRSQ_nodef(const lint& a, const lint& b) { return a + b; } void RUQRSQ_lazyf(lint& a, const lint& b) { a = b; } void RUQRSQ_updf(lint& a, const lint& b, const unsigned int& width) { a = width * b; } lint RUQRMiQ_nodef(const lint& a, const lint& b) { return std::min(a, b); } void RUQRMiQ_lazyf(lint& a, const lint& b) { a = b; } void RUQRMiQ_updf(lint& a, const lint& b, const unsigned int& width) { a = b; } lint RUQRMaQ_nodef(const lint& a, const lint& b) { return std::max(a, b); } void RUQRMaQ_lazyf(lint& a, const lint& b) { a = b; } void RUQRMaQ_updf(lint& a, const lint& b, const unsigned int& width) { a = b; } } // namespace class RAQRSQ : public IntervalSegTree { using Base = IntervalSegTree; public: template RAQRSQ(Args&&... args) : Base(std::forward(args)..., 0) {} }; class RAQRMiQ : public IntervalSegTree { using Base = IntervalSegTree; public: template RAQRMiQ(Args&&... args) : Base(std::forward(args)..., LINF) {} }; class RAQRMaQ : public IntervalSegTree { using Base = IntervalSegTree; public: template RAQRMaQ(Args&&... args) : Base(std::forward(args)..., -LINF) {} }; class RUQRSQ : public IntervalSegTree { using Base = IntervalSegTree; public: template RUQRSQ(Args&&... args) : Base(std::forward(args)..., 0) {} }; class RUQRMiQ : public IntervalSegTree { using Base = IntervalSegTree; public: template RUQRMiQ(Args&&... args) : Base(std::forward(args)..., LINF) {} }; class RUQRMaQ : public IntervalSegTree { using Base = IntervalSegTree; public: template RUQRMaQ(Args&&... args) : Base(std::forward(args)..., -LINF) {} }; /** * @title Interval Segment Tree */ #line 3 "library/graph/UnionFind.hpp" class UnionFind { protected: std::vector par, size; public: UnionFind() {} UnionFind(int size) { init(size); } void init(int size) { par.resize(size); this->size.resize(size, 1); std::iota(all(par), 0); } int find(int n) { if (par[n] == n) return n; return par[n] = find(par[n]); } void unite(int n, int m) { n = find(n); m = find(m); if (n == m) return; int a = n, b = m; if (size[a] > size[b]) std::swap(a, b); par[a] = b; size[b] += size[a]; } bool same(int n, int m) { return find(n) == find(m); } int getsize(int n) { return size[find(n)]; } bool is_root(int n) { return find(n) == n; } }; /** * @title Disjoint set */ #line 4 "main.cpp" int N, rev[200010]; std::vector P, A, ans; int main() { std::cin >> N; P.resize(N + 2), A.resize(N + 2), ans.resize(N + 2); REP(i, N) { std::cin >> P[i]; rev[P[i]] = i; } REP(i, N) std::cin >> A[i]; RAQRSQ st1(P), st2(P); // right, left UnionFind uf(N + 2); for (int i = N; i > 0; i--) { int l = 0, r = 0; if (rev[i] != 1 && P[rev[i] - 1] > i) l = uf.getsize(rev[i] - 1); if (rev[i] != N && i < P[rev[i] + 1]) r = uf.getsize(rev[i] + 1); if (i <= A[i]) { if (l < r) { lint cur = i; int x = st1.max_right(rev[i] + 1, [&](lint v) { return v <= A[i] - cur; }); assert(x == -1 || rev[i] <= x); if (x == -1) ans[i] += r + 1; else ans[i] += std::min(r + 1, x - rev[i]); for (int j = rev[i] - 1; j >= rev[i] - l; j--) { cur += P[j]; if (cur > A[i]) break; int x = st1.max_right( rev[i] + 1, [&](lint v) { return v <= A[i] - cur; }); assert(x == -1 || rev[i] <= x); if (x == -1) ans[i] += r + 1; else ans[i] += std::min(r + 1, x - rev[i]); } } else { lint cur = i; int x = st2.min_left(rev[i], [&](lint v) { return v <= A[i] - cur; }); assert(x <= rev[i]); if (x == -1) ans[i] += l + 1; else ans[i] += std::min(l + 1, rev[i] - x + 1); for (int j = rev[i] + 1; j <= rev[i] + r; j++) { cur += P[j]; if (cur > A[i]) break; int x = st2.min_left( rev[i], [&](lint v) { return v <= A[i] - cur; }); assert(x <= rev[i]); if (x == -1) ans[i] += l + 1; else ans[i] += std::min(l + 1, rev[i] - x + 1); } } } if (rev[i] != 1 && P[rev[i] - 1] > i) uf.unite(rev[i] - 1, rev[i]); if (rev[i] != N && i < P[rev[i] + 1]) uf.unite(rev[i], rev[i] + 1); } printArray(ans.begin() + 1, ans.begin() + N + 1, '\n'); return 0; }