結果
問題 | No.1300 Sum of Inversions |
ユーザー |
![]() |
提出日時 | 2020-11-27 22:16:10 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 181 ms / 2,000 ms |
コード長 | 1,808 bytes |
コンパイル時間 | 2,863 ms |
コンパイル使用メモリ | 205,464 KB |
最終ジャッジ日時 | 2025-01-16 07:39:06 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <bits/stdc++.h>using namespace std;int main() {constexpr int64_t mod = 998244353;int n;cin >> n;vector<int> a(n);for (int i = 0; i < n; i++) {cin >> a.at(i);}reverse(a.begin(), a.end());vector<int> b = a;sort(b.begin(), b.end());b.erase(unique(b.begin(), b.end()), b.end());vector<int> c(n);for (int i = 0; i < n; i++) {c.at(i) = lower_bound(b.begin(), b.end(), a.at(i)) - b.begin();}vector<int64_t> s(n + 1), t(n + 1);vector<int64_t> s0(n), t0(n);for (int i = 0; i < n; i++) {int x = c.at(i);while (x) {(s0.at(i) += s.at(x)) %= mod;t0.at(i) += t.at(x);x -= x & -x;}x = c.at(i) + 1;while (x <= n) {(s.at(x) += b.at(c.at(i))) %= mod;t.at(x)++;x += x & -x;}}for (int i = 0; i <= n; i++) {s.at(i) = 0;t.at(i) = 0;}reverse(c.begin(), c.end());vector<int64_t> s1(n), t1(n);int64_t ssum = 0;for (int i = 0; i < n; i++) {int x = c.at(i) + 1;s1.at(i) = ssum;t1.at(i) = i;(ssum += b.at(c.at(i))) %= mod;while (x) {(s1.at(i) -= s.at(x)) %= mod;t1.at(i) -= t.at(x);x -= x & -x;}x = c.at(i) + 1;while (x <= n) {(s.at(x) += b.at(c.at(i))) %= mod;t.at(x)++;x += x & -x;}}int64_t ans = 0;for (int i = 0; i < n; i++) {int p = n - 1 - i;ans += a.at(i) * t0.at(i) % mod * t1.at(p) % mod;ans += s0.at(i) * t1.at(p) % mod;ans += t0.at(i) * s1.at(p) % mod;ans %= mod;}cout << (ans + mod) % mod << endl;}