結果

問題 No.1300 Sum of Inversions
ユーザー Mr.SpockMr.Spock
提出日時 2020-12-01 16:28:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,513 bytes
コンパイル時間 1,896 ms
コンパイル使用メモリ 180,452 KB
実行使用メモリ 23,796 KB
最終ジャッジ日時 2023-10-11 03:37:57
合計ジャッジ時間 11,484 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,664 KB
testcase_01 AC 2 ms
7,528 KB
testcase_02 AC 2 ms
7,528 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 51 ms
10,372 KB
testcase_34 AC 64 ms
10,252 KB
testcase_35 AC 174 ms
23,796 KB
testcase_36 AC 177 ms
23,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll nax = 2 * 1e5 + 7;
const ll mod = 998244353;
struct bit {
	const static ll siz = 2e5 + 7;
	ll bit[siz];
	ll get(ll idx) {
		ll sum = 0;
		while (idx > 0) {
			sum += bit[idx];
			idx = (idx & (idx + 1)) - 1;
		}
		return sum;
	}
	void modify(ll idx, ll val) {
		while (idx < siz) {
			bit[idx] += val;
			idx = idx | (idx + 1);
		}
	}
	ll range(ll l, ll r) {
		return (get(r) - get(l - 1));
	}
} rightcnt, leftcnt, rightsum, leftsum;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
	cin >> n;
	vector<ll>v(n + 1);
	map<int, int>m;
	for (ll i = 0; i < n; i++) {
		cin >> v[i];
		m[v[i]]++;
	}
	vector<ll>a;
	for (int i = 0; i < n; i++) {
		if (m[v[i]]) {
			a.push_back(v[i]);
			m[v[i]] = 0;
		}
	}
	sort(a.begin(), a.end());
	vector<ll>b(n);
	for (ll i = 0; i < n; i++) {
		b[i] = lower_bound(a.begin(), a.end(), v[i]) - a.begin();
	}
	for (ll i = 0; i < n; i++) {
		rightcnt.modify(b[i], 1);
		rightsum.modify(b[i], v[i]);
	}
	ll ans = 0;
	for (ll i = 0; i < n; i++) {
		rightsum.modify(b[i], -v[i]);
		rightcnt.modify(b[i], -1);
		ll cnt1 = rightcnt.range(0, b[i]);
		ll cnt2 = leftcnt.range(b[i] + 1, nax);
		ans += ((v[i] % mod) * (cnt1 % mod) * (cnt2 % mod));
		ans %= mod;
		ans += ((rightsum.range(0, b[i]) % mod) * (cnt2 % mod));
		ans %= mod;
		ans += ((leftsum.range(b[i] + 1, nax) % mod) * (cnt1 % mod));
		ans %= mod;
		leftsum.modify(b[i], v[i]);
		leftcnt.modify(b[i], 1);
	}
	cout << ans << endl;
}
0