結果

問題 No.2327 Inversion Sum
ユーザー keisuke6keisuke6
提出日時 2023-05-28 15:58:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,825 bytes
コンパイル時間 2,601 ms
コンパイル使用メモリ 211,252 KB
実行使用メモリ 9,880 KB
最終ジャッジ日時 2023-08-27 13:32:16
合計ジャッジ時間 7,300 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
9,880 KB
testcase_01 AC 66 ms
7,436 KB
testcase_02 AC 48 ms
6,940 KB
testcase_03 AC 11 ms
5,072 KB
testcase_04 AC 76 ms
7,468 KB
testcase_05 AC 11 ms
4,528 KB
testcase_06 AC 49 ms
7,004 KB
testcase_07 AC 26 ms
4,860 KB
testcase_08 AC 10 ms
4,380 KB
testcase_09 AC 66 ms
7,172 KB
testcase_10 AC 17 ms
4,380 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 41 ms
5,528 KB
testcase_15 AC 71 ms
7,304 KB
testcase_16 AC 26 ms
5,780 KB
testcase_17 AC 7 ms
4,536 KB
testcase_18 AC 9 ms
4,380 KB
testcase_19 AC 20 ms
5,668 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
int mergeAndCount(vector<int>& arr, int left, int mid, int right) {
    int count = 0;

    // 左側の配列と右側の配列を一時的な配列にマージする
    vector<int> temp(right - left + 1);
    int i = left;    // 左側の配列の開始インデックス
    int j = mid + 1; // 右側の配列の開始インデックス
    int k = 0;       // 一時的な配列のインデックス

    while (i <= mid && j <= right) {
        if (arr[i] <= arr[j]) {
            temp[k] = arr[i];
            i++;
        } else {
            temp[k] = arr[j];
            j++;
            count += (mid - i + 1);  // 転倒数の計算
        }
        k++;
    }

    // 残った要素を一時的な配列に追加する
    while (i <= mid) {
        temp[k] = arr[i];
        i++;
        k++;
    }

    while (j <= right) {
        temp[k] = arr[j];
        j++;
        k++;
    }

    // 一時的な配列を元の配列に戻す
    for (int x = left; x <= right; x++) {
        arr[x] = temp[x - left];
    }

    return count;
}

int mergeSortAndCount(vector<int>& arr, int left, int right) {
    int count = 0;

    if (left < right) {
        int mid = left + (right - left) / 2;

        // 左側の配列と右側の配列を再帰的にソートする
        count += mergeSortAndCount(arr, left, mid);
        count += mergeSortAndCount(arr, mid + 1, right);

        // ソートされた配列をマージする
        count += mergeAndCount(arr, left, mid, right);
    }

    return count;
}

int countInversions(vector<int>& arr) {
    int n = arr.size();
    int inversions = mergeSortAndCount(arr, 0, n - 1);
    return inversions;
}
signed main(){
  int N,M;
  cin>>N>>M;
  set<int> s;
  vector<int> A(N,-1);
  for(int i=0;i<M;i++){
  	int p,k;
  	cin>>p>>k;
  	p--;
  	k--;
  	A[k] = p;
  	s.insert(p);
  }
  vector<int> S = {}, T = {};
  for(int i=0;i<N;i++){
  	if(!s.count(i)) T.push_back(i);
  	if(A[i] != -1) S.push_back(A[i]);
  }
  int mod = 998244353;
  int a = 1, b = (T.size())*(T.size()-1)/2%mod, c = 0;
  for(int i=0;i<T.size();i++){
  	a *= (i+1);
  	a %= mod;
  }
  a = a*countInversions(S)%mod;
  for(int i=3;i<=T.size();i++){
  	b *= i;
  	b %= mod;
  }
  int x = 1, count = 0;
  for(int i=0;i<T.size()-1;i++){
  	x *= (i+1);
  	x %= mod;
  }
  int n = T.size();
  sort(T.begin(),T.end());
  vector<int> TT(n);
  for(int i=0;i<n;i++) TT[i] = T[i];
  if(S.size() > 0 && T.size() > 0){
	for(int i=0;i<N;i++){
		if(A[i] == -1){
			count++;
			continue;
		}
		auto indd = lower_bound(TT.begin(),TT.end(),A[i]);
		auto in = TT.begin();
		int ind = distance(in,indd);
		c += (count*(n-ind)+(n-count)*ind)%mod*x%mod;
		c %= mod;
	}
  }
  //cout<<a<<' '<<b<<' '<<c<<endl;
  cout<<(a+b+c+10*mod)%mod<<endl;
}
0