結果
問題 | No.2861 Slime Party |
ユーザー | maspy |
提出日時 | 2024-09-11 17:43:06 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,218 ms / 4,000 ms |
コード長 | 44,674 bytes |
コンパイル時間 | 6,993 ms |
コンパイル使用メモリ | 347,372 KB |
実行使用メモリ | 89,496 KB |
最終ジャッジ日時 | 2024-09-11 17:44:14 |
合計ジャッジ時間 | 64,400 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 204 ms
87,684 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 3 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,944 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 2 ms
6,940 KB |
testcase_11 | AC | 4 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,944 KB |
testcase_13 | AC | 5 ms
6,940 KB |
testcase_14 | AC | 4 ms
6,944 KB |
testcase_15 | AC | 4 ms
6,940 KB |
testcase_16 | AC | 3 ms
6,940 KB |
testcase_17 | AC | 3 ms
6,940 KB |
testcase_18 | AC | 3 ms
6,940 KB |
testcase_19 | AC | 2 ms
6,940 KB |
testcase_20 | AC | 4 ms
6,944 KB |
testcase_21 | AC | 985 ms
71,680 KB |
testcase_22 | AC | 996 ms
70,664 KB |
testcase_23 | AC | 1,043 ms
70,440 KB |
testcase_24 | AC | 1,011 ms
71,628 KB |
testcase_25 | AC | 1,040 ms
71,060 KB |
testcase_26 | AC | 1,006 ms
70,732 KB |
testcase_27 | AC | 1,020 ms
71,060 KB |
testcase_28 | AC | 980 ms
70,732 KB |
testcase_29 | AC | 1,021 ms
71,260 KB |
testcase_30 | AC | 995 ms
70,792 KB |
testcase_31 | AC | 1,098 ms
72,076 KB |
testcase_32 | AC | 1,094 ms
70,416 KB |
testcase_33 | AC | 1,081 ms
70,508 KB |
testcase_34 | AC | 1,042 ms
70,976 KB |
testcase_35 | AC | 1,103 ms
70,624 KB |
testcase_36 | AC | 1,018 ms
71,504 KB |
testcase_37 | AC | 995 ms
70,408 KB |
testcase_38 | AC | 1,043 ms
71,196 KB |
testcase_39 | AC | 1,015 ms
71,724 KB |
testcase_40 | AC | 943 ms
70,708 KB |
testcase_41 | AC | 1,092 ms
71,100 KB |
testcase_42 | AC | 1,097 ms
71,092 KB |
testcase_43 | AC | 1,124 ms
70,448 KB |
testcase_44 | AC | 1,218 ms
71,496 KB |
testcase_45 | AC | 1,172 ms
70,916 KB |
testcase_46 | AC | 1,060 ms
71,796 KB |
testcase_47 | AC | 970 ms
70,668 KB |
testcase_48 | AC | 969 ms
70,976 KB |
testcase_49 | AC | 997 ms
70,580 KB |
testcase_50 | AC | 957 ms
71,192 KB |
testcase_51 | AC | 391 ms
87,632 KB |
testcase_52 | AC | 338 ms
88,808 KB |
testcase_53 | AC | 359 ms
88,632 KB |
testcase_54 | AC | 378 ms
87,712 KB |
testcase_55 | AC | 317 ms
88,608 KB |
testcase_56 | AC | 393 ms
87,668 KB |
testcase_57 | AC | 358 ms
88,520 KB |
testcase_58 | AC | 361 ms
88,340 KB |
testcase_59 | AC | 377 ms
87,980 KB |
testcase_60 | AC | 361 ms
87,576 KB |
testcase_61 | AC | 399 ms
89,032 KB |
testcase_62 | AC | 389 ms
87,672 KB |
testcase_63 | AC | 380 ms
89,288 KB |
testcase_64 | AC | 374 ms
88,304 KB |
testcase_65 | AC | 393 ms
87,556 KB |
testcase_66 | AC | 439 ms
88,372 KB |
testcase_67 | AC | 382 ms
88,028 KB |
testcase_68 | AC | 396 ms
88,308 KB |
testcase_69 | AC | 370 ms
87,716 KB |
testcase_70 | AC | 383 ms
88,120 KB |
testcase_71 | AC | 385 ms
88,048 KB |
testcase_72 | AC | 413 ms
89,496 KB |
testcase_73 | AC | 428 ms
88,960 KB |
testcase_74 | AC | 424 ms
87,884 KB |
testcase_75 | AC | 412 ms
87,904 KB |
ソースコード
#line 1 "/home/maspy/compro/library/my_template.hpp" #if defined(LOCAL) #include <my_template_compiled.hpp> #else // https://codeforces.com/blog/entry/96344 #pragma GCC optimize("Ofast,unroll-loops") // いまの CF だとこれ入れると動かない? // #pragma GCC target("avx2,popcnt") #include <bits/stdc++.h> using namespace std; using ll = long long; using u32 = unsigned int; using u64 = unsigned long long; using i128 = __int128; using u128 = unsigned __int128; using f128 = __float128; template <class T> constexpr T infty = 0; template <> constexpr int infty<int> = 1'010'000'000; template <> constexpr ll infty<ll> = 2'020'000'000'000'000'000; template <> constexpr u32 infty<u32> = infty<int>; template <> constexpr u64 infty<u64> = infty<ll>; template <> constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000; template <> constexpr double infty<double> = infty<ll>; template <> constexpr long double infty<long double> = infty<ll>; using pi = pair<ll, ll>; using vi = vector<ll>; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vc<T>>; template <class T> using vvvc = vector<vvc<T>>; template <class T> using vvvvc = vector<vvvc<T>>; template <class T> using vvvvvc = vector<vvvvc<T>>; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; #define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__)) #define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__)))) // https://trap.jp/post/1224/ #define FOR1(a) for (ll _ = 0; _ < ll(a); ++_) #define FOR2(i, a) for (ll i = 0; i < ll(a); ++i) #define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i) #define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c)) #define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i) #define overload4(a, b, c, d, e, ...) e #define overload3(a, b, c, d, ...) d #define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__) #define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__) #define FOR_subset(t, s) for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s))) #define all(x) x.begin(), x.end() #define len(x) ll(x.size()) #define elif else if #define eb emplace_back #define mp make_pair #define mt make_tuple #define fi first #define se second #define stoi stoll int popcnt(int x) { return __builtin_popcount(x); } int popcnt(u32 x) { return __builtin_popcount(x); } int popcnt(ll x) { return __builtin_popcountll(x); } int popcnt(u64 x) { return __builtin_popcountll(x); } int popcnt_mod_2(int x) { return __builtin_parity(x); } int popcnt_mod_2(u32 x) { return __builtin_parity(x); } int popcnt_mod_2(ll x) { return __builtin_parityll(x); } int popcnt_mod_2(u64 x) { return __builtin_parityll(x); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2) int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2) int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template <typename T> T floor(T a, T b) { return a / b - (a % b && (a ^ b) < 0); } template <typename T> T ceil(T x, T y) { return floor(x + y - 1, y); } template <typename T> T bmod(T x, T y) { return x - y * floor(x, y); } template <typename T> pair<T, T> divmod(T x, T y) { T q = floor(x, y); return {q, x - q * y}; } template <typename T, typename U> T SUM(const vector<U> &A) { T sm = 0; for (auto &&a: A) sm += a; return sm; } #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define LB(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define UB(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit() template <typename T> T POP(deque<T> &que) { T a = que.front(); que.pop_front(); return a; } template <typename T> T POP(pq<T> &que) { T a = que.top(); que.pop(); return a; } template <typename T> T POP(pqg<T> &que) { T a = que.top(); que.pop(); return a; } template <typename T> T POP(vc<T> &que) { T a = que.back(); que.pop_back(); return a; } template <typename F> ll binary_search(F check, ll ok, ll ng, bool check_ok = true) { if (check_ok) assert(check(ok)); while (abs(ok - ng) > 1) { auto x = (ng + ok) / 2; (check(x) ? ok : ng) = x; } return ok; } template <typename F> double binary_search_real(F check, double ok, double ng, int iter = 100) { FOR(iter) { double x = (ok + ng) / 2; (check(x) ? ok : ng) = x; } return (ok + ng) / 2; } template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } // ? は -1 vc<int> s_to_vi(const string &S, char first_char) { vc<int> A(S.size()); FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); } return A; } template <typename T, typename U> vector<T> cumsum(vector<U> &A, int off = 1) { int N = A.size(); vector<T> B(N + 1); FOR(i, N) { B[i + 1] = B[i] + A[i]; } if (off == 0) B.erase(B.begin()); return B; } // stable sort template <typename T> vector<int> argsort(const vector<T> &A) { vector<int> ids(len(A)); iota(all(ids), 0); sort(all(ids), [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); }); return ids; } // A[I[0]], A[I[1]], ... template <typename T> vc<T> rearrange(const vc<T> &A, const vc<int> &I) { vc<T> B(len(I)); FOR(i, len(I)) B[i] = A[I[i]]; return B; } template <typename T, typename... Vectors> void concat(vc<T> &first, const Vectors &... others) { vc<T> &res = first; (res.insert(res.end(), others.begin(), others.end()), ...); } #endif #line 1 "/home/maspy/compro/library/other/io.hpp" #define FASTIO #include <unistd.h> // https://judge.yosupo.jp/submission/21623 namespace fastio { static constexpr uint32_t SZ = 1 << 17; char ibuf[SZ]; char obuf[SZ]; char out[100]; // pointer of ibuf, obuf uint32_t pil = 0, pir = 0, por = 0; struct Pre { char num[10000][4]; constexpr Pre() : num() { for (int i = 0; i < 10000; i++) { int n = i; for (int j = 3; j >= 0; j--) { num[i][j] = n % 10 | '0'; n /= 10; } } } } constexpr pre; inline void load() { memcpy(ibuf, ibuf + pil, pir - pil); pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin); pil = 0; if (pir < SZ) ibuf[pir++] = '\n'; } inline void flush() { fwrite(obuf, 1, por, stdout); por = 0; } void rd(char &c) { do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (isspace(c)); } void rd(string &x) { x.clear(); char c; do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (isspace(c)); do { x += c; if (pil == pir) load(); c = ibuf[pil++]; } while (!isspace(c)); } template <typename T> void rd_real(T &x) { string s; rd(s); x = stod(s); } template <typename T> void rd_integer(T &x) { if (pil + 100 > pir) load(); char c; do c = ibuf[pil++]; while (c < '-'); bool minus = 0; if constexpr (is_signed<T>::value || is_same_v<T, i128>) { if (c == '-') { minus = 1, c = ibuf[pil++]; } } x = 0; while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; } if constexpr (is_signed<T>::value || is_same_v<T, i128>) { if (minus) x = -x; } } void rd(int &x) { rd_integer(x); } void rd(ll &x) { rd_integer(x); } void rd(i128 &x) { rd_integer(x); } void rd(u32 &x) { rd_integer(x); } void rd(u64 &x) { rd_integer(x); } void rd(u128 &x) { rd_integer(x); } void rd(double &x) { rd_real(x); } void rd(long double &x) { rd_real(x); } void rd(f128 &x) { rd_real(x); } template <class T, class U> void rd(pair<T, U> &p) { return rd(p.first), rd(p.second); } template <size_t N = 0, typename T> void rd_tuple(T &t) { if constexpr (N < std::tuple_size<T>::value) { auto &x = std::get<N>(t); rd(x); rd_tuple<N + 1>(t); } } template <class... T> void rd(tuple<T...> &tpl) { rd_tuple(tpl); } template <size_t N = 0, typename T> void rd(array<T, N> &x) { for (auto &d: x) rd(d); } template <class T> void rd(vc<T> &x) { for (auto &d: x) rd(d); } void read() {} template <class H, class... T> void read(H &h, T &... t) { rd(h), read(t...); } void wt(const char c) { if (por == SZ) flush(); obuf[por++] = c; } void wt(const string s) { for (char c: s) wt(c); } void wt(const char *s) { size_t len = strlen(s); for (size_t i = 0; i < len; i++) wt(s[i]); } template <typename T> void wt_integer(T x) { if (por > SZ - 100) flush(); if (x < 0) { obuf[por++] = '-', x = -x; } int outi; for (outi = 96; x >= 10000; outi -= 4) { memcpy(out + outi, pre.num[x % 10000], 4); x /= 10000; } if (x >= 1000) { memcpy(obuf + por, pre.num[x], 4); por += 4; } else if (x >= 100) { memcpy(obuf + por, pre.num[x] + 1, 3); por += 3; } else if (x >= 10) { int q = (x * 103) >> 10; obuf[por] = q | '0'; obuf[por + 1] = (x - q * 10) | '0'; por += 2; } else obuf[por++] = x | '0'; memcpy(obuf + por, out + outi + 4, 96 - outi); por += 96 - outi; } template <typename T> void wt_real(T x) { ostringstream oss; oss << fixed << setprecision(15) << double(x); string s = oss.str(); wt(s); } void wt(int x) { wt_integer(x); } void wt(ll x) { wt_integer(x); } void wt(i128 x) { wt_integer(x); } void wt(u32 x) { wt_integer(x); } void wt(u64 x) { wt_integer(x); } void wt(u128 x) { wt_integer(x); } void wt(double x) { wt_real(x); } void wt(long double x) { wt_real(x); } void wt(f128 x) { wt_real(x); } template <class T, class U> void wt(const pair<T, U> val) { wt(val.first); wt(' '); wt(val.second); } template <size_t N = 0, typename T> void wt_tuple(const T t) { if constexpr (N < std::tuple_size<T>::value) { if constexpr (N > 0) { wt(' '); } const auto x = std::get<N>(t); wt(x); wt_tuple<N + 1>(t); } } template <class... T> void wt(tuple<T...> tpl) { wt_tuple(tpl); } template <class T, size_t S> void wt(const array<T, S> val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } template <class T> void wt(const vector<T> val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } void print() { wt('\n'); } template <class Head, class... Tail> void print(Head &&head, Tail &&... tail) { wt(head); if (sizeof...(Tail)) wt(' '); print(forward<Tail>(tail)...); } // gcc expansion. called automaticall after main. void __attribute__((destructor)) _d() { flush(); } } // namespace fastio using fastio::read; using fastio::print; using fastio::flush; #if defined(LOCAL) #define SHOW(...) SHOW_IMPL(__VA_ARGS__, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, SHOW1)(__VA_ARGS__) #define SHOW_IMPL(_1, _2, _3, _4, _5, _6, NAME, ...) NAME #define SHOW1(x) print(#x, "=", (x)), flush() #define SHOW2(x, y) print(#x, "=", (x), #y, "=", (y)), flush() #define SHOW3(x, y, z) print(#x, "=", (x), #y, "=", (y), #z, "=", (z)), flush() #define SHOW4(x, y, z, w) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush() #define SHOW5(x, y, z, w, v) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v)), flush() #define SHOW6(x, y, z, w, v, u) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v), #u, "=", (u)), flush() #else #define SHOW(...) #endif #define INT(...) \ int __VA_ARGS__; \ read(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ read(__VA_ARGS__) #define U32(...) \ u32 __VA_ARGS__; \ read(__VA_ARGS__) #define U64(...) \ u64 __VA_ARGS__; \ read(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ read(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ read(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ read(__VA_ARGS__) #define VEC(type, name, size) \ vector<type> name(size); \ read(name) #define VV(type, name, h, w) \ vector<vector<type>> name(h, vector<type>(w)); \ read(name) void YES(bool t = 1) { print(t ? "YES" : "NO"); } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { print(t ? "Yes" : "No"); } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { print(t ? "yes" : "no"); } void no(bool t = 1) { yes(!t); } #line 3 "main.cpp" #line 2 "/home/maspy/compro/library/ds/unionfind/unionfind.hpp" struct UnionFind { int n, n_comp; vc<int> dat; // par or (-size) UnionFind(int n = 0) { build(n); } void build(int m) { n = m, n_comp = m; dat.assign(n, -1); } void reset() { build(n); } int operator[](int x) { while (dat[x] >= 0) { int pp = dat[dat[x]]; if (pp < 0) { return dat[x]; } x = dat[x] = pp; } return x; } ll size(int x) { x = (*this)[x]; return -dat[x]; } bool merge(int x, int y) { x = (*this)[x], y = (*this)[y]; if (x == y) return false; if (-dat[x] < -dat[y]) swap(x, y); dat[x] += dat[y], dat[y] = x, n_comp--; return true; } vc<int> get_all() { vc<int> A(n); FOR(i, n) A[i] = (*this)[i]; return A; } }; #line 2 "/home/maspy/compro/library/graph/tree.hpp" #line 2 "/home/maspy/compro/library/graph/base.hpp" template <typename T> struct Edge { int frm, to; T cost; int id; }; template <typename T = int, bool directed = false> struct Graph { static constexpr bool is_directed = directed; int N, M; using cost_type = T; using edge_type = Edge<T>; vector<edge_type> edges; vector<int> indptr; vector<edge_type> csr_edges; vc<int> vc_deg, vc_indeg, vc_outdeg; bool prepared; class OutgoingEdges { public: OutgoingEdges(const Graph* G, int l, int r) : G(G), l(l), r(r) {} const edge_type* begin() const { if (l == r) { return 0; } return &G->csr_edges[l]; } const edge_type* end() const { if (l == r) { return 0; } return &G->csr_edges[r]; } private: const Graph* G; int l, r; }; bool is_prepared() { return prepared; } Graph() : N(0), M(0), prepared(0) {} Graph(int N) : N(N), M(0), prepared(0) {} void build(int n) { N = n, M = 0; prepared = 0; edges.clear(); indptr.clear(); csr_edges.clear(); vc_deg.clear(); vc_indeg.clear(); vc_outdeg.clear(); } void add(int frm, int to, T cost = 1, int i = -1) { assert(!prepared); assert(0 <= frm && 0 <= to && to < N); if (i == -1) i = M; auto e = edge_type({frm, to, cost, i}); edges.eb(e); ++M; } #ifdef FASTIO // wt, off void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); } void read_graph(int M, bool wt = false, int off = 1) { for (int m = 0; m < M; ++m) { INT(a, b); a -= off, b -= off; if (!wt) { add(a, b); } else { T c; read(c); add(a, b, c); } } build(); } #endif void build() { assert(!prepared); prepared = true; indptr.assign(N + 1, 0); for (auto&& e: edges) { indptr[e.frm + 1]++; if (!directed) indptr[e.to + 1]++; } for (int v = 0; v < N; ++v) { indptr[v + 1] += indptr[v]; } auto counter = indptr; csr_edges.resize(indptr.back() + 1); for (auto&& e: edges) { csr_edges[counter[e.frm]++] = e; if (!directed) csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id}); } } OutgoingEdges operator[](int v) const { assert(prepared); return {this, indptr[v], indptr[v + 1]}; } vc<int> deg_array() { if (vc_deg.empty()) calc_deg(); return vc_deg; } pair<vc<int>, vc<int>> deg_array_inout() { if (vc_indeg.empty()) calc_deg_inout(); return {vc_indeg, vc_outdeg}; } int deg(int v) { if (vc_deg.empty()) calc_deg(); return vc_deg[v]; } int in_deg(int v) { if (vc_indeg.empty()) calc_deg_inout(); return vc_indeg[v]; } int out_deg(int v) { if (vc_outdeg.empty()) calc_deg_inout(); return vc_outdeg[v]; } #ifdef FASTIO void debug() { print("Graph"); if (!prepared) { print("frm to cost id"); for (auto&& e: edges) print(e.frm, e.to, e.cost, e.id); } else { print("indptr", indptr); print("frm to cost id"); FOR(v, N) for (auto&& e: (*this)[v]) print(e.frm, e.to, e.cost, e.id); } } #endif vc<int> new_idx; vc<bool> used_e; // G における頂点 V[i] が、新しいグラフで i になるようにする // {G, es} // sum(deg(v)) の計算量になっていて、 // 新しいグラフの n+m より大きい可能性があるので注意 Graph<T, directed> rearrange(vc<int> V, bool keep_eid = 0) { if (len(new_idx) != N) new_idx.assign(N, -1); int n = len(V); FOR(i, n) new_idx[V[i]] = i; Graph<T, directed> G(n); vc<int> history; FOR(i, n) { for (auto&& e: (*this)[V[i]]) { if (len(used_e) <= e.id) used_e.resize(e.id + 1); if (used_e[e.id]) continue; int a = e.frm, b = e.to; if (new_idx[a] != -1 && new_idx[b] != -1) { history.eb(e.id); used_e[e.id] = 1; int eid = (keep_eid ? e.id : -1); G.add(new_idx[a], new_idx[b], e.cost, eid); } } } FOR(i, n) new_idx[V[i]] = -1; for (auto&& eid: history) used_e[eid] = 0; G.build(); return G; } Graph<T, true> to_directed_tree(int root = -1) { if (root == -1) root = 0; assert(!is_directed && prepared && M == N - 1); Graph<T, true> G1(N); vc<int> par(N, -1); auto dfs = [&](auto& dfs, int v) -> void { for (auto& e: (*this)[v]) { if (e.to == par[v]) continue; par[e.to] = v, dfs(dfs, e.to); } }; dfs(dfs, root); for (auto& e: edges) { int a = e.frm, b = e.to; if (par[a] == b) swap(a, b); assert(par[b] == a); G1.add(a, b, e.cost); } G1.build(); return G1; } private: void calc_deg() { assert(vc_deg.empty()); vc_deg.resize(N); for (auto&& e: edges) vc_deg[e.frm]++, vc_deg[e.to]++; } void calc_deg_inout() { assert(vc_indeg.empty()); vc_indeg.resize(N); vc_outdeg.resize(N); for (auto&& e: edges) { vc_indeg[e.to]++, vc_outdeg[e.frm]++; } } }; #line 4 "/home/maspy/compro/library/graph/tree.hpp" // HLD euler tour をとっていろいろ。 template <typename GT> struct Tree { using Graph_type = GT; GT &G; using WT = typename GT::cost_type; int N; vector<int> LID, RID, head, V, parent, VtoE; vc<int> depth; vc<WT> depth_weighted; Tree(GT &G, int r = 0, bool hld = 1) : G(G) { build(r, hld); } void build(int r = 0, bool hld = 1) { if (r == -1) return; // build を遅延したいとき N = G.N; LID.assign(N, -1), RID.assign(N, -1), head.assign(N, r); V.assign(N, -1), parent.assign(N, -1), VtoE.assign(N, -1); depth.assign(N, -1), depth_weighted.assign(N, 0); assert(G.is_prepared()); int t1 = 0; dfs_sz(r, -1, hld); dfs_hld(r, t1); } void dfs_sz(int v, int p, bool hld) { auto &sz = RID; parent[v] = p; depth[v] = (p == -1 ? 0 : depth[p] + 1); sz[v] = 1; int l = G.indptr[v], r = G.indptr[v + 1]; auto &csr = G.csr_edges; // 使う辺があれば先頭にする for (int i = r - 2; i >= l; --i) { if (hld && depth[csr[i + 1].to] == -1) swap(csr[i], csr[i + 1]); } int hld_sz = 0; for (int i = l; i < r; ++i) { auto e = csr[i]; if (depth[e.to] != -1) continue; depth_weighted[e.to] = depth_weighted[v] + e.cost; VtoE[e.to] = e.id; dfs_sz(e.to, v, hld); sz[v] += sz[e.to]; if (hld && chmax(hld_sz, sz[e.to]) && l < i) { swap(csr[l], csr[i]); } } } void dfs_hld(int v, int ×) { LID[v] = times++; RID[v] += LID[v]; V[LID[v]] = v; bool heavy = true; for (auto &&e: G[v]) { if (depth[e.to] <= depth[v]) continue; head[e.to] = (heavy ? head[v] : e.to); heavy = false; dfs_hld(e.to, times); } } vc<int> heavy_path_at(int v) { vc<int> P = {v}; while (1) { int a = P.back(); for (auto &&e: G[a]) { if (e.to != parent[a] && head[e.to] == v) { P.eb(e.to); break; } } if (P.back() == a) break; } return P; } int heavy_child(int v) { int k = LID[v] + 1; if (k == N) return -1; int w = V[k]; return (parent[w] == v ? w : -1); } int e_to_v(int eid) { auto e = G.edges[eid]; return (parent[e.frm] == e.to ? e.frm : e.to); } int v_to_e(int v) { return VtoE[v]; } int get_eid(int u, int v) { if (parent[u] != v) swap(u, v); assert(parent[u] == v); return VtoE[u]; } int ELID(int v) { return 2 * LID[v] - depth[v]; } int ERID(int v) { return 2 * RID[v] - depth[v] - 1; } // 目標地点へ進む個数が k int LA(int v, int k) { assert(k <= depth[v]); while (1) { int u = head[v]; if (LID[v] - k >= LID[u]) return V[LID[v] - k]; k -= LID[v] - LID[u] + 1; v = parent[u]; } } int la(int u, int v) { return LA(u, v); } int LCA(int u, int v) { for (;; v = parent[head[v]]) { if (LID[u] > LID[v]) swap(u, v); if (head[u] == head[v]) return u; } } int meet(int a, int b, int c) { return LCA(a, b) ^ LCA(a, c) ^ LCA(b, c); } int lca(int u, int v) { return LCA(u, v); } int subtree_size(int v, int root = -1) { if (root == -1) return RID[v] - LID[v]; if (v == root) return N; int x = jump(v, root, 1); if (in_subtree(v, x)) return RID[v] - LID[v]; return N - RID[x] + LID[x]; } int dist(int a, int b) { int c = LCA(a, b); return depth[a] + depth[b] - 2 * depth[c]; } WT dist_weighted(int a, int b) { int c = LCA(a, b); return depth_weighted[a] + depth_weighted[b] - WT(2) * depth_weighted[c]; } // a is in b bool in_subtree(int a, int b) { return LID[b] <= LID[a] && LID[a] < RID[b]; } int jump(int a, int b, ll k) { if (k == 1) { if (a == b) return -1; return (in_subtree(b, a) ? LA(b, depth[b] - depth[a] - 1) : parent[a]); } int c = LCA(a, b); int d_ac = depth[a] - depth[c]; int d_bc = depth[b] - depth[c]; if (k > d_ac + d_bc) return -1; if (k <= d_ac) return LA(a, k); return LA(b, d_ac + d_bc - k); } vc<int> collect_child(int v) { vc<int> res; for (auto &&e: G[v]) if (e.to != parent[v]) res.eb(e.to); return res; } vc<int> collect_light(int v) { vc<int> res; bool skip = true; for (auto &&e: G[v]) if (e.to != parent[v]) { if (!skip) res.eb(e.to); skip = false; } return res; } vc<pair<int, int>> get_path_decomposition(int u, int v, bool edge) { // [始点, 終点] の"閉"区間列。 vc<pair<int, int>> up, down; while (1) { if (head[u] == head[v]) break; if (LID[u] < LID[v]) { down.eb(LID[head[v]], LID[v]); v = parent[head[v]]; } else { up.eb(LID[u], LID[head[u]]); u = parent[head[u]]; } } if (LID[u] < LID[v]) down.eb(LID[u] + edge, LID[v]); elif (LID[v] + edge <= LID[u]) up.eb(LID[u], LID[v] + edge); reverse(all(down)); up.insert(up.end(), all(down)); return up; } // 辺の列の情報 (frm,to,str) // str = "heavy_up", "heavy_down", "light_up", "light_down" vc<tuple<int, int, string>> get_path_decomposition_detail(int u, int v) { vc<tuple<int, int, string>> up, down; while (1) { if (head[u] == head[v]) break; if (LID[u] < LID[v]) { if (v != head[v]) down.eb(head[v], v, "heavy_down"), v = head[v]; down.eb(parent[v], v, "light_down"), v = parent[v]; } else { if (u != head[u]) up.eb(u, head[u], "heavy_up"), u = head[u]; up.eb(u, parent[u], "light_up"), u = parent[u]; } } if (LID[u] < LID[v]) down.eb(u, v, "heavy_down"); elif (LID[v] < LID[u]) up.eb(u, v, "heavy_up"); reverse(all(down)); concat(up, down); return up; } vc<int> restore_path(int u, int v) { vc<int> P; for (auto &&[a, b]: get_path_decomposition(u, v, 0)) { if (a <= b) { FOR(i, a, b + 1) P.eb(V[i]); } else { FOR_R(i, b, a + 1) P.eb(V[i]); } } return P; } // path [a,b] と [c,d] の交わり. 空ならば {-1,-1}. // https://codeforces.com/problemset/problem/500/G pair<int, int> path_intersection(int a, int b, int c, int d) { int ab = lca(a, b), ac = lca(a, c), ad = lca(a, d); int bc = lca(b, c), bd = lca(b, d), cd = lca(c, d); int x = ab ^ ac ^ bc, y = ab ^ ad ^ bd; // meet(a,b,c), meet(a,b,d) if (x != y) return {x, y}; int z = ac ^ ad ^ cd; if (x != z) x = -1; return {x, x}; } }; #line 2 "/home/maspy/compro/library/alg/monoid/monoid_reverse.hpp" template <class Monoid> struct Monoid_Reverse { using value_type = typename Monoid::value_type; using X = value_type; static constexpr X op(const X &x, const X &y) { return Monoid::op(y, x); } static constexpr X unit() { return Monoid::unit(); } static const bool commute = Monoid::commute; }; #line 2 "/home/maspy/compro/library/ds/segtree/lazy_segtree.hpp" template <typename ActedMonoid> struct Lazy_SegTree { using AM = ActedMonoid; using MX = typename AM::Monoid_X; using MA = typename AM::Monoid_A; using X = typename MX::value_type; using A = typename MA::value_type; int n, log, size; vc<X> dat; vc<A> laz; Lazy_SegTree() {} Lazy_SegTree(int n) { build(n); } template <typename F> Lazy_SegTree(int n, F f) { build(n, f); } Lazy_SegTree(const vc<X>& v) { build(v); } void build(int m) { build(m, [](int i) -> X { return MX::unit(); }); } void build(const vc<X>& v) { build(len(v), [&](int i) -> X { return v[i]; }); } template <typename F> void build(int m, F f) { n = m, log = 1; while ((1 << log) < n) ++log; size = 1 << log; dat.assign(size << 1, MX::unit()); laz.assign(size, MA::unit()); FOR(i, n) dat[size + i] = f(i); FOR_R(i, 1, size) update(i); } void update(int k) { dat[k] = MX::op(dat[2 * k], dat[2 * k + 1]); } void set(int p, X x) { assert(0 <= p && p < n); p += size; for (int i = log; i >= 1; i--) push(p >> i); dat[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } void multiply(int p, const X& x) { assert(0 <= p && p < n); p += size; for (int i = log; i >= 1; i--) push(p >> i); dat[p] = MX::op(dat[p], x); for (int i = 1; i <= log; i++) update(p >> i); } X get(int p) { assert(0 <= p && p < n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return dat[p]; } vc<X> get_all() { FOR(k, 1, size) { push(k); } return {dat.begin() + size, dat.begin() + size + n}; } X prod(int l, int r) { assert(0 <= l && l <= r && r <= n); if (l == r) return MX::unit(); l += size, r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } X xl = MX::unit(), xr = MX::unit(); while (l < r) { if (l & 1) xl = MX::op(xl, dat[l++]); if (r & 1) xr = MX::op(dat[--r], xr); l >>= 1, r >>= 1; } return MX::op(xl, xr); } X prod_all() { return dat[1]; } void apply(int l, int r, A a) { assert(0 <= l && l <= r && r <= n); if (l == r) return; l += size, r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } int l2 = l, r2 = r; while (l < r) { if (l & 1) apply_at(l++, a); if (r & 1) apply_at(--r, a); l >>= 1, r >>= 1; } l = l2, r = r2; for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template <typename F> int max_right(const F check, int l) { assert(0 <= l && l <= n); assert(check(MX::unit())); if (l == n) return n; l += size; for (int i = log; i >= 1; i--) push(l >> i); X sm = MX::unit(); do { while (l % 2 == 0) l >>= 1; if (!check(MX::op(sm, dat[l]))) { while (l < size) { push(l); l = (2 * l); if (check(MX::op(sm, dat[l]))) { sm = MX::op(sm, dat[l++]); } } return l - size; } sm = MX::op(sm, dat[l++]); } while ((l & -l) != l); return n; } template <typename F> int min_left(const F check, int r) { assert(0 <= r && r <= n); assert(check(MX::unit())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); X sm = MX::unit(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!check(MX::op(dat[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (check(MX::op(dat[r], sm))) { sm = MX::op(dat[r--], sm); } } return r + 1 - size; } sm = MX::op(dat[r], sm); } while ((r & -r) != r); return 0; } private: void apply_at(int k, A a) { ll sz = 1 << (log - topbit(k)); dat[k] = AM::act(dat[k], a, sz); if (k < size) laz[k] = MA::op(laz[k], a); } void push(int k) { if (laz[k] == MA::unit()) return; apply_at(2 * k, laz[k]), apply_at(2 * k + 1, laz[k]); laz[k] = MA::unit(); } }; #line 4 "/home/maspy/compro/library/graph/ds/lazy_tree_monoid.hpp" template <typename TREE, typename ActedMonoid, bool edge> struct Lazy_Tree_Monoid { using MX = typename ActedMonoid::Monoid_X; using MA = typename ActedMonoid::Monoid_A; using X = typename MX::value_type; using A = typename MA::value_type; struct RevAM { using Monoid_X = Monoid_Reverse<MX>; using Monoid_A = MA; using X = typename Monoid_X::value_type; using A = typename Monoid_A::value_type; static X act(const X &x, const A &a, const ll &size) { return ActedMonoid::act(x, a, size); } }; TREE &tree; int N; Lazy_SegTree<ActedMonoid> seg; Lazy_SegTree<RevAM> seg_r; Lazy_Tree_Monoid(TREE &tree) : tree(tree), N(tree.N) { build([](int i) -> X { return MX::unit(); }); } Lazy_Tree_Monoid(TREE &tree, vc<X> &dat) : tree(tree), N(tree.N) { build([&](int i) -> X { return dat[i]; }); } template <typename F> Lazy_Tree_Monoid(TREE &tree, F f) : tree(tree), N(tree.N) { build(f); } template <typename F> void build(F f) { if (!edge) { auto f_v = [&](int i) -> X { return f(tree.V[i]); }; seg.build(N, f_v); if constexpr (!MX::commute) { seg_r.build(N, f_v); } } else { auto f_e = [&](int i) -> X { return (i == 0 ? MX::unit() : f(tree.v_to_e(tree.V[i]))); }; seg.build(N, f_e); if constexpr (!MX::commute) { seg_r.build(N, f_e); } } } void set(int i, X x) { if constexpr (edge) i = tree.e_to_v(i); i = tree.LID[i]; seg.set(i, x); if constexpr (!MX::commute) { seg_r.set(i, x); } } X get(int v) { return seg.get(tree.LID[v]); } vc<X> get_all() { vc<X> dat = seg.get_all(); if (!edge) { vc<X> res(N); FOR(v, N) res[v] = dat[tree.LID[v]]; return res; } else { vc<X> res(N - 1); FOR(i, N - 1) { res[i] = dat[tree.LID[tree.e_to_v(i)]]; } return res; } } X prod_path(int u, int v) { auto pd = tree.get_path_decomposition(u, v, edge); X val = MX::unit(); for (auto &&[a, b]: pd) { val = MX::op(val, get_prod(a, b)); } return val; } X prod_subtree(int u, int root = -1) { if (root == u) return prod_all(); if (root == -1 || tree.in_subtree(u, root)) { int l = tree.LID[u], r = tree.RID[u]; return seg.prod(l + edge, r); } assert(!edge); // さぼり u = tree.jump(u, root, 1); int L = tree.LID[u], R = tree.RID[u]; return MX::op(seg.prod(0, L), seg.prod(R, N)); } X prod_all() { static_assert(MX::commute); return seg.prod_all(); } void apply_path(int u, int v, A a) { auto pd = tree.get_path_decomposition(u, v, edge); for (auto &&[x, y]: pd) { int l = min(x, y), r = max(x, y); seg.apply(l, r + 1, a); if constexpr (!MX::commute) { seg_r.apply(l, r + 1, a); } } } void apply_subtree(int u, A a) { int l = tree.LID[u], r = tree.RID[u]; seg.apply(l + edge, r, a); if constexpr (!MX::commute) { seg_r.apply(l + edge, r, a); } } void apply_outtree(int u, A a) { int l = tree.LID[u], r = tree.RID[u]; seg.apply(0 + edge, l + edge, a); seg.apply(r, N, a); if constexpr (!MX::commute) { seg_r.apply(0 + edge, l + edge, a); seg_r.apply(r, N, a); } } template <class F> int max_path(F check, int u, int v) { if constexpr (edge) return max_path_edge(check, u, v); if (!check(prod_path(u, u))) return -1; auto pd = tree.get_path_decomposition(u, v, edge); X val = MX::unit(); for (auto &&[a, b]: pd) { X x = get_prod(a, b); if (check(MX::op(val, x))) { val = MX::op(val, x); u = (tree.V[b]); continue; } auto check_tmp = [&](X x) -> bool { return check(MX::op(val, x)); }; if (a <= b) { // 下り auto i = seg.max_right(check_tmp, a); return (i == a ? u : tree.V[i - 1]); } else { // 上り int i = 0; if constexpr (MX::commute) i = seg.min_left(check_tmp, a + 1); if constexpr (!MX::commute) i = seg_r.min_left(check_tmp, a + 1); if (i == a + 1) return u; return tree.V[i]; } } return v; } // closed range [a,b] を heavy path の形式に応じて inline X get_prod(int a, int b) { if constexpr (MX::commute) return (a <= b ? seg.prod(a, b + 1) : seg.prod(b, a + 1)); return (a <= b ? seg.prod(a, b + 1) : seg_r.prod(b, a + 1)); } private: template <class F> int max_path_edge(F check, int u, int v) { static_assert(edge); if (!check(MX::unit())) return -1; int lca = tree.lca(u, v); auto pd = tree.get_path_decomposition(u, lca, edge); X val = MX::unit(); // climb for (auto &&[a, b]: pd) { assert(a >= b); X x = get_prod(a, b); if (check(MX::op(val, x))) { val = MX::op(val, x); u = (tree.parent[tree.V[b]]); continue; } auto check_tmp = [&](X x) -> bool { return check(MX::op(val, x)); }; int i = 0; if constexpr (MX::commute) i = seg.min_left(check_tmp, a + 1); if constexpr (!MX::commute) i = seg_r.min_left(check_tmp, a + 1); if (i == a + 1) return u; return tree.parent[tree.V[i]]; } // down pd = tree.get_path_decomposition(lca, v, edge); for (auto &&[a, b]: pd) { assert(a <= b); X x = get_prod(a, b); if (check(MX::op(val, x))) { val = MX::op(val, x); u = (tree.V[b]); continue; } auto check_tmp = [&](X x) -> bool { return check(MX::op(val, x)); }; auto i = seg.max_right(check_tmp, a); return (i == a ? u : tree.V[i - 1]); } return v; } }; #line 2 "/home/maspy/compro/library/alg/monoid/add.hpp" template <typename E> struct Monoid_Add { using X = E; using value_type = X; static constexpr X op(const X &x, const X &y) noexcept { return x + y; } static constexpr X inverse(const X &x) noexcept { return -x; } static constexpr X power(const X &x, ll n) noexcept { return X(n) * x; } static constexpr X unit() { return X(0); } static constexpr bool commute = true; }; #line 2 "/home/maspy/compro/library/alg/monoid/max.hpp" template <typename E> struct Monoid_Max { using X = E; using value_type = X; static constexpr X op(const X &x, const X &y) noexcept { return max(x, y); } static constexpr X unit() { return -infty<E>; } static constexpr bool commute = true; }; #line 3 "/home/maspy/compro/library/alg/acted_monoid/max_add.hpp" template <typename E> struct ActedMonoid_Max_Add { using Monoid_X = Monoid_Max<E>; using Monoid_A = Monoid_Add<E>; using X = typename Monoid_X::value_type; using A = typename Monoid_A::value_type; static constexpr X act(const X &x, const A &a, const ll &size) { if (x == -infty<E>) return x; return x + a; } }; #line 3 "/home/maspy/compro/library/ds/fenwicktree/fenwicktree.hpp" template <typename Monoid> struct FenwickTree { using G = Monoid; using MX = Monoid; using E = typename G::value_type; int n; vector<E> dat; E total; FenwickTree() {} FenwickTree(int n) { build(n); } template <typename F> FenwickTree(int n, F f) { build(n, f); } FenwickTree(const vc<E>& v) { build(v); } void build(int m) { n = m; dat.assign(m, G::unit()); total = G::unit(); } void build(const vc<E>& v) { build(len(v), [&](int i) -> E { return v[i]; }); } template <typename F> void build(int m, F f) { n = m; dat.clear(); dat.reserve(n); total = G::unit(); FOR(i, n) { dat.eb(f(i)); } for (int i = 1; i <= n; ++i) { int j = i + (i & -i); if (j <= n) dat[j - 1] = G::op(dat[i - 1], dat[j - 1]); } total = prefix_sum(m); } E prod_all() { return total; } E sum_all() { return total; } E sum(int k) { return prefix_sum(k); } E prod(int k) { return prefix_prod(k); } E prefix_sum(int k) { return prefix_prod(k); } E prefix_prod(int k) { chmin(k, n); E ret = G::unit(); for (; k > 0; k -= k & -k) ret = G::op(ret, dat[k - 1]); return ret; } E sum(int L, int R) { return prod(L, R); } E prod(int L, int R) { chmax(L, 0), chmin(R, n); if (L == 0) return prefix_prod(R); assert(0 <= L && L <= R && R <= n); E pos = G::unit(), neg = G::unit(); while (L < R) { pos = G::op(pos, dat[R - 1]), R -= R & -R; } while (R < L) { neg = G::op(neg, dat[L - 1]), L -= L & -L; } return G::op(pos, G::inverse(neg)); } vc<E> get_all() { vc<E> res(n); FOR(i, n) res[i] = prod(i, i + 1); return res; } void add(int k, E x) { multiply(k, x); } void multiply(int k, E x) { static_assert(G::commute); total = G::op(total, x); for (++k; k <= n; k += k & -k) dat[k - 1] = G::op(dat[k - 1], x); } template <class F> int max_right(const F check, int L = 0) { assert(check(G::unit())); E s = G::unit(); int i = L; // 2^k 進むとダメ int k = [&]() { while (1) { if (i % 2 == 1) { s = G::op(s, G::inverse(dat[i - 1])), i -= 1; } if (i == 0) { return topbit(n) + 1; } int k = lowbit(i) - 1; if (i + (1 << k) > n) return k; E t = G::op(s, dat[i + (1 << k) - 1]); if (!check(t)) { return k; } s = G::op(s, G::inverse(dat[i - 1])), i -= i & -i; } }(); while (k) { --k; if (i + (1 << k) - 1 < len(dat)) { E t = G::op(s, dat[i + (1 << k) - 1]); if (check(t)) { i += (1 << k), s = t; } } } return i; } // check(i, x) template <class F> int max_right_with_index(const F check, int L = 0) { assert(check(L, G::unit())); E s = G::unit(); int i = L; // 2^k 進むとダメ int k = [&]() { while (1) { if (i % 2 == 1) { s = G::op(s, G::inverse(dat[i - 1])), i -= 1; } if (i == 0) { return topbit(n) + 1; } int k = lowbit(i) - 1; if (i + (1 << k) > n) return k; E t = G::op(s, dat[i + (1 << k) - 1]); if (!check(i + (1 << k), t)) { return k; } s = G::op(s, G::inverse(dat[i - 1])), i -= i & -i; } }(); while (k) { --k; if (i + (1 << k) - 1 < len(dat)) { E t = G::op(s, dat[i + (1 << k) - 1]); if (check(i + (1 << k), t)) { i += (1 << k), s = t; } } } return i; } template <class F> int min_left(const F check, int R) { assert(check(G::unit())); E s = G::unit(); int i = R; // false になるところまで戻る int k = 0; while (i > 0 && check(s)) { s = G::op(s, dat[i - 1]); k = lowbit(i); i -= i & -i; } if (check(s)) { assert(i == 0); return 0; } // 2^k 進むと ok になる // false を維持して進む while (k) { --k; E t = G::op(s, G::inverse(dat[i + (1 << k) - 1])); if (!check(t)) { i += (1 << k), s = t; } } return i + 1; } int kth(E k, int L = 0) { return max_right([&k](E x) -> bool { return x <= k; }, L); } }; #line 3 "/home/maspy/compro/library/graph/ds/tree_abelgroup.hpp" template <typename TREE, typename AbelGroup, bool edge, bool path_query, bool subtree_query> struct Tree_AbelGroup { using MX = AbelGroup; using X = typename MX::value_type; TREE &tree; int N; FenwickTree<MX> bit, bit_subtree; Tree_AbelGroup(TREE &tree) : tree(tree), N(tree.N) { build([](int i) -> X { return MX::unit(); }); } Tree_AbelGroup(TREE &tree, vc<X> &dat) : tree(tree), N(tree.N) { build([&](int i) -> X { return dat[i]; }); } template <typename F> Tree_AbelGroup(TREE &tree, F f) : tree(tree), N(tree.N) { build(f); } template <typename F> void build(F f) { vc<X> bit_raw_1(2 * N); vc<X> bit_raw_2(N); FOR(v, N) { X x = MX::unit(); if (!edge) x = f(v); if (edge) x = (v == 0 ? MX::unit() : f(tree.v_to_e(v))); bit_raw_1[tree.ELID(v)] = x; bit_raw_1[tree.ERID(v)] = MX::inverse(x); bit_raw_2[tree.LID[v]] = x; } if constexpr (path_query) bit.build(bit_raw_1); if constexpr (subtree_query) bit_subtree.build(bit_raw_2); } void add(int i, X x) { int v = (edge ? tree.e_to_v(i) : i); if constexpr (path_query) { bit.add(tree.ELID(v), x); bit.add(tree.ERID(v), MX::inverse(x)); } if constexpr (subtree_query) bit_subtree.add(tree.LID[v], x); } X prod_path(int frm, int to) { static_assert(path_query); int lca = tree.LCA(frm, to); // [frm, lca) X x1 = bit.prod(tree.ELID(lca) + 1, tree.ELID(frm) + 1); // edge なら (lca, to]、vertex なら [lca, to] X x2 = bit.prod(tree.ELID(lca) + edge, tree.ELID(to) + 1); return MX::op(x1, x2); } X prod_subtree(int u, int root = -1) { static_assert(subtree_query); int l = tree.LID[u], r = tree.RID[u]; if (root == -1) return bit_subtree.prod(l + edge, r); if (root == u) return bit_subtree.prod_all(); if (tree.in_subtree(u, root)) return bit_subtree.prod(l + edge, r); return MX::op(bit_subtree.prod(0, l + 1), bit_subtree.prod(r, N)); } }; #line 10 "main.cpp" void solve() { LL(N, Q, L); VEC(ll, A0, N); VEC(ll, X0, N); UnionFind uf(N + N + 1); vi A(N + 1), X(N + 1); auto I = argsort(A0); vc<int> R(N + N + 1); FOR(i, N + N + 1) R[i] = i; vc<int> toV(N); vc<int> par(N + 1, -1); for (auto& i: I) { int v = len(A); toV[i] = v; int a = R[uf[i]]; int b = R[uf[i + 1]]; par[a] = v, par[b] = v; A.eb(A0[i]); X.eb(X0[i]); par.eb(-1); uf.merge(v, a), uf.merge(v, b); R[uf[v]] = v; } ll n = N + N + 1; Graph<int, 1> G(n); FOR(v, n) { if (par[v] != -1) G.add(par[v], v); } G.build(); Tree<decltype(G)> tree(G, N + N); Lazy_Tree_Monoid<decltype(tree), ActedMonoid_Max_Add<ll>, true> TM(tree, [&](int eid) -> ll { auto& e = G.edges[eid]; return A[e.frm] + 1; }); Tree_AbelGroup<decltype(tree), Monoid_Add<ll>, true, 0, 1> TA(tree); FOR(v, N + 1, n) { TM.apply_path(v, n - 1, -X[v]); if (v < n - 1) TA.add(tree.v_to_e(v), X[v]); } FOR(Q) { INT(t); if (t == 1) { LL(a, b); --a; a = toV[a]; ll add = b - X[a]; TM.apply_path(a, n - 1, -add); X[a] += add; if (a < n - 1) TA.add(tree.v_to_e(a), add); } if (t == 2) { LL(v); auto w = TM.max_path([&](ll x) -> bool { return x <= L; }, v, n - 1); ll get = TA.prod_subtree(w) + X[w]; print(L + get); } } } signed main() { int T = 1; // INT(T); FOR(T) solve(); return 0; }