結果
問題 | No.1496 不思議な数え上げ |
ユーザー | kaage |
提出日時 | 2021-04-11 02:27:22 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 18,554 bytes |
コンパイル時間 | 3,760 ms |
コンパイル使用メモリ | 187,240 KB |
実行使用メモリ | 8,960 KB |
最終ジャッジ日時 | 2024-07-16 03:40:27 |
合計ジャッジ時間 | 18,360 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | RE | - |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | RE | - |
testcase_32 | RE | - |
testcase_33 | RE | - |
testcase_34 | RE | - |
testcase_35 | RE | - |
testcase_36 | RE | - |
testcase_37 | RE | - |
testcase_38 | RE | - |
testcase_39 | RE | - |
testcase_40 | RE | - |
testcase_41 | RE | - |
コンパイルメッセージ
library/data-structure/SegTree.hpp:107:7: warning: 'RSQ' has a base 'SegTree<long long int, {anonymous}::RSQ_nodef>' whose type uses the anonymous namespace [-Wsubobject-linkage] library/data-structure/SegTree.hpp:114:7: warning: 'RMiQ' has a base 'SegTree<long long int, {anonymous}::RMiQ_nodef>' whose type uses the anonymous namespace [-Wsubobject-linkage] library/data-structure/SegTree.hpp:121:7: warning: 'RMaQ' has a base 'SegTree<long long int, {anonymous}::RMaQ_nodef>' whose type uses the anonymous namespace [-Wsubobject-linkage] library/data-structure/IntervalSegTree.hpp:218:7: warning: 'RAQRSQ' has a base 'IntervalSegTree<long long int, long long int, {anonymous}::RAQRSQ_nodef, {anonymous}::RAQRSQ_lazyf, {anonymous}::RAQRSQ_updf>' whose type uses the anonymous namespace [-Wsubobject-linkage] library/data-structure/IntervalSegTree.hpp:227:7: warning: 'RAQRMiQ' has a base 'IntervalSegTree<long long int, long long int, {anonymous}::RAQRMiQ_nodef, {anonymous}::RAQRMiQ_lazyf, {anonymous}::RAQRMiQ_updf>' whose type uses the anonymous namespace [-Wsubobject-linkage] library/data-structure/IntervalSegTree.hpp:236:7: warning: 'RAQRMaQ' has a base 'IntervalSegTree<long long int, long long int, {anonymous}::RAQRMaQ_nodef, {anonymous}::RAQRMaQ_lazyf, {anonymous}::RAQRMaQ_updf>' whose type uses the anonymous namespace [-Wsubobject-linkage] library/data-structure/IntervalSegTree.hpp:245:7: warning: 'RUQRSQ' has a base 'IntervalSegTree<long long int, long long int, {anonymous}::RUQRSQ_nodef, {anonymous}::RUQRSQ_lazyf, {anonymous}::RUQRSQ_updf>' whose type uses the anonymous namespace [-Wsubobject-linkage] library/data-structure/IntervalSegTree.hpp:254:7: warning: 'RUQRMiQ' has a base 'IntervalSegTree<long long int, long long int, {anonymous}::RUQRMiQ_nodef, {anonymous}::RUQRMiQ_lazyf, {anonymous}::RUQRMiQ_updf>' whose type uses the anonymous namespace [-Wsubobject-linkage] library/data-structure/IntervalSegTree.hpp:263:7: warning: 'RUQRMaQ' has a base 'IntervalSegTree<long
ソースコード
#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 <string.h> #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #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<int, int>; using LP = std::pair<lint, lint>; constexpr int INF = INT_MAX / 2; constexpr lint LINF = LLONG_MAX / 2; constexpr double eps = DBL_EPSILON; constexpr double PI = 3.141592653589793238462643383279; template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; 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 <class F> inline constexpr decltype(auto) lambda_fix(F&& f) { return [f = std::forward<F>(f)](auto&&... args) { return f(f, std::forward<decltype(args)>(args)...); }; } template <class T> constexpr std::vector<T> make_vec(size_t n) { return std::vector<T>(n); } template <class T, class... Args> constexpr auto make_vec(size_t n, Args&&... args) { return std::vector<decltype(make_vec<T>(args...))>( n, make_vec<T>(std::forward<Args>(args)...)); } template <class T, class U> constexpr inline bool chmax(T& lhs, const U& rhs) noexcept { if (lhs < rhs) { lhs = rhs; return true; } return false; } template <class T, class U> 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 <class T> 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 <class T> constexpr void printArray(const std::vector<T>& vec, char split = ' ') { rep(i, vec.size()) { std::cout << vec[i]; std::cout << (i == (int)vec.size() - 1 ? '\n' : split); } } template <class InputIter> 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<int>(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 <class T, std::enable_if_t<std::is_convertible<int, T>::value, std::nullptr_t> = nullptr> void compress(std::vector<T>& 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 <class T> void compress(T* l, T* r) { std::vector<T> 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 <class InputIter> void compress(InputIter l, InputIter r) { std::vector<typename InputIter::value_type> 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 T, T (*nodef)(const T&, const T&)> class SegTree { protected: unsigned int n = 1, rank = 0; std::vector<T> 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 <class U> SegTree(const std::vector<U>& 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 <class F> 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 <class F> int max_right(int st, F check) const { T acc = ident; return max_right(st, check, acc, 1, 0, n); } template <bool (*check)(const T&)> 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<lint, RSQ_nodef> { using Base = SegTree<lint, RSQ_nodef>; public: template <class... Args> RSQ(Args&&... args) : Base(std::forward<Args>(args)..., 0) {} }; class RMiQ : public SegTree<lint, RMiQ_nodef> { using Base = SegTree<lint, RMiQ_nodef>; public: template <class... Args> RMiQ(Args&&... args) : Base(std::forward<Args>(args)..., LINF) {} }; class RMaQ : public SegTree<lint, RMaQ_nodef> { using Base = SegTree<lint, RMaQ_nodef>; public: template <class... Args> RMaQ(Args&&... args) : Base(std::forward<Args>(args)..., -LINF) {} }; /** * @title Segment Tree */ #line 4 "library/data-structure/IntervalSegTree.hpp" template <class T, class U, T (*nodef)(const T&, const T&), void (*lazyf)(U&, const U&), void (*updf)(T&, const U&, const unsigned int&)> class IntervalSegTree : public SegTree<T, nodef> { using Base = SegTree<T, nodef>; using Base::ident; using Base::n; using Base::node; using Base::rank; std::vector<U> lazy; std::vector<bool> lazyflag; std::vector<int> 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<T>& 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 <class F> 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 <class F> 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 <class F> int max_right(int st, F check) { T acc = ident; return max_right(st, check, acc, 1, 0, n); } template <bool (*check)(const T&)> int max_right(int st) { T acc = ident; return max_right(st, check, acc, 1, 0, n); } template <class F> int min_left(int st, F check) { T acc = ident; return min_left(st, check, acc, 1, 0, n); } template <bool (*check)(const T&)> 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<lint, lint, RAQRSQ_nodef, RAQRSQ_lazyf, RAQRSQ_updf> { using Base = IntervalSegTree<lint, lint, RAQRSQ_nodef, RAQRSQ_lazyf, RAQRSQ_updf>; public: template <class... Args> RAQRSQ(Args&&... args) : Base(std::forward<Args>(args)..., 0) {} }; class RAQRMiQ : public IntervalSegTree<lint, lint, RAQRMiQ_nodef, RAQRMiQ_lazyf, RAQRMiQ_updf> { using Base = IntervalSegTree<lint, lint, RAQRMiQ_nodef, RAQRMiQ_lazyf, RAQRMiQ_updf>; public: template <class... Args> RAQRMiQ(Args&&... args) : Base(std::forward<Args>(args)..., LINF) {} }; class RAQRMaQ : public IntervalSegTree<lint, lint, RAQRMaQ_nodef, RAQRMaQ_lazyf, RAQRMaQ_updf> { using Base = IntervalSegTree<lint, lint, RAQRMaQ_nodef, RAQRMaQ_lazyf, RAQRMaQ_updf>; public: template <class... Args> RAQRMaQ(Args&&... args) : Base(std::forward<Args>(args)..., -LINF) {} }; class RUQRSQ : public IntervalSegTree<lint, lint, RUQRSQ_nodef, RUQRSQ_lazyf, RUQRSQ_updf> { using Base = IntervalSegTree<lint, lint, RUQRSQ_nodef, RUQRSQ_lazyf, RUQRSQ_updf>; public: template <class... Args> RUQRSQ(Args&&... args) : Base(std::forward<Args>(args)..., 0) {} }; class RUQRMiQ : public IntervalSegTree<lint, lint, RUQRMiQ_nodef, RUQRMiQ_lazyf, RUQRMiQ_updf> { using Base = IntervalSegTree<lint, lint, RUQRMiQ_nodef, RUQRMiQ_lazyf, RUQRMiQ_updf>; public: template <class... Args> RUQRMiQ(Args&&... args) : Base(std::forward<Args>(args)..., LINF) {} }; class RUQRMaQ : public IntervalSegTree<lint, lint, RUQRMaQ_nodef, RUQRMaQ_lazyf, RUQRMaQ_updf> { using Base = IntervalSegTree<lint, lint, RUQRMaQ_nodef, RUQRMaQ_lazyf, RUQRMaQ_updf>; public: template <class... Args> RUQRMaQ(Args&&... args) : Base(std::forward<Args>(args)..., -LINF) {} }; /** * @title Interval Segment Tree */ #line 3 "library/graph/UnionFind.hpp" class UnionFind { protected: std::vector<int> 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<lint> 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) { int cur = i; int x = st1.max_right(rev[i] + 1, [&](int v) { return v <= A[i] - cur; }); 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, [&](int v) { return v <= A[i] - cur; }); if (x == -1) ans[i] += r + 1; else ans[i] += std::min(r + 1, x - rev[i]); } } else { int cur = i; int x = st2.min_left(rev[i], [&](int v) { return v <= A[i] - cur; }); 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], [&](int v) { return v <= A[i] - cur; }); 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; }