// made by https://github.com/094-gengar/add_lib_to_templ #if !__INCLUDE_LEVEL__ #include __FILE__ int main() { using namespace std; using namespace m9; LL(n); // VPLL(v, n); vpll v(n); vvll g(n); rep(i, n - 1) { LL(l, a); a--; v[i + 1] = {l, a}; g[a].eb(i + 1); } vll max_level(n, inf); vb vis(n, false); max_level[0] = 0; priority_queue> pq{}; pq.emplace(0, 0); while(pq.size()) { const auto [curCst, curE] = pq.top(); pq.pop(); if(curCst > max_level[curE])continue; for(const auto& e : g[curE]) { auto& lv = v[e].first; // dbg(e, v[e].first); if(chmin(max_level[e], max(max_level[curE], lv)))pq.emplace(max_level[e], e); } } //dbg(max_level); auto mx_lv_sor = max_level >> Sor; LL(q); // dbg(q); rep(_, q) { LL(t); // dbg(t); if(t == 1) { LL(x); print(u_b(mx_lv_sor, x)); } else { LL(y); print(max_level[y - 1] == inf ? -1 : max_level[y - 1]); } } } #else // #line 2 "graph/Graph.hpp" #include #include #include #include #include #include namespace m9 { template struct Graph { template inline bool chmin(_T& a, const _T& b) { if(b < a) { a = b; return true; } else return false; } int SIZ; bool isOffset; bool isDirected; std::vector> G; Graph(int n, bool offset, bool directed) : SIZ(n), isOffset(offset), isDirected(directed), G(n) {} void init(int m) { T a, b; for(int i{}; i < m; i++) { #ifdef MY_FASTIO io.IN(a, b); #else #ifdef MY_FASTIO_VER2 IN(a, b); #else std::cin >> a >> b; #endif #endif a -= isOffset; b -= isOffset; assert(0 <= a and a < SIZ); assert(0 <= b and b < SIZ); G[a].emplace_back(b); if(not isDirected)G[b].emplace_back(a); } } void init(std::vector> g) { G = g; } std::vector bfs(T s, T t = -1) { assert(0 <= s and s < SIZ); assert((t == -1) or (0 <= s or s < SIZ)); std::vector dist(SIZ, std::numeric_limits::max() / 2); std::vector vis(SIZ, false); dist[s] = 0; std::queue q{}; q.emplace(s); while(!q.empty()) { T cur = q.front(); q.pop(); for(const auto& e : G[cur])if(chmin(dist[e], dist[cur] + 1))q.emplace(e); } return (t == -1 ? dist : std::vector{dist[t]}); } }; // #include template struct weightedGraph { using PTT = std::pair; template inline bool chmin(_T& a, const _T& b) { if(b < a) { a = b; return true; } else return false; } int SIZ; bool isOffset; bool isDirected; std::vector> G; weightedGraph(int n, bool os, bool drc) : SIZ(n), isOffset(os), isDirected(drc), G(n) {} void init(int m) { T a, b, cst; for(int i{}; i < m; i++) { #ifdef MY_FASTIO io.IN(a, b, cst); #else #ifdef MY_FASTIO_VER2 IN(a, b, cst); #else std::cin >> a >> b >> cst; #endif #endif a -= isOffset; b -= isOffset; assert(0 <= a and a < SIZ); assert(0 <= b and b < SIZ); G[a].emplace_back(cst, b); if(not isDirected)G[b].emplace_back(cst, a); } } void init(std::vector> g) { G = g; } std::vector bfs(T s, T t = -1) { assert(0 <= s and s < SIZ); assert((t == -1) or (0 <= s or s < SIZ)); std::vector dist(SIZ, std::numeric_limits::max() / 2); std::vector vis(SIZ, false); dist[s] = 0; std::queue q{}; q.emplace(0, s); while(!q.empty()) { const auto [curCst, curE]= q.front(); q.pop(); if(curCst > dist[curE])continue; for(const auto&[cst, e] : G[curE])if(chmin(dist[e], dist[curE] + cst))q.emplace(dist[e], e); } return (t == -1 ? dist : std::vector{dist[t]}); } std::vector dijk(T s, T t = -1) { assert(0 <= s and s < SIZ); assert((t == -1) or (0 <= s or s < SIZ)); std::vector dist(SIZ, std::numeric_limits::max() / 2); std::vector vis(SIZ, false); dist[s] = 0; std::priority_queue, std::greater<>> pq{}; pq.emplace(0, s); while(!pq.empty()) { const auto [curCst, curE]= pq.top(); pq.pop(); if(curCst > dist[curE])continue; for(const auto&[cst, e] : G[curE])if(chmin(dist[e], dist[curE] + cst))pq.emplace(dist[e], e); } return (t == -1 ? dist : std::vector{dist[t]}); } }; } // namespace m9 // #line 2 "graph/SCC.hpp" #include #include #include #include namespace m9 { class SCC { int SIZ; std::vector> g, rg; std::vector ord, comp; std::vector used; public: void dfs(int cur) { used[cur] = true; for(const auto& e : g[cur])if(not used[e])dfs(e); ord.emplace_back(cur); } void rdfs(int cur, int k) { comp[cur] = k; for(const auto& e : rg[cur])if(comp[e] == -1)rdfs(e, k); } SCC(std::vector>& tmp) : g(tmp) { SIZ = static_cast(g.size()); rg.resize(SIZ); comp.assign(SIZ, -1); used.assign(SIZ, false); for(int v{}; v < SIZ; v++) for(const auto& e : g[v]) rg[e].emplace_back(v); for(int v{}; v < SIZ; v++)if(not used[v])dfs(v); int k{}; std::reverse(std::begin(ord), std::end(ord)); for(const auto& v: ord)if(comp[v] == -1)rdfs(v, k++); } bool same(int u, int v) { assert(0 <= u and u < SIZ); assert(0 <= v and v < SIZ); return comp[u] == comp[v]; } std::vector> rebuild() { int MX = *std::max_element(std::begin(comp), std::end(comp)) + 1; std::vector> rebuildedGraph(SIZ); std::set> conn{}; for(int v{}; v < MX; v++) for(const auto& e : g[v]) if(comp[v] != comp[e] and not conn.count(std::make_pair(v, e))) conn.emplace(v, e), rebuildedGraph[comp[v]].emplace_back(comp[e]); return rebuildedGraph; } }; } // namespace m9 // #line 2 "heuristic/RandInt.hpp" #include #include namespace m9 { struct RandInt { private: std::mt19937 mt; public: RandInt() { mt.seed((unsigned int)time(0)); } auto seed() -> std::mt19937 { return mt; } unsigned int next() { return mt(); } unsigned int roll(int high) { std::uniform_int_distribution die(0, high - 1); return die(mt); } } ri; } // namespace m9 using m9::ri; // #line 2 "heuristic/Timer.hpp" #include namespace m9 { struct Timer { private: std::chrono::system_clock::time_point m_start; public: Timer() : m_start(std::chrono::system_clock::time_point::min()) { (*this).start(); } void clear() { m_start = std::chrono::system_clock::time_point::min(); } bool is_started() const { return (m_start.time_since_epoch() != std::chrono::system_clock::duration(0)); } void start() { m_start = std::chrono::system_clock::now(); } unsigned long long get_time() { if(is_started()) { std::chrono::system_clock::duration diff; diff = std::chrono::system_clock::now() - m_start; return (unsigned long long)std::chrono::duration_cast(diff).count() + 50; } return 0; } } timer; } // namespace m9 using m9::timer; // #line 2 "io/FastIO.hpp" #include #include #include #include #include #include #include #define MY_FASTIO_VER2 //#define IS_OUTPUT_ONLY namespace m9 { struct fastin { std::array _buf; ssize_t n_w, n_r; #ifdef IS_OUTPUT_ONLY fastin() {} #else fastin() { _do_read(); } #endif long long rd_ll() noexcept { long long ret = 0, sgn = 1; signed char ch = _current_char(); while(ch == ' ' || ch == '\n')ch = _next_char(); if(ch == '-') sgn *= -1, ch = _next_char(); for(; '0' <= ch && ch <= '9'; ch = _next_char()) { ret = (ret * 10) + ch - '0'; } return sgn * ret; } double rd_dbl() noexcept { double ret{}, sgn = 1; signed char ch = _current_char(); while(ch == ' ' or ch == '\n')ch = _next_char(); if(ch == '-')sgn *= -1, ch = _next_char(); bool foundDot = false; double mul = 1; for(; ('0' <= ch && ch <= '9') or ch == '.'; ch = _next_char()) { if(ch == '.') { foundDot = true; continue; } if(foundDot) { ret = ret + (ch - '0') / (mul *= 10); } else { ret = (ret * 10) + ch - '0'; } } return sgn * ret; } int rd_int() noexcept { long long _result = rd_ll(); assert(-2147483648ll <= _result && _result <= 2147483647ll); return static_cast(_result); } std::string rd_str() noexcept { std::string _res{}; signed char ch = _current_char(); while(ch == ' ' || ch == '\n')ch = _next_char(); for(; ch != -1 && ch != '\n' && ch != ' '; ch = _next_char()) { _res += std::string(1, ch); } return _res; } char rd_chr() noexcept { signed char ch = _current_char(); while(ch == ' ' || ch == '\n')ch = _next_char(); signed char discard = _next_char(); return ch; } private: void _do_read() noexcept { ssize_t r = read(0, &_buf[0], _buf.size()); assert(r >= 0); n_w = r, n_r = 0; } inline signed char _next_char() noexcept { if(++n_r == n_w)_do_read(); return _current_char(); } inline signed char _current_char() noexcept { return (n_r == n_w ? -1 : _buf[n_r]); } } fi; struct fastout { unsigned _wt_double_digit = 15; inline void wt_bool(bool x) noexcept { putchar_unlocked(x ? '1' : '0'); } inline void wt_ll(long long x) noexcept { std::array _buf; ssize_t _siz = 0; if(x < 0) { x *= -1; putchar_unlocked('-'); } if(x == 0)putchar_unlocked('0'); while(x > 0) { _buf[_siz++] = x % 10 + '0'; x /= 10; } while(_siz--)putchar_unlocked(_buf[_siz]); } inline void wt_int(int x) noexcept { wt_ll(static_cast(x)); } inline void wt_ull(unsigned long long x) noexcept { std::array _buf; ssize_t _siz = 0; if(x < 0) { x *= -1; putchar_unlocked('-'); } if(x == 0)putchar_unlocked('0'); while(x > 0) { _buf[_siz++] = x % 10 + '0'; x /= 10; } while(_siz--)putchar_unlocked(_buf[_siz]); } inline void wt_chr(char x) noexcept { putchar_unlocked(x); } inline void wt_str(std::string x) noexcept { ssize_t _siz = static_cast(x.length()); for(ssize_t i = 0; i < _siz; i++)putchar_unlocked(x[i]); } inline void wt_dbl(double x) noexcept { int k, r = 0; double v = 1; if(x < 0) { x *= -1; putchar_unlocked('-'); } x += 0.5 * pow(0.1, _wt_double_digit); while(x >= 10 * v)v *= 10, r++; while(r-- >= 0) { k = floor(x / v); if(k >= 10)k = 9; if(k <= -1)k = 0; x -= k * v; v *= 0.1; putchar_unlocked(k + '0'); } if(_wt_double_digit > 0) { putchar_unlocked('.'); v = 1; for(ssize_t _ = 0; _ < _wt_double_digit; _++) { v *= 0.1; k = floor(x / v); if(k >= 10)k = 9; if(k <= -1)k = 0; x -= k * v; putchar_unlocked(k + '0'); } } } // const char* const END = "\n"; // const char* const SPLIT = " "; } fo; inline void set_digit(unsigned d) { fo._wt_double_digit = d; } inline void scan(int& x) noexcept { x = fi.rd_int(); } inline void scan(long& x) noexcept { x = (sizeof(long) == 32 ? fi.rd_int() : fi.rd_ll()); } inline void scan(long long& x) noexcept { x = fi.rd_ll(); } inline void scan(unsigned& x) noexcept { int a = fi.rd_int(); assert(a >= 0); x = a; } inline void scan(unsigned long& x) noexcept { long a; scan(a); assert(a >= 0l); x = a; } inline void scan(unsigned long long& x) noexcept { long long a = fi.rd_ll(); assert(a >= 0ll); x = a; } // inline void scan(double& x) noexcept { x = static_cast(fi.rd_ll()); } inline void scan(double& x) noexcept { x = fi.rd_dbl(); } inline void scan(char& x) noexcept { x = fi.rd_chr(); } inline void scan(std::string& x) noexcept { x = fi.rd_str(); } template inline void scan(std::pair& x) { scan(x.first); scan(x.second); } template inline void scan(std::vector& x) { for(auto& e : x)scan(e); } void IN() {} template void IN(Car&& car, Cdr &&...cdr) { scan(car); IN(std::forward(cdr)...); } inline void wt_any(const bool& x) noexcept { fo.wt_bool(x); } inline void wt_any(const int& x) noexcept { fo.wt_int(x); } inline void wt_any(const long& x) noexcept { fo.wt_ll(static_cast(x)); } inline void wt_any(const long long& x) noexcept { fo.wt_ll(x); } inline void wt_any(const unsigned& x) noexcept { fo.wt_ull(static_cast(x)); } inline void wt_any(const unsigned long& x) noexcept { fo.wt_ull(static_cast(x)); } inline void wt_any(const unsigned long long& x) noexcept { fo.wt_ull(x); } inline void wt_any(const double& x) noexcept { fo.wt_dbl(x); } inline void wt_any(const char& x) noexcept { fo.wt_chr(x); } inline void wt_any(const char x[]) noexcept { size_t _siz = 0; while(x[_siz] != '\0')fo.wt_chr(x[_siz++]); } inline void wt_any(const std::string& x) noexcept { fo.wt_str(x); } template inline void wt_any(const std::pair& x) { wt_any(x.first); wt_any(" "); wt_any(x.second); } template inline void wt_any(const std::vector& x) { size_t _siz = x.size(); for(size_t i = 0; i < _siz - 1; i++)wt_any(x[i]), wt_any(" "); wt_any(x.back()); } int print() { wt_any("\n"); return 0; } template int print(const T& t) { wt_any(t); wt_any("\n"); return 0; } template int print(const Car& car, const Cdr &...cdr) { wt_any(car); wt_any(" "); print(cdr...); return 0; } template int wt(const T& t) { wt_any(t); return 0; } template int wt(const Car& car, const Cdr &...cdr) { wt_any(car); wt(cdr...); return 0; } void yn(bool fl = true) { print(fl ? "Yes" : "No"); } template void drop(const T&... x) { print(x...); exit(0); } void dyn(bool fl = true) { print(fl ? "Yes" : "No"); exit(0); } // void setEND(const char* c) { fo.END = c; } // void setSPLIT(const char* c) { fo.SPLIT = c; } #define INT(...) int __VA_ARGS__; IN(__VA_ARGS__) #define LL(...) long long __VA_ARGS__; IN(__VA_ARGS__) #define ULL(...) unsigned long long __VA_ARGS__; IN(__VA_ARGS__) #define STR(...) std::string __VA_ARGS__; IN(__VA_ARGS__) #define CHR(...) char __VA_ARGS__; IN(__VA_ARGS__) #define DBL(...) double __VA_ARGS__; IN(__VA_ARGS__) using ll = long long; using ull = unsigned long long; using pii = std::pair; using pll = std::pair; #define VEC(a, type, n) std::vector (a)(n); IN(a) #define VVEC(a, type, h, w) std::vector> (a)(h, std::vector(w)); IN(a) #define VI(a, n) VEC(a, int, n) #define VVI(a, h, w) VVEC(a, int, h, w) #define VPII(a, n) VEC(a, pii, n) #define VVPII(a, h, w) VVEC(a, pii, h, w) #define VLL(a, n) VEC(a, ll, n) #define VVLL(a, h, w) VVEC(a, ll, h, w) #define VPLL(a, n) VEC(a, pll, n) #define VVPLL(a, h, w) VVEC(a, pll, h, w) #define VULL(a, n) VEC(a, ull, n) #define VVULL(a, h, w) VVEC(a, ull, h, w) #define VC(a, n) VEC(a, char, n) #define VVC(a, h, w) VVEC(a, char, h, w) #define VD(a, n) VEC(a, double, n) #define VVD(a, h, w) VVEC(a, double, h, w) #define VS(a, n) VEC(a, std::string, n) } // namespace m9 // #line 2 "math/Argsort.hpp" #include namespace m9 { template bool arg_cmp(const std::pair& p, const std::pair& q) { auto area = [](const std::pair& a) -> int { const auto&[x, y] = a; if(y < 0)return -1; else if(y == 0 and 0 <= x)return 0; else return 1; }; const int ap = area(p); const int aq = area(q); if(ap != aq)return ap < aq; else { const auto& [px, py] = p; const auto& [qx, qy] = q; return (0 < (px * qy - py * qx)); } } } // namespace m9 // #line 2 "math/Combination.hpp" #include #include namespace m9 { template struct combination { using ll = long long; ll N; std::vector fct; combination(ll n) : N(n) { fct.resize(N + 1); fct[0] = 1; for(ll i{1}; i <= N; i++)fct[i] = fct[i - 1] * i; } T nPr(ll n, ll r) { return n < 0 || r < 0 || n < r ? (T)(0) : fct[n] / fct[n - r]; } T nCr(ll n, ll r) { return n < 0 || r < 0 || n < r ? (T)(0) : nPr(n, r) / fct[r]; } T nHr(ll n, ll r) {return n < 0 || r + n - 1 < 0 || n < r + n - 1 ? (T)(0) : nCr(r + n - 1, r); } }; } // namespace m9 // #line 2 "math/DivisorList.hpp" #include #include namespace m9 { template std::vector divisorList(const T& N) { std::vector result{}; for(T i{1}; i * i <= N; i++) { if(N % i == 0) { result.emplace_back(i); if(i * i != N)result.emplace_back(N / i); } } std::sort(std::begin(result), std::end(result)); return result; } } // namespace m9 // #line 2 "math/ModInt.hpp" #include namespace m9 { #define MY_MODINT template struct modInt { long long x; constexpr modInt() noexcept : x() {} template constexpr modInt(T v = 0) noexcept : x(v% Mod) { if(x < 0)x += Mod; } constexpr long long val() const noexcept { return x; } constexpr modInt operator-() const noexcept { return x ? Mod - x : 0; } constexpr modInt operator+(const modInt& r) const noexcept { return modInt(*this) += r; } constexpr modInt operator-(const modInt& r) const noexcept { return modInt(*this) -= r; } constexpr modInt operator*(const modInt& r) const noexcept { return modInt(*this) *= r; } constexpr modInt operator/(const modInt& r) const noexcept { return modInt(*this) /= r; } constexpr modInt& operator+=(const modInt& r) noexcept { x += r.x; if(x >= Mod)x -= Mod; return *this; } constexpr modInt& operator-=(const modInt& r) noexcept { x -= r.x; if(x < 0)x += Mod; return *this; } constexpr modInt& operator*=(const modInt& r) noexcept { x = x * r.x % Mod; return *this; } constexpr modInt& operator/=(const modInt& r) noexcept { x = x * r.inv().val() % Mod; return *this; } constexpr modInt powm(long long n) noexcept { if(n < 0)return powm(-n).inv(); modInt x = *this, r = 1; for(; n; x *= x, n >>= 1)if(n & 1)r *= x; return r; } constexpr modInt inv() const noexcept { long long a = x, b = Mod, u = 1, v = 0; while(b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return modInt(u); } constexpr modInt comb(long long a) noexcept { modInt n = *this, s = 1; for(int i = 0; i < a; i++)s *= (n - modInt(i)); modInt m = 1; for(int i = 1; i <= a; i++)m *= modInt(i); return s * m.powm(Mod - 2); } constexpr bool operator==(const modInt& r) { return this->x == r.x; } constexpr bool operator!=(const modInt& r) { return this->x != r.x; } friend std::ostream& operator<<(std::ostream& os, const modInt& a) { return os << a.x; } friend std::istream& operator>>(std::istream& is, modInt& a) { long long v; is >> v; a = modInt(v); return is; } }; using mint = modInt<1000000007>; using mint2 = modInt<998244353>; } // namespace m9 // #line 2 "math/PrimeFactor.hpp" #include namespace m9 { template std::vector> prime_factor(T n) { std::vector> ret; for(T i{2}; i * i <= n; i++) { if(n % i != 0)continue; T tmp = 0; while(n % i == 0) { tmp++; n /= i; } ret.push_back(std::make_pair(i, tmp)); } if(n != 1) ret.push_back(std::make_pair(n, 1)); return ret; } } // namespace m9 // #line 2 "other/Integers.hpp" #include namespace m9 { struct cent_t { private: __int128_t N; public: template constexpr cent_t(T n) : N(static_cast<__int128_t>(n)) {} friend std::ostream& operator<<(std::ostream& os, const cent_t& a) { if(a.N > INT64_MAX)return os << "too big integer"; else return os << static_cast(a.N); } friend std::istream& operator>>(std::istream& is, cent_t& a) { long long tmp{}; is >> tmp; a.N = static_cast<__int128_t>(tmp); return is; } constexpr __int128_t val() const noexcept { return N; } constexpr cent_t operator-() const noexcept { return -N; } template constexpr cent_t operator+(const INTEGER& x) const noexcept { return N + x.N; } template constexpr cent_t operator-(const INTEGER& x) const noexcept { return N - x.N; } template constexpr cent_t operator*(const INTEGER& x) const noexcept { return N * x.N; } template constexpr cent_t operator/(const INTEGER& x) const noexcept { return N / x.N; } template constexpr cent_t operator+=(const INTEGER& x) noexcept { N += x.N; return *this; } template constexpr cent_t operator-=(const INTEGER& x) noexcept { N -= x.N; return *this; } template constexpr cent_t operator*=(const INTEGER& x) noexcept { N *= x.N; return *this; } template constexpr cent_t operator/=(const INTEGER& x) noexcept { N /= x.N; return *this; } constexpr cent_t operator++() noexcept { N += 1; return *this; } constexpr cent_t operator--() noexcept { N -= 1; return *this; } template constexpr bool operator==(const INTEGER& x) { return this->N == x.N; } template constexpr bool operator!=(const INTEGER& x) { return this->N != x.N; } template constexpr bool operator<(const INTEGER& x) { return this->N < x.N; } template constexpr bool operator>(const INTEGER& x) { return this->N > x.N; } template constexpr bool operator<=(const INTEGER& x) { return this->N <= x.N; } template constexpr bool operator>=(const INTEGER& x) { return this->N >= x.N; } }; } // namespace m9 using i8 = signed char; using u8 = unsigned char; using i32 = signed int; using u32 = unsigned int; using i64 = signed long long; using u64 = unsigned long long; i8 operator"" _i8(unsigned long long x) { return static_cast(x); } u8 operator"" _u8(unsigned long long x) { return static_cast(x); } i32 operator"" _i32(unsigned long long x) { return static_cast(x); } u32 operator"" _u32(unsigned long long x) { return static_cast(x); } i64 operator"" _i64(unsigned long long x) { return static_cast(x); } u64 operator"" _u64(unsigned long long x) { return static_cast(x); } m9::cent_t operator"" _i128(unsigned long long x) { return m9::cent_t(x); } using f32 = float; using f64 = double; double operator"" _f32(unsigned long long x) { return static_cast(x); } double operator"" _f64(unsigned long long x) { return static_cast(x); } // #line 2 "other/others.hpp" #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 #include #include #include #include #include #include #include #include #include #include #include //#define INCLUDE_BOOST #ifdef INCLUDE_BOOST #if __has_include() #include #include #include #include #include #endif #endif //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define all(x) std::begin(x), std::end(x) #define Sort(x) sort(all(x)) #define rSort(x) sort(all(x)); reverse(all(x)) #define UNIQUE(v) v.erase(unique(all(v)), v.end()) #define uniq(v) sort(all(v)); UNIQUE(v) #define l_b(c, x) distance(begin(c), lower_bound(all(c), (x))) #define u_b(c, x) distance(begin(c), upper_bound(all(c), (x))) #define fi first #define se second #define m_p make_pair #define m_t make_tuple #define pb push_back #define eb emplace_back #define cauto const auto& #define _rep_overload(_1, _2, _3, _4, name, ...) name #define _rep1(i, n) _rep2(i, 0, n) #define _rep2(i, a, b) for(ll i = (a); i < (b); i++) #define _rep3(i, a, b, c) for(ll i = (a); i < (b); i += c) #define rep(...) _rep_overload(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__) #define myceil(a, b) ((a) + ((b) - 1)) / (b) #define continue_with(...) ({__VA_ARGS__; continue;}) #define break_with(...) ({__VA_ARGS__; break;}) #include //#define int long long using ll = long long; using ull = unsigned long long; using pii = std::pair; using pll = std::pair; #include #include template using Vec = std::vector; using vb = Vec; using vi = Vec; using vu = Vec; using vll = Vec; using vull = Vec; using vd = Vec; using vc = Vec; using vs = Vec; using vpii = Vec; using vpll = Vec; using vvb = Vec; using vvi = Vec; using vvu = Vec; using vvll = Vec; using vvull = Vec; using vvd = Vec; using vvc = Vec; using vvs = Vec; using vvpii = Vec; using vvpll = Vec; template inline bool chmin(T& a, const T& b) { if(b < a) { a = b; return true; } return false; } template inline bool chmax(T& a, const T& b) { if(a < b) { a = b; return true; } return false; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll fact(ll n, ll m) { ll f = n; for(ll i = n - 1; i >= 1; i--) { f *= i; if(m != -1)f %= m; } return f; } constexpr ll inf = 0x1fffffffffffffff; constexpr ll mod = 1000000007LL; constexpr ll mod2 = 998244353LL; constexpr double eps = 1e-8; constexpr double pi = 3.141592653589793238462643383279; #include #include #include struct sorted_operator { template friend std::vector operator>>(std::vectora, sorted_operator) { std::sort(std::begin(a), std::end(a)); return a; } friend std::string operator>>(std::string a, sorted_operator) { std::sort(std::begin(a), std::end(a)); return a; } } Sor; struct reversed_operator { template friend std::vector operator>>(std::vector a, reversed_operator) { std::reverse(std::begin(a), std::end(a)); return a; } friend std::string operator>>(std::string a, reversed_operator) { std::reverse(std::begin(a), std::end(a)); return a; } } Rev; struct unique_operator { template friend std::vector operator>>(std::vector a, unique_operator) { a.erase(unique(std::begin(a), std::end(a)), std::end(a)); return a; } friend std::string operator>>(std::string a, unique_operator) { a.erase(unique(std::begin(a), std::end(a)), std::end(a)); return a; } } Set; template void INCVEC(std::vector& a) { for(T& e : a)e++; } template void DECVEC(std::vector& a) { for(T& e : a)e--; } struct inc_operator { template friend std::vector operator>>(std::vector a, inc_operator) { INCVEC(a); return a; } } Inc; struct dec_operator { template friend std::vector operator>>(std::vector a, dec_operator) { DECVEC(a); return a; } } Dec; template auto operator>> (std::vector a, F f) -> std::vector { std::vector res{}; for(const T& e : a)res.emplace_back(f(e)); return res; } #include namespace m9 { #ifdef ONLINE_JUDGE #define dbg(...) void(0) #else #define dbg(...) _DEBUG(#__VA_ARGS__, __VA_ARGS__) template void _DEBUG(const char* s, Car&& car, Cdr&&... cdr) { constexpr const char* open_br = sizeof...(cdr) == 0 ? "" : "("; constexpr const char* close_br = sizeof...(cdr) == 0 ? "" : ")"; #ifdef MY_FASTIO io.wt_any(open_br); io.wt_any(s); io.wt_any(close_br); io.wt_any(" : "); io.wt_any(open_br); io.wt_any(std::forward(car)); ((io.wt_any(", "), io.wt_any(std::forward(cdr))), ...); io.wt_any(close_br); io.wt_any("\n"); #else #ifdef MY_FASTIO_VER2 wt_any(open_br); wt_any(s); wt_any(close_br); wt_any(" : "); wt_any(open_br); wt_any(std::forward(car)); ((wt_any(", "), wt_any(std::forward(cdr))), ...); wt_any(close_br); wt_any("\n"); #else std::cerr << open_br << s << close_br << " : " << open_br << std::forward(car); ((std::cerr << ", " << std::forward(cdr)), ...); std::cerr << close_br << "\n"; #endif #endif } #endif } // namespace m9 // #line 2 "structure/BinaryIndexedTree.hpp" #include #include namespace m9 { template class BIT { int SIZ; std::vector tree; public: BIT(int n = 0, T x = 0) : SIZ(n), tree(n, x) {} T sum(int i) { assert(0 <= i and i <= SIZ); T result{0}; for(; i > 0; i -= (i & -i))result += tree[i - 1]; return result; } T sum(int l, int r) { return sum(r) - sum(l); } void add(int i, T a) { assert(0 <= i and i < SIZ); for(i++; i <= SIZ; i += (i & -i))tree[i - 1] += a; } int lowerBound(T k) { if(k <= 0)return 0; int result{0}, i{1}; for(; (i << 1) <= SIZ; )i <<= 1; for(; i; i >>= 1)if(result + i <= SIZ and tree[result + i] < k)k -= tree[result += i]; return result; } }; } // namespace m9 // #line 2 "structure/CompressVec.hpp" #include namespace m9 { template struct compress { private: int SIZ; public: std::vector C; compress(std::vector a) { SIZ = a.size(); C = a; std::map mp{}; for(int i{}; i < SIZ; i++)mp[C[i]] = -1; int c{}; for(auto&[key, value] : mp)value = c++; for(int i{}; i < SIZ; i++)C[i] = mp[C[i]]; } void init(std::vector a) { SIZ = a.size(); C = a; std::map mp{}; for(int i{}; i < SIZ; i++)mp[C[i]] = -1; int c{}; for(auto&[key, value] : mp)value = c++; for(int i{}; i < SIZ; i++)C[i] = mp[C[i]]; } T operator[](int idx) { assert(0 <= idx and idx < SIZ); return C[idx]; } }; } // namespace m9 // #line 2 "structure/Cumsum.hpp" #include namespace m9 { template struct cumsum { private: int SIZ; public: std::vector S; cumsum(std::vector a) { SIZ = a.size(); S.assign(SIZ + 1, 0); for(int i{}; i < SIZ; i++)S[i + 1] = S[i] + a[i]; } void init(std::vector a) { SIZ = a.size(); S.assign(SIZ + 1, 0); for(int i{}; i < SIZ; i++)S[i + 1] = S[i] + a[i]; } T operator[](int idx) { assert(0 <= idx and idx <= SIZ); return S[idx]; } }; } // namespace m9 // #line 2 "structure/SegmentTree.hpp" #include #include namespace m9 { template class segmentTree { using T = typename M::valueType; std::vector seg; int SIZ; public: segmentTree(int n) { for(SIZ = 1; SIZ < n; )SIZ <<= 1; seg.assign(2 * SIZ, M::id);} void update(int k, const T& x) { assert(0 <= k and k < SIZ); for(seg[k += SIZ] = x; k>>= 1; )seg[k] = M::op(seg[2 * k], seg[2 * k + 1]); } void set(int k, const T& x) { assert(0 <= k and k < SIZ); seg[k + SIZ] = x; } T operator[](const int& k) const { assert(0 <= k and k < SIZ); return seg[k + SIZ]; } void build() { for(int k = SIZ - 1; k > 0; k--)seg[k] = M::op(seg[2 * k], seg[2 * k + 1]); } T query(int a, int b) { assert(0 <= a and a <= b and b <= SIZ); auto L = M::id; auto R = M::id; for(a += SIZ, b += SIZ; a < b; a >>= 1, b >>= 1) { if(a & 1)L = M::op(L, seg[a++]); if(b & 1)R = M::op(seg[--b], R); } return M::op(L, R); } template int findSubtree(int a, const C& check, T& mono, bool type) { for(; a < SIZ; ) { auto next = M::op(seg[2 * a + type], mono); if(check(next))a = 2 * a + type; else mono = next, a = 2 * a + !type; } return (a - SIZ); } template int findFirst(int a, const C& check) { assert(0 <= a and a <= SIZ); auto L = M::id; if(a <= 0)return (check(M::op(L, seg[1])) ? findSubtree(1, check, L, false) : -1); int b = SIZ; for(a += SIZ, b += SIZ; a < b; a >>= 1, b >>= 1) { if(a & 1) { auto next = M::op(L, seg[a]); if(check(next))return findSubtree(a, check, L, false); L = next; a++; } } return -1; } template int findLast(int b, const C& check) { assert(0 <= b and b <= SIZ); auto R = M::id; if(b >= SIZ)return (check(M::op(seg[1], R)) ? findSubtree(1, check, R, true) : -1); int a = SIZ; for(b += SIZ; a < b; a >>= 1, b >>= 1) { if(b & 1) { auto next = M::op(seg[--b], R); if(check(next))return findSubtree(b, check, R, true); R = next; } } return -1; } }; struct RSumQ { using valueType = int; static int op(int a, int b) { return a + b; } static inline int id{0}; }; struct RSumQLL { using valueType = long long; static long long op(long long a, long long b) { return a + b; } static inline long long id{0}; }; struct RMaxQ { using valueType = int; static int op(int a, int b) { return std::max(a, b); } static inline int id{-(1 << 29)}; }; struct RMaxQLL { using valueType = long long; static long long op(long long a, long long b) { return std::max(a, b); } static inline long long id{-(1ll << 60)}; }; struct RminQ { using valueType = int; static int op(int a, int b) { return std::min(a, b); } static inline int id{1 << 29}; }; struct RminQLL { using valueType = long long; static long long op(long long a, long long b) { return std::min(a, b); } static inline long long id{1ll << 60}; }; } // namespace m9 // #line 2 "structure/UnionFind.hpp" #include #include #include namespace m9 { class UnionFind { int SIZ; std::vector a; public: UnionFind(int n) : SIZ(n), a(n) { for(int i{}; i < SIZ; i++)a[i] = -1; } int root(int x) { assert(x < SIZ); return (a[x] < 0 ? x : (a[x] = root(a[x]))); } bool same(int x, int y) { assert(x < SIZ); assert(y < SIZ); return root(x) == root(y); } void merge(int x, int y) { assert(x < SIZ); assert(y < SIZ); x = root(x), y = root(y); if(x == y)return; if(a[x] > a[y])std::swap(x, y); a[x] += a[y], a[y] = x; } int size(int x) { assert(x < SIZ); return -a[root(x)]; } std::vector> groups() { std::vector rootBuf(SIZ), groupSize(SIZ); for(int i{}; i < SIZ; i++)groupSize[rootBuf[i] = root(i)]++; std::vector> result(SIZ); for(int i{}; i < SIZ; i++)result[i].reserve(groupSize[i]); for(int i{}; i < SIZ; i++)result[rootBuf[i]].emplace_back(i); result.erase( \ remove_if(std::begin(result), std::end(result), [&](const std::vector& v) -> bool { return v.empty(); }), std::end(result)); return result; } }; using uni = UnionFind; } // namespace m9 #endif