結果
| 問題 |
No.1300 Sum of Inversions
|
| コンテスト | |
| ユーザー |
Manuel1024
|
| 提出日時 | 2022-01-11 03:22:04 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 189 ms / 2,000 ms |
| コード長 | 2,260 bytes |
| コンパイル時間 | 896 ms |
| コンパイル使用メモリ | 80,252 KB |
| 実行使用メモリ | 12,672 KB |
| 最終ジャッジ日時 | 2024-11-14 11:23:46 |
| 合計ジャッジ時間 | 7,421 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
constexpr ll MOD = 998244353;
template <typename T>
struct FenwickTree{
const int n;
vector<T> arr;
FenwickTree() = default;
FenwickTree(int n): n(n), arr(vector<T>(n+1, 0)){}
void add(int ind, T x){
for(int i = ind+1; i <= n; i += i & (-i)){
arr[i] += x;
}
}
T sum_sub(int end){
T res = 0;
for(int i = end; i > 0; i -= i & (-i)){
res += arr[i];
}
return res;
}
T sum(int start, int end){
return sum_sub(end) - sum_sub(start);
}
int lower_bound(T w){
if(w <= 0) return 0;
int x = 0;
int k = 1 << 30;
while(k > n) k /= 2;
for(; k > 0; k /= 2){
if(x+k <= n && arr[x+k] < w){
w -= arr[x+k];
x += k;
}
}
return x;
}
};
template <typename T>
vector<T> compress(vector<T> &x){
vector<T> vals = x;
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
for(auto &p: x){
p = lower_bound(vals.begin(), vals.end(), p) - vals.begin();
}
return vals;
}
int main(){
int n;
cin >> n;
vector<ll> a(n);
for(auto &it: a) cin >> it;
auto val = compress(a);
ll ans = 0;
FenwickTree<ll> cntl(n+1), cntr(n+1);
FenwickTree<ll> totl(n+1), totr(n+1);
for(int i = 0; i < n; i++){
cntr.add(a[i], 1);
totr.add(a[i], val[a[i]]);
}
for(int i = 0; i < n; i++){
cntr.add(a[i], -1);
totr.add(a[i], -val[a[i]]);
ans += (cntl.sum(a[i]+1, n+1)*cntr.sum(0, a[i]))%MOD*val[a[i]];
ans %= MOD;
// cout << ans << " " << val[a[i]] << " ";
if(cntl.sum(a[i]+1, n+1)*cntr.sum(0, a[i]) > 0){
ans += totr.sum(0, a[i])%MOD*cntl.sum(a[i]+1, n+1);
ans %= MOD;
ans += totl.sum(a[i]+1, n+1)%MOD*cntr.sum(0, a[i]);
ans %= MOD;
}
// cout << cntl.sum(a[i]+1, n+1) << " " << cntr.sum(0, a[i]) << " " << ans << endl;
cntl.add(a[i], 1);
totl.add(a[i], val[a[i]]);
}
cout << ans << endl;
return 0;
}
Manuel1024