結果
| 問題 |
No.2327 Inversion Sum
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-05-28 15:56:06 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 30 ms / 2,000 ms |
| コード長 | 3,107 bytes |
| コンパイル時間 | 4,553 ms |
| コンパイル使用メモリ | 234,292 KB |
| 実行使用メモリ | 8,584 KB |
| 最終ジャッジ日時 | 2024-12-27 09:50:32 |
| 合計ジャッジ時間 | 5,375 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
// type
typedef long long ll;
typedef long double ld;
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template<class T> using v = vector<T>;
#define pl pair<ll, ll>
#define vl v<ll>
#define vp v<pl>
#define vm v<mint>
// IN-OUT
#define NYAN ios::sync_with_stdio(false);cin.tie(nullptr);cout<<fixed<<setprecision(15);
void Yes(bool b=1) { cout << ( b == 1 ? "Yes" : "No" ) << "\n"; }
void YES(bool b=1) { cout << ( b == 1 ? "YES" : "NO" ) << "\n"; }
void No(bool b=1) { cout << ( b == 1 ? "No" : "Yes" ) << "\n"; }
void NO(bool b=1) { cout << ( b == 1 ? "NO" : "YES" ) << "\n"; }
void CIN() {}
template <typename T, class... U> void CIN(T &t, U &...u) { cin >> t; CIN(u...); }
void COUT() { cout << "\n"; }
template <typename T, class... U, char sep = ' '> void COUT(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; COUT(u...); }
#define dump(x) cerr << #x << ":"<< x << "\n";
#define vdump(x) rep(repeat, sz(x)) cerr << repeat << ":" << x[repeat] << "\n";
// macro
#define bp __builtin_popcountll
#define ALL(x) x.begin(),x.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define reps(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define sz(x) (ll)x.size()
ll xd[]={0, 1, 0, -1, 1, 1, -1, -1};
ll yd[]={1, 0, -1, 0, 1, -1, -1, 1};
// function
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll rmax(ll a, ll b){return max(a, b);}
ll rmin(ll a, ll b){return min(a, b);}
ll rsum(ll a, ll b){return a + b;}
ll rzero(){return 0;}
// constant
long double PI = 3.14159265358979;
#define INF32 2147483647
#define INF64 9223372036854775807
#define INF 922337203685477580
using mint = modint998244353;
// using mint = modint1000000007;
/* SOLVE BEGIN ************************************************************************** */
void solve()
{
ll n, m;
cin >> n >> m;
vl used(n), a(n);
rep(i, m){
ll x, k;
cin >> x >> k;
a[k - 1] = x;
used[x - 1] = 1;
}
vl vec, vec2;
rep(i, n) if(used[i] == 0) vec.emplace_back(i + 1);
mint ans = 0, base = 1;
reps(i, 1, sz(vec)) base *= i;
ll big = 0, small = sz(vec);
rep(i, n){
if(a[i] == 0){
big++;
small--;
}else{
auto it = lower_bound(ALL(vec), a[i]);
ll x = vec.end() - it;
ans += x * big * base + (sz(vec) - x) * small * base;
vec2.emplace_back(a[i]);
}
}
mint add = 0;
segtree<ll, rsum, rzero> sg(n + 1);
rep(i, sz(vec2)){
auto x = vec2[i];
add += sg.prod(x, n + 1);
sg.set(x, 1);
}
reps(i, 1, sz(vec) + 1) add *= i;
ans += add;
if(sz(vec)){
vm dp(sz(vec));
mint now = 1;
dp[0] = 0;
reps(i, 1, sz(vec)){
now *= i;
dp[i] = now * ((1 + i) * i / 2) + dp[i - 1] * (i + 1);
}
ans += dp[sz(vec) - 1];
}
cout << ans.val() << "\n";
}
int main()
{
NYAN
solve();
return 0;
}