結果

問題 No.2327 Inversion Sum
ユーザー m_99m_99
提出日時 2023-05-28 14:02:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 1,315 bytes
コンパイル時間 5,941 ms
コンパイル使用メモリ 269,276 KB
実行使用メモリ 4,464 KB
最終ジャッジ日時 2023-08-27 08:52:04
合計ジャッジ時間 6,260 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
4,384 KB
testcase_01 AC 58 ms
4,380 KB
testcase_02 AC 42 ms
4,380 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 68 ms
4,380 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 44 ms
4,380 KB
testcase_07 AC 28 ms
4,380 KB
testcase_08 AC 10 ms
4,380 KB
testcase_09 AC 59 ms
4,452 KB
testcase_10 AC 18 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 40 ms
4,380 KB
testcase_15 AC 67 ms
4,464 KB
testcase_16 AC 21 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 9 ms
4,380 KB
testcase_19 AC 15 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,384 KB
testcase_30 AC 2 ms
4,384 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001


int main(){
	
	int N,M;
	cin>>N>>M;
	
	vector<pair<int,int>> t(M);
	rep(i,M){
		cin>>t[i].second>>t[i].first;

	}
	sort(t.begin(),t.end());
	fenwick_tree<mint> F(N+1);
	mint ans = 0;
	rep(i,M){
		ans += F.sum(t[i].second,N+1);
		F.add(t[i].second,1);
	}
	
	if(N!=M){
		vector<int> vs;
		rep(i,M)vs.push_back(t[i].second);
		sort(vs.begin(),vs.end());
		rep(i,M){
			mint tt = distance(upper_bound(vs.begin(),vs.end(),t[i].second),vs.end());
			tt = (N-t[i].second) - tt;
			tt /= N-M;
			tt *= t[i].first-1-i;
			ans += tt;
		}
		
		reverse(t.begin(),t.end());
		rep(i,t.size()){
			t[i].first = N+1-t[i].first;
			t[i].second = N+1-t[i].second;
		}
		rep(i,vs.size()){
			vs[i] = N+1-vs[i];
		}
		sort(vs.begin(),vs.end());
		rep(i,M){
			mint tt = distance(upper_bound(vs.begin(),vs.end(),t[i].second),vs.end());
			tt = (N-t[i].second) - tt;
			tt /= N-M;
			tt *= t[i].first-1-i;
			ans += tt;
		}
		
		{
			mint cur = N-M;
			cur *= N-M-1;
			cur /= 2;
			cur /= 2;
			ans += cur;
		}
	}
	rep(i,N-M){
		ans *= i+1;
	}
	
	cout<<ans.val()<<endl;
	
	return 0;
}
0