結果

問題 No.1300 Sum of Inversions
ユーザー SSRSSSRS
提出日時 2020-11-27 21:34:29
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 333 ms / 2,000 ms
コード長 1,672 bytes
コンパイル時間 2,018 ms
コンパイル使用メモリ 181,884 KB
実行使用メモリ 26,512 KB
最終ジャッジ日時 2023-10-01 04:59:21
合計ジャッジ時間 11,044 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 248 ms
21,380 KB
testcase_04 AC 248 ms
20,392 KB
testcase_05 AC 194 ms
17,536 KB
testcase_06 AC 286 ms
23,504 KB
testcase_07 AC 279 ms
22,288 KB
testcase_08 AC 318 ms
24,692 KB
testcase_09 AC 307 ms
24,120 KB
testcase_10 AC 151 ms
15,300 KB
testcase_11 AC 155 ms
15,548 KB
testcase_12 AC 240 ms
20,708 KB
testcase_13 AC 236 ms
20,248 KB
testcase_14 AC 333 ms
26,260 KB
testcase_15 AC 295 ms
24,272 KB
testcase_16 AC 249 ms
21,464 KB
testcase_17 AC 148 ms
15,008 KB
testcase_18 AC 175 ms
16,488 KB
testcase_19 AC 211 ms
18,548 KB
testcase_20 AC 216 ms
19,196 KB
testcase_21 AC 210 ms
19,248 KB
testcase_22 AC 190 ms
17,828 KB
testcase_23 AC 280 ms
23,084 KB
testcase_24 AC 200 ms
18,028 KB
testcase_25 AC 164 ms
15,908 KB
testcase_26 AC 161 ms
15,652 KB
testcase_27 AC 185 ms
17,272 KB
testcase_28 AC 315 ms
24,964 KB
testcase_29 AC 210 ms
18,856 KB
testcase_30 AC 314 ms
24,116 KB
testcase_31 AC 202 ms
17,784 KB
testcase_32 AC 201 ms
18,284 KB
testcase_33 AC 43 ms
10,936 KB
testcase_34 AC 95 ms
10,948 KB
testcase_35 AC 195 ms
26,512 KB
testcase_36 AC 215 ms
26,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
struct binary_indexed_tree{
  int N;
  vector<long long> BIT;
  binary_indexed_tree(int N): N(N){
    BIT = vector<long long>(N + 1, 0);
  }
  void add(int i, long long x){
    i++;
    while (i <= N){
      BIT[i] += x;
      BIT[i] %= MOD;
      i += i & -i;
    }
  }
  long long sum(int i){
    long long ans = 0;
    while (i > 0){
      ans += BIT[i];
      ans %= MOD;
      i -= i & -i;
    }
    return ans;
  }
  long long sum(int L, int R){
    return (sum(R) - sum(L) + MOD) % MOD;
  }
};
int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<int> A2 = A;
  sort(A2.begin(), A2.end());
  A2.erase(unique(A2.begin(), A2.end()), A2.end());
  int cnt = A2.size();
  map<int, int> mp;
  for (int i = 0; i < cnt; i++){
    mp[A2[i]] = i;
  }
  for (int i = 0; i < N; i++){
    A[i] = mp[A[i]];
  }
  vector<long long> F(N, 0);
  vector<long long> Fc(N, 0);
  binary_indexed_tree BIT1(cnt);
  binary_indexed_tree BIT2(cnt);
  for (int i = 0; i < N; i++){
    F[i] = BIT1.sum(A[i] + 1, cnt);
    Fc[i] = BIT2.sum(A[i] + 1, cnt);
    BIT1.add(A[i], A2[A[i]]);
    BIT2.add(A[i], 1);
  }
  vector<long long> B(N, 0);
  vector<long long> Bc(N, 0);
  binary_indexed_tree BIT3(cnt);
  binary_indexed_tree BIT4(cnt);
  for (int i = N - 1; i >= 0; i--){
    B[i] = BIT3.sum(A[i]);
    Bc[i] = BIT4.sum(A[i]);
    BIT3.add(A[i], A2[A[i]]);
    BIT4.add(A[i], 1);
  }
  long long ans = 0;
  for (int i = 0; i < N; i++){
    ans += F[i] * Bc[i] + B[i] * Fc[i] + Bc[i] * Fc[i] % MOD * A2[A[i]];
    ans %= MOD;
  }
  cout << ans << endl;
}
0