結果

問題 No.1300 Sum of Inversions
ユーザー kaede2020kaede2020
提出日時 2020-11-28 14:45:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 2,680 bytes
コンパイル時間 2,052 ms
コンパイル使用メモリ 175,748 KB
実行使用メモリ 11,888 KB
最終ジャッジ日時 2023-10-10 00:01:32
合計ジャッジ時間 7,719 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 128 ms
9,636 KB
testcase_04 AC 126 ms
9,428 KB
testcase_05 AC 102 ms
8,508 KB
testcase_06 AC 147 ms
10,392 KB
testcase_07 AC 137 ms
10,308 KB
testcase_08 AC 152 ms
10,892 KB
testcase_09 AC 153 ms
10,848 KB
testcase_10 AC 82 ms
7,500 KB
testcase_11 AC 85 ms
7,480 KB
testcase_12 AC 124 ms
9,624 KB
testcase_13 AC 123 ms
9,488 KB
testcase_14 AC 166 ms
11,540 KB
testcase_15 AC 153 ms
10,744 KB
testcase_16 AC 131 ms
9,904 KB
testcase_17 AC 80 ms
7,448 KB
testcase_18 AC 94 ms
8,184 KB
testcase_19 AC 113 ms
8,796 KB
testcase_20 AC 114 ms
9,268 KB
testcase_21 AC 114 ms
8,968 KB
testcase_22 AC 102 ms
8,316 KB
testcase_23 AC 143 ms
10,448 KB
testcase_24 AC 106 ms
8,592 KB
testcase_25 AC 89 ms
7,776 KB
testcase_26 AC 89 ms
7,952 KB
testcase_27 AC 99 ms
8,384 KB
testcase_28 AC 158 ms
11,096 KB
testcase_29 AC 111 ms
9,000 KB
testcase_30 AC 153 ms
10,888 KB
testcase_31 AC 104 ms
8,400 KB
testcase_32 AC 108 ms
8,568 KB
testcase_33 AC 43 ms
9,328 KB
testcase_34 AC 94 ms
9,304 KB
testcase_35 AC 108 ms
11,888 KB
testcase_36 AC 129 ms
11,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
//勉強用写し

const int MOD = 998244353;
struct mint {
  long long x; // typedef long long ll;
  mint(long long x=0):x((x%MOD+MOD)%MOD){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += MOD-a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime MOD
  mint inv() const { return pow(MOD-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

template<typename T>
struct BIT {
  int n;
  vector<T> d;
  BIT(int n=0):n(n),d(n+1) {}
  void add(int i, T x=1) {
    for (i++; i <= n; i += i&-i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i&-i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) {
    return sum(r-1) - sum(l-1);
  }
};
template <class T>
std::vector<T> press(std::vector<T> &x) {
	auto res = x;
	std::sort(res.begin(), res.end());
	res.erase(std::unique(res.begin(), res.end()), res.end());
	for(int i = 0; i < (int)x.size(); i++)
		x[i] = std::lower_bound(res.begin(), res.end(), x[i]) - res.begin();
	return res;
}

mint res; 
int main() {
  int n;
  cin >> n;
  vector<int> a(n,0);
  rep(i,n) cin >> a[i];
  //座標圧縮
 	auto rec = press(a);
	int m = rec.size();
  //以下は前準備
  vector<mint> x(n), y(n);
	vector<int> l(n), r(n);

	{
		BIT<mint> BT(m);
		BIT<int> kosuu(m);
		for(int i = 0; i < n; i++) {
			BT.add(a[i], rec[a[i]]);
			kosuu.add(a[i], 1);
			x[i] = BT.sum(a[i] + 1, m);
			l[i] = kosuu.sum(a[i] + 1, m);
		}
	}

	{
		BIT<mint> BT(m);
		BIT<int> kosuu(m);
		for(int i = n - 1; i >= 0; i--) {
			BT.add(a[i], rec[a[i]]);
			kosuu.add(a[i], 1);
			y[i] = BT.sum(0, a[i]);
			r[i] = kosuu.sum(0, a[i]);
		}
	}
	//答えの計算
	for(int j = 0; j < n; j++) {
		if(!l[j] * r[j]) continue;
		mint tmp = rec[a[j]];
		res += x[j] * r[j] + y[j] * l[j] + tmp * l[j] * r[j];
	}
	cout << res << endl;
	return 0;
}
0