結果
| 問題 |
No.1300 Sum of Inversions
|
| コンテスト | |
| ユーザー |
trineutron
|
| 提出日時 | 2020-11-27 22:13:52 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,873 bytes |
| コンパイル時間 | 8,492 ms |
| コンパイル使用メモリ | 266,516 KB |
| 最終ジャッジ日時 | 2025-01-16 07:35:44 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 WA * 1 |
ソースコード
#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) * t1.at(p) % mod;
ans += s0.at(i) * t1.at(p) % mod;
ans += t0.at(i) * s1.at(p) % mod;
ans %= mod;
cerr << a.at(i) << " " << t0.at(i) << " " << t1.at(p) << endl;
}
cout << (ans + mod) % mod << endl;
}
trineutron