結果
問題 | No.2512 Mountain Sequences |
ユーザー | 👑 seekworser |
提出日時 | 2023-09-02 03:04:42 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,302 ms / 3,000 ms |
コード長 | 32,982 bytes |
コンパイル時間 | 3,673 ms |
コンパイル使用メモリ | 270,640 KB |
実行使用メモリ | 16,496 KB |
最終ジャッジ日時 | 2024-06-24 18:19:59 |
合計ジャッジ時間 | 25,423 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
14,848 KB |
testcase_01 | AC | 43 ms
14,856 KB |
testcase_02 | AC | 41 ms
14,848 KB |
testcase_03 | AC | 41 ms
14,848 KB |
testcase_04 | AC | 40 ms
14,848 KB |
testcase_05 | AC | 43 ms
14,812 KB |
testcase_06 | AC | 40 ms
14,848 KB |
testcase_07 | AC | 41 ms
14,848 KB |
testcase_08 | AC | 41 ms
14,876 KB |
testcase_09 | AC | 43 ms
14,720 KB |
testcase_10 | AC | 1,280 ms
16,484 KB |
testcase_11 | AC | 1,276 ms
16,392 KB |
testcase_12 | AC | 1,302 ms
16,420 KB |
testcase_13 | AC | 1,284 ms
16,424 KB |
testcase_14 | AC | 1,283 ms
16,488 KB |
testcase_15 | AC | 1,286 ms
16,480 KB |
testcase_16 | AC | 1,285 ms
16,488 KB |
testcase_17 | AC | 1,297 ms
16,468 KB |
testcase_18 | AC | 1,294 ms
16,496 KB |
testcase_19 | AC | 1,279 ms
16,476 KB |
testcase_20 | AC | 1,091 ms
16,356 KB |
testcase_21 | AC | 1,102 ms
16,484 KB |
testcase_22 | AC | 1,094 ms
16,484 KB |
testcase_23 | AC | 1,103 ms
16,484 KB |
testcase_24 | AC | 1,102 ms
16,480 KB |
testcase_25 | AC | 94 ms
16,484 KB |
testcase_26 | AC | 95 ms
16,460 KB |
testcase_27 | AC | 96 ms
16,468 KB |
testcase_28 | AC | 258 ms
16,312 KB |
ソースコード
// line 1 "answer.cpp" #if !__INCLUDE_LEVEL__ #include __FILE__ int main() { using mint = modint998244353; Combination<mint> c(powm(10, 6)); ll t; input(t); vector<tuple<ll,ll,ll>> lr(t); rep(i, t) { ll l,r; input(l,r); lr[i] = {l, r, i}; } sort(all(lr), [](tuple<ll,ll,ll> x, tuple<ll,ll,ll> y) { auto [xl, xr, idx] = x; auto [yl, yr, idy] = y; ll bsize = 3000; ll bx = xl / bsize; ll by = yl / bsize; if (bx != by) return bx < by; if (bx % 2 == 0) return xr < yr; return xr > yr; }); ll posl = 1; ll posr = 1; mint ans = 1; mint inv2 = mint(2).inv(); auto addl = [&] () -> void { ans = c(2*posr, posl+1) - 2*ans; }; auto dell = [&] () -> void { ans = (c(2*posr, posl+1) - ans) * inv2; }; auto addr = [&] () -> void { ans = ans + c(2*posr - 2, posl-1);}; auto delr = [&] () -> void { ans = ans - c(2*posr - 2, posl-1);}; vector<mint> a(t); rep(i, t) { auto [l, r, id] = lr[i]; debug(l, r); while (l < posl) { posl--; addl(); // debug(posl, posr, ans); } while (l > posl) { dell(); posl++; // debug(posl, posr, ans); } while (r < posr) { delr(); posr--; // debug(posl, posr, ans); } while (r > posr) { posr++; addr(); // debug(posl, posr, ans); } a[id] = ans; } repe(e, a) print(e); } #else // line 2 "/home/seekworser/.cpp_lib/competitive_library/competitive/std/std.hpp" #include <bits/stdc++.h> #ifndef LOCAL_TEST #pragma GCC target ("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #endif // LOCAL_TEST using namespace std; // shorten typenames using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; using vs = vector<string>; using vvs = vector<vector<string>>; using vvvs = vector<vector<vector<string>>>; template<typename T> vector<vector<T>> vv(int h, int w, T val = T()) { return vector(h, vector<T>(w, val)); } template<typename T> vector<vector<vector<T>>> vvv(int h1, int h2, int h3, T val = T()) { return vector(h1, vector(h2, vector<T>(h3, val))); } template<typename T> vector<vector<vector<vector<T>>>> vvvv(int h1, int h2, int h3, int h4, T val = T()) { return vector(h1, vector(h2, vector(h3, vector<T>(h4, val)))); } template <class T> using priority_queue_min = priority_queue<T, vector<T>, greater<T>>; // define CONSTANTS constexpr double PI = 3.14159265358979323; constexpr int INF = 100100111; constexpr ll INFL = 3300300300300300491LL; float EPS = 1e-8; double EPSL = 1e-10; template<typename T> bool eq(const T x, const T y) { return x == y; } template<> bool eq<double>(const double x, const double y) { return (abs(x - y) < EPSL * x || abs(x - y) < EPSL); } template<> bool eq<float>(const float x, const float y) { return abs(x - y) < EPS * x; } template<typename T> bool neq(const T x, const T y) { return !(eq<T>(x, y)); } template<typename T> bool ge(const T x, const T y) { return (eq<T>(x, y) || (x > y)); } template<typename T> bool le(const T x, const T y) { return (eq<T>(x, y) || (x < y)); } template<typename T> bool gt(const T x, const T y) { return !(le<T>(x, y)); } template<typename T> bool lt(const T x, const T y) { return !(ge<T>(x, y)); } constexpr int MODINT998244353 = 998244353; constexpr int MODINT1000000007 = 1000000007; // fasten io struct Nyan { Nyan() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } nyan; // define macros #define all(a) (a).begin(), (a).end() #define sz(x) ((ll)(x).size()) #define rep1(n) for(ll dummy_iter = 0LL; dummy_iter < n; ++dummy_iter) // 0,1,...,n-1 #define rep2(i, n) for(ll i = 0LL, i##_counter = 0LL; i##_counter < ll(n); ++(i##_counter), (i) = i##_counter) // i=0,1,...,n-1 #define rep3(i, s, t) for(ll i = ll(s), i##_counter = ll(s); i##_counter < ll(t); ++(i##_counter), (i) = (i##_counter)) // i=s,s+1,...,t-1 #define rep4(i, s, t, step) for(ll i##_counter = step > 0 ? ll(s) : -ll(s), i##_end = step > 0 ? ll(t) : -ll(t), i##_step = abs(step), i = ll(s); i##_counter < i##_end; i##_counter += i##_step, i = step > 0 ? i##_counter : -i##_counter) // i=s,s+step,...,<t #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define repe(a, v) for(auto& a : (v)) // iterate over all elements in v #define smod(n, m) ((((n) % (m)) + (m)) % (m)) #define sdiv(n, m) (((n) - smod(n, m)) / (m)) #define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} int Yes(bool b=true) { cout << (b ? "Yes\n" : "No\n"); return 0; }; int YES(bool b=true) { cout << (b ? "YES\n" : "NO\n"); return 0; }; int No(bool b=true) {return Yes(!b);}; int NO(bool b=true) {return YES(!b);}; template<typename T, size_t N> T max(array<T, N>& a) { return *max_element(all(a)); }; template<typename T, size_t N> T min(array<T, N>& a) { return *min_element(all(a)); }; template<typename T> T max(vector<T>& a) { return *max_element(all(a)); }; template<typename T> T min(vector<T>& a) { return *min_element(all(a)); }; template<typename T> vector<T> vec_slice(const vector<T>& a, int l, int r) { vector<T> rev; rep(i, l, r) rev.push_back(a[i]); return rev; }; template<typename T> T sum(vector<T>& a, T zero = T(0)) { T rev = zero; rep(i, sz(a)) rev += a[i]; return rev; }; template<typename T> bool in_range(const T& val, const T& s, const T& t) { return s <= val && val < t; }; template <class T> inline vector<T>& operator--(vector<T>& v) { repe(x, v) --x; return v; } template <class T> inline vector<T>& operator++(vector<T>& v) { repe(x, v) ++x; return v; } ll powm(ll a, ll n, ll mod=INFL) { ll res = 1; while (n > 0) { if (n & 1) res = (res * a) % mod; if (n > 1) a = (a * a) % mod; n >>= 1; } return res; } ll sqrtll(ll x) { assert(x >= 0); ll rev = sqrt(x); while(rev * rev > x) --rev; while((rev+1) * (rev+1)<=x) ++rev; return rev; } template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } int digit(ll x, int d=10) { int rev=0; while (x > 0) { rev++; x /= d;}; return rev; } /** * @brief std.hpp * @docs docs/std/std.md */ // line 4 "/home/seekworser/.cpp_lib/competitive_library/atcoder/modint.hpp" #include <type_traits> #ifdef _MSC_VER #include <intrin.h> #endif // line 3 "/home/seekworser/.cpp_lib/competitive_library/atcoder/internal_math.hpp" #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { // @param m `1 <= m` // @return x mod m constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } // Fast modular multiplication by barrett reduction // Reference: https://en.wikipedia.org/wiki/Barrett_reduction // NOTE: reconsider after Ice Lake struct barrett { unsigned int _m; unsigned long long im; // @param m `1 <= m < 2^31` explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {} // @return m unsigned int umod() const { return _m; } // @param a `0 <= a < m` // @param b `0 <= b < m` // @return `a * b % m` unsigned int mul(unsigned int a, unsigned int b) const { // [1] m = 1 // a = b = im = 0, so okay // [2] m >= 2 // im = ceil(2^64 / m) // -> im * m = 2^64 + r (0 <= r < m) // let z = a*b = c*m + d (0 <= c, d < m) // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2 // ((ab * im) >> 64) == c or c + 1 unsigned long long z = a; z *= b; #ifdef _MSC_VER unsigned long long x; _umul128(z, im, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64); #endif unsigned int v = (unsigned int)(z - x * _m); if (_m <= v) v += _m; return v; } }; // @param n `0 <= n` // @param m `1 <= m` // @return `(x ** n) % m` constexpr long long pow_mod_constexpr(long long x, long long n, int m) { if (m == 1) return 0; unsigned int _m = (unsigned int)(m); unsigned long long r = 1; unsigned long long y = safe_mod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } // Reference: // M. Forisek and J. Jancina, // Fast Primality Testing for Integers That Fit into a Machine Word // @param n `0 <= n` constexpr bool is_prime_constexpr(int n) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long long d = n - 1; while (d % 2 == 0) d /= 2; constexpr long long bases[3] = {2, 7, 61}; for (long long a : bases) { long long t = d; long long y = pow_mod_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } template <int n> constexpr bool is_prime = is_prime_constexpr(n); // @param b `1 <= b` // @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; // Contracts: // [1] s - m0 * a = 0 (mod b) // [2] t - m1 * a = 0 (mod b) // [3] s * |m1| + t * |m0| <= b long long s = b, t = a; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b // [3]: // (s - t * u) * |m1| + t * |m0 - m1 * u| // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u) // = s * |m1| + t * |m0| <= b auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } // by [3]: |m0| <= b/g // by g != b: |m0| < b/g if (m0 < 0) m0 += b / s; return {s, m0}; } // Compile time primitive root // @param m must be prime // @return primitive root (and minimum in now) constexpr int primitive_root_constexpr(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int divs[20] = {}; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long long)(i)*i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template <int m> constexpr int primitive_root = primitive_root_constexpr(m); // @param n `n < 2^32` // @param m `1 <= m < 2^32` // @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64) unsigned long long floor_sum_unsigned(unsigned long long n, unsigned long long m, unsigned long long a, unsigned long long b) { unsigned long long ans = 0; while (true) { if (a >= m) { ans += n * (n - 1) / 2 * (a / m); a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } unsigned long long y_max = a * n + b; if (y_max < m) break; // y_max < m * (n + 1) // floor(y_max / m) <= n n = (unsigned long long)(y_max / m); b = (unsigned long long)(y_max % m); std::swap(m, a); } return ans; } } // namespace internal } // namespace atcoder // line 5 "/home/seekworser/.cpp_lib/competitive_library/atcoder/internal_type_traits.hpp" namespace atcoder { namespace internal { #ifndef _MSC_VER template <class T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value || std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <class T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <class T> using is_integral = typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_signed_int = typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) || is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional< is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <class T> using is_integral = typename std::is_integral<T>; template <class T> using is_signed_int = typename std::conditional<is_integral<T>::value && std::is_signed<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<is_integral<T>::value && std::is_unsigned<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; #endif template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>; template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>; template <class T> using to_unsigned_t = typename to_unsigned<T>::type; } // namespace internal } // namespace atcoder // line 12 "/home/seekworser/.cpp_lib/competitive_library/atcoder/modint.hpp" namespace atcoder { namespace internal { struct modint_base {}; struct static_modint_base : modint_base {}; template <class T> using is_modint = std::is_base_of<modint_base, T>; template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>; } // namespace internal template <int m, std::enable_if_t<(1 <= m)>* = nullptr> struct static_modint : internal::static_modint_base { using mint = static_modint; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T>* = nullptr> static_modint(T v) { long long x = (long long)(v % (long long)(umod())); if (x < 0) x += umod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T>* = nullptr> static_modint(T v) { _v = (unsigned int)(v % umod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } mint& operator*=(const mint& rhs) { unsigned long long z = _v; z *= rhs._v; _v = (unsigned int)(z % umod()); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint& operator=(const mint& rhs) { (*this)._v = rhs.val(); return *this; } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { if (prime) { assert(_v); return pow(umod() - 2); } else { auto eg = internal::inv_gcd(_v, m); assert(eg.first == 1); return eg.second; } } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static constexpr unsigned int umod() { return m; } static constexpr bool prime = internal::is_prime<m>; }; template <int id> struct dynamic_modint : internal::modint_base { using mint = dynamic_modint; public: static int mod() { return (int)(bt.umod()); } static void set_mod(int m) { assert(1 <= m); bt = internal::barrett(m); } static mint raw(int v) { mint x; x._v = v; return x; } dynamic_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T>* = nullptr> dynamic_modint(T v) { long long x = (long long)(v % (long long)(mod())); if (x < 0) x += mod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T>* = nullptr> dynamic_modint(T v) { _v = (unsigned int)(v % mod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v += mod() - rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator*=(const mint& rhs) { _v = bt.mul(_v, rhs._v); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint& operator=(const mint& rhs) { (*this)._v = rhs.val(); return *this; } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { auto eg = internal::inv_gcd(_v, mod()); assert(eg.first == 1); return eg.second; } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static internal::barrett bt; static unsigned int umod() { return bt.umod(); } }; template <int id> internal::barrett dynamic_modint<id>::bt(998244353); using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; using modint = dynamic_modint<-1>; namespace internal { template <class T> using is_static_modint = std::is_base_of<internal::static_modint_base, T>; template <class T> using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>; template <class> struct is_dynamic_modint : public std::false_type {}; template <int id> struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {}; template <class T> using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>; } // namespace internal } // namespace atcoder // line 4 "/home/seekworser/.cpp_lib/competitive_library/competitive/math/modint.hpp" namespace modint_internal { template<typename Mint> Mint pow(Mint a, ll n) { Mint res = 1; while (n > 0) { if (n & 1) res *= a; if (n > 1) a *= a; n >>= 1; } return res; } template<typename Mint> inline istream& input(istream& is, Mint& x) {ll a; is >> a; x = a; return is; } template<typename Mint> inline ostream& print(ostream& os, const Mint& x) { os << x.val(); return os; } } inline istream& operator>>(istream& is, atcoder::modint& x) { return modint_internal::input(is, x); } template<int m> inline istream& operator>>(istream& is, atcoder::static_modint<m>& x) { return modint_internal::input(is, x); } inline ostream& operator<<(ostream& os, const atcoder::modint& x) { return modint_internal::print(os, x); } template<int m> inline ostream& operator<<(ostream& os, const atcoder::static_modint<m>& x) { return modint_internal::print(os, x); } atcoder::modint pow(atcoder::modint a, ll n) { return modint_internal::pow(a, n); } template<int m> atcoder::static_modint<m> pow(atcoder::static_modint<m> a, ll n) { return modint_internal::pow(a, n); } using modint998244353 = atcoder::modint998244353; using modint1000000007 = atcoder::modint1000000007; using modint = atcoder::modint; /** * @brief modint.hpp * @docs docs/math/modint.md */ // line 4 "/home/seekworser/.cpp_lib/competitive_library/competitive/math/combination.hpp" template<typename mint> struct Combination { vector<mint> fact, fact_inv; Combination(int nmax) : fact(nmax+1), fact_inv(nmax+1) { int p = mint::mod(); vector<mint> inv(nmax+1); fact[0] = fact[1] = 1; fact_inv[0] = fact_inv[1] = 1; inv[0] = 0; inv[1] = 1; for (int i = 2; i < nmax+1; i++) { fact[i] = fact[i - 1] * i; inv[i] = p - inv[p % i] * (p / i); fact_inv[i] = fact_inv[i - 1] * inv[i]; } } mint operator()(int n, int r) { if (r < 0 || n < r) return 0; return fact[n] * fact_inv[r] * fact_inv[n - r]; } }; /** * @brief combination.hpp * @docs docs/math/combination.md */ // line 3 "/home/seekworser/.cpp_lib/competitive_library/competitive/algorithm/mo.hpp" template<typename Rev_t> struct Mo { int range_min, range_max; using query_t = tuple<int, int, int>; vector<query_t> query; Mo() {}; Mo(const vector<pii>& lr) : range_min(INF), range_max(-INF) { rep(i, sz(lr)) { assert(lr[i].first <= lr[i].second); query.emplace_back(lr[i].first, lr[i].second, i); chmin((*this).range_min, lr[i].first); chmax((*this).range_max, lr[i].second); } } Mo(const vi& l, const vi& r) : range_min(INF), range_max(-INF) { assert(sz(l) == sz(r)); rep(i, sz(l)) { assert(l[i] <= r[i]); query.emplace_back(l[i], r[i], i); chmin((*this).range_min, l[i]); chmax((*this).range_max, r[i]); } } void add_query(int l, int r) { (*this).query.emplace_back(l, r, sz((*this).query)); } template<typename AL, typename AR, typename DL, typename DR, typename GET> vector<Rev_t> answer( const AL& add_left, const AR& add_right, const DL& delete_left, const DR& delete_right, const GET& get, int init_l = 0, int init_r = 0 ) { assert(range_max >= range_min); int q = sz(query); int range = range_max - range_min; int width = max<int>(1, 1.0 * range / max<double>(1.0, sqrt(q * 2.0 / 3.0))); sort(all((*this).query), [&](query_t a, query_t b) { auto [la, ra, ida] = a; auto [lb, rb, idb] = b; int ablock = la / width, bblock = lb / width; if (ablock != bblock) return ablock < bblock; if (ablock & 1) return ra < rb; return ra > rb; }); vector<Rev_t> rev(q); int cl(init_l), cr(init_r); rep(i, q) { auto [l, r, id] = query[i]; while (cl > l) { cl--; add_left(cl); } while (cr < r) { add_right(cr); cr++;} while (cl < l) { delete_left(cl); cl++; } while (cr > r) { cr--; delete_right(cr); } rev[id] = get(); } return rev; }; template<typename A, typename D, typename GET> vector<Rev_t> answer( const A& add, const D& del, const GET& get, int init_l = 0, int init_r = 0 ) { return (*this).answer(add, add, del, del, get, init_l, init_r); } }; /** * @brief mo.hpp * @docs docs/algorithm/mo.md */ // line 3 "/home/seekworser/.cpp_lib/competitive_library/competitive/std/io.hpp" // overload operators (prototypes) template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p); template <class T> inline istream& operator>>(istream& is, vector<T>& v); template <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p); template <class T> inline ostream& operator<<(ostream& os, const vector<T>& v); template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp); template <typename T> ostream &operator<<(ostream &os, const set<T> &st); template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st); template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st); template <typename T> ostream &operator<<(ostream &os, queue<T> q); template <typename T> ostream &operator<<(ostream &os, deque<T> q); template <typename T> ostream &operator<<(ostream &os, stack<T> st); template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq); // overload operators template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repe(x, v) is >> x; return is; } template <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p) { os << p.first << " " << p.second; return os; } template <class T> inline ostream& operator<<(ostream& os, const vector<T>& v) { rep(i, sz(v)) { os << v.at(i); if (i != sz(v) - 1) os << " "; } return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp) { for (auto &[key, val] : mp) { os << key << ":" << val << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st) { ll cnt = 0; for (auto &e : st) { os << e << (++cnt != (int)st.size() ? " " : ""); } return os; } template <typename T> ostream &operator<<(ostream &os, queue<T> q) { while (q.size()) { os << q.front() << " "; q.pop(); } return os; } template <typename T> ostream &operator<<(ostream &os, deque<T> q) { while (q.size()) { os << q.front() << " "; q.pop_front(); } return os; } template <typename T> ostream &operator<<(ostream &os, stack<T> st) { while (st.size()) { os << st.top() << " "; st.pop(); } return os; } template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq) { while (pq.size()) { os << pq.top() << " "; pq.pop(); } return os; } template <typename T> int print_sep_end(string sep, string end, const T& val) { (void)sep; cout << val << end; return 0; }; template <typename T1, typename... T2> int print_sep_end(string sep, string end, const T1 &val, const T2 &...remain) { cout << val << sep; print_sep_end(sep, end, remain...); return 0; }; template <typename... T> int print(const T &...args) { print_sep_end(" ", "\n", args...); return 0; }; template <typename... T> void flush() { cout << flush; }; template <typename... T> int print_and_flush(const T &...args) { print(args...); flush(); return 0; }; #define debug(...) debug_func(0, #__VA_ARGS__, __VA_ARGS__) // debug print template <typename T> void input(T &a) { cin >> a; }; template <typename T1, typename... T2> void input(T1&a, T2 &...b) { cin >> a; input(b...); }; #ifdef LOCAL_TEST template <typename T> void debug_func(int i, const T name) { (void)i; (void)name; cerr << endl; } template <typename T1, typename T2, typename... T3> void debug_func(int i, const T1 &name, const T2 &a, const T3 &...b) { int scope = 0; for ( ; (scope != 0 || name[i] != ',') && name[i] != '\0'; i++ ) { cerr << name[i]; if (name[i] == '(' || name[i] == '{') scope++; if (name[i] == ')' || name[i] == '}') scope--; } cerr << ":" << a << " "; debug_func(i + 1, name, b...); } template <typename T1, typename T2, typename... T3> void debug_func(int i, const T1 &name, T2 &a, T3 &...b) { int scope = 0; for ( ; (scope != 0 || name[i] != ',') && name[i] != '\0'; i++ ) { cerr << name[i]; if (name[i] == '(' || name[i] == '{') scope++; if (name[i] == ')' || name[i] == '}') scope--; } cerr << ":" << a << " "; debug_func(i + 1, name, b...); } #endif #ifndef LOCAL_TEST template <typename... T> void debug_func(T &...) {} template <typename... T> void debug_func(const T &...) {} #endif /** * @brief io.hpp * @docs docs/std/io.md */ // line 65 "answer.cpp" #endif