結果
問題 | No.2327 Inversion Sum |
ユーザー | t98slider |
提出日時 | 2023-05-28 16:37:17 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 23 ms / 2,000 ms |
コード長 | 5,239 bytes |
コンパイル時間 | 1,525 ms |
コンパイル使用メモリ | 173,536 KB |
実行使用メモリ | 6,784 KB |
最終ジャッジ日時 | 2024-06-08 09:40:03 |
合計ジャッジ時間 | 2,714 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 7 ms
6,784 KB |
testcase_01 | AC | 20 ms
6,784 KB |
testcase_02 | AC | 15 ms
6,784 KB |
testcase_03 | AC | 7 ms
6,272 KB |
testcase_04 | AC | 22 ms
6,528 KB |
testcase_05 | AC | 7 ms
5,632 KB |
testcase_06 | AC | 16 ms
6,656 KB |
testcase_07 | AC | 11 ms
5,376 KB |
testcase_08 | AC | 6 ms
5,376 KB |
testcase_09 | AC | 20 ms
6,400 KB |
testcase_10 | AC | 9 ms
5,376 KB |
testcase_11 | AC | 5 ms
5,760 KB |
testcase_12 | AC | 5 ms
5,376 KB |
testcase_13 | AC | 4 ms
5,376 KB |
testcase_14 | AC | 16 ms
5,632 KB |
testcase_15 | AC | 23 ms
6,400 KB |
testcase_16 | AC | 10 ms
6,528 KB |
testcase_17 | AC | 5 ms
6,144 KB |
testcase_18 | AC | 6 ms
5,376 KB |
testcase_19 | AC | 9 ms
6,400 KB |
testcase_20 | AC | 4 ms
5,376 KB |
testcase_21 | AC | 4 ms
5,376 KB |
testcase_22 | AC | 4 ms
5,376 KB |
testcase_23 | AC | 4 ms
5,376 KB |
testcase_24 | AC | 4 ms
5,376 KB |
testcase_25 | AC | 4 ms
5,376 KB |
testcase_26 | AC | 4 ms
5,376 KB |
testcase_27 | AC | 4 ms
5,376 KB |
testcase_28 | AC | 4 ms
5,376 KB |
testcase_29 | AC | 4 ms
5,376 KB |
testcase_30 | AC | 4 ms
5,376 KB |
testcase_31 | AC | 3 ms
5,376 KB |
testcase_32 | AC | 4 ms
5,376 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; template<const unsigned int MOD> struct prime_modint { using mint = prime_modint; unsigned int v; prime_modint() : v(0) {} prime_modint(unsigned int a) { a %= MOD; v = a; } prime_modint(unsigned long long a) { a %= MOD; v = a; } prime_modint(int a) { a %= (int)(MOD); if(a < 0)a += MOD; v = a; } prime_modint(long long a) { a %= (int)(MOD); if(a < 0)a += MOD; v = a; } static constexpr int mod() { return MOD; } mint& operator++() {v++; if(v == MOD)v = 0; return *this;} mint& operator--() {if(v == 0)v = MOD; 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 >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& rhs) { if(v < rhs.v) v += MOD; v -= rhs.v; return *this; } mint& operator*=(const mint& rhs) { v = (unsigned int)((unsigned long long)(v) * rhs.v % MOD); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint r = 1, x = *this; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { assert(v); return pow(MOD - 2); } 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); } friend std::ostream& operator << (std::ostream &os, const mint& rhs) noexcept { return os << rhs.v; } }; //using mint = prime_modint<1000000007>; using mint = prime_modint<998244353>; template <class T> struct fenwick_tree { using U = T; public: fenwick_tree() : _n(0) {} fenwick_tree(int n) : _n(n), data(n) {} void add(int p, T x) { assert(0 <= p && p < _n); p++; while (p <= _n) { data[p - 1] += U(x); p += p & -p; } } T sum(int l, int r) { assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } private: int _n; std::vector<U> data; U sum(int r) { U s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } }; //enumeration<mint> enu(200000);のように宣言する template<class T> struct enumeration{ int N; vector<T> fact, inv; enumeration() : N(0), fact(1, 1), inv(1, 1) {} enumeration(int _n) : N(_n), fact(_n + 1), inv(_n + 1) { fact[0] = 1; for(int i = 1; i <= N; i++) fact[i] = fact[i - 1] * i; inv[N] = T(1) / fact[N]; for(int i = N; i >= 1; i--) inv[i - 1] = inv[i] * i; } void expand(int lim){ fact.resize(lim + 1); inv.resize(lim + 1); for(int i = N + 1; i <= lim; i++) fact[i] = i * fact[i - 1]; inv[lim] = T(1) / fact[lim]; for(int i = lim; i >= N + 2; i--) inv[i - 1] = i * inv[i]; N = lim; } T Per(int n, int k){ if(k > n) return 0; if(n > N) expand(n); return fact[n] * inv[n - k]; } T C(int n, int k){ if(n < 0 || k < 0 || k > n) return 0; if(n > N) expand(n); return fact[n] * inv[n - k] * inv[k]; } T H(int n, int k){ if(n ==0 && k == 0) return 1; if(n <= 0 || k < 0) return 0; return C(n + k - 1, k); } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<int> a(n, -1); vector<int> s(n + 1), s2(n + 1, 1); s2[0] = 0; enumeration<mint> enu(200000); fenwick_tree<ll> fw(n); for(int i = 0; i < m; i++){ int p, v; cin >> p >> v; a[--v] = --p; } ll ten = 0; ll k = n - m; for(int i = 0; i < n; i++){ s[i + 1] = s[i]; if(a[i] != -1){ ten += fw.sum(a[i] + 1, n); fw.add(a[i], 1); //s[i + 1]++; s2[a[i] + 1]--; }else{ s[i + 1]++; } } mint ans, coef = enu.fact[k]; for(int i = 0; i < n; i++) s2[i + 1] += s2[i]; //for(int i = 0; i <= n; i++){ //cerr << s2[i] << " "; //} //cerr << '\n'; ans = (ten + mint(k) * (k - 1) / 4) * coef; //cerr << ans << '\n'; for(int i = 0; i < n; i++){ if(a[i] != -1){ ll fr = s[i], ba = s[n] - s[i + 1]; //cerr << fr << " " << ba << '\n'; ll v1 = s2[a[i]], v2 = s2[n] - s2[a[i] + 1]; if(k >= 1) ans += mint(fr * v2 + ba * v1) / k * coef; } } cout << ans << '\n'; }