結果

問題 No.1300 Sum of Inversions
ユーザー yataro800yataro800
提出日時 2023-01-28 17:05:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 667 ms / 2,000 ms
コード長 1,647 bytes
コンパイル時間 4,230 ms
コンパイル使用メモリ 239,416 KB
実行使用メモリ 32,624 KB
最終ジャッジ日時 2023-09-11 05:47:59
合計ジャッジ時間 22,803 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 480 ms
26,012 KB
testcase_04 AC 475 ms
25,180 KB
testcase_05 AC 376 ms
21,824 KB
testcase_06 AC 546 ms
28,796 KB
testcase_07 AC 560 ms
27,656 KB
testcase_08 AC 609 ms
30,068 KB
testcase_09 AC 594 ms
29,976 KB
testcase_10 AC 277 ms
18,316 KB
testcase_11 AC 273 ms
18,584 KB
testcase_12 AC 489 ms
25,760 KB
testcase_13 AC 452 ms
24,960 KB
testcase_14 AC 667 ms
32,624 KB
testcase_15 AC 596 ms
29,776 KB
testcase_16 AC 491 ms
26,496 KB
testcase_17 AC 252 ms
17,976 KB
testcase_18 AC 320 ms
19,944 KB
testcase_19 AC 378 ms
22,940 KB
testcase_20 AC 387 ms
23,404 KB
testcase_21 AC 422 ms
23,672 KB
testcase_22 AC 352 ms
21,480 KB
testcase_23 AC 536 ms
28,676 KB
testcase_24 AC 376 ms
22,200 KB
testcase_25 AC 310 ms
19,420 KB
testcase_26 AC 307 ms
19,420 KB
testcase_27 AC 355 ms
21,004 KB
testcase_28 AC 587 ms
30,728 KB
testcase_29 AC 413 ms
23,180 KB
testcase_30 AC 583 ms
29,936 KB
testcase_31 AC 363 ms
21,800 KB
testcase_32 AC 386 ms
22,460 KB
testcase_33 AC 102 ms
11,092 KB
testcase_34 AC 155 ms
11,264 KB
testcase_35 AC 281 ms
32,568 KB
testcase_36 AC 302 ms
32,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <math.h>

#include <algorithm>
#include <array>
#include <atcoder/all>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;
using namespace atcoder;

using Graph = vector<vector<int>>;

using ll = long long;
typedef pair<ll, ll> P_ll;
typedef pair<int, int> P;

const ll INF_ll = 1e17;
const int INF = 1e8;
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  set<ll> s(A.begin(), A.end());
  map<ll, ll> d;
  int cnt = 0;
  for (auto x : s) d[x] = cnt++;

  fenwick_tree<ll> cl(N), sl(N), cr(N), sr(N);
  for (int i = 0; i < N; i++) {
    ll x = d[A[i]];
    cr.add(x, 1);
    sr.add(x, A[i]);
  }
  modint998244353 ans = 0;
  for (int i = 0; i < N; i++) {
    ll x = d[A[i]];
    {
      modint998244353 a = 1;
      a *= sr.sum(0, x);
      a *= cl.sum(x + 1, N);
      ans += a;
    }
    {
      modint998244353 a = 1;
      a *= sl.sum(x + 1, N);
      a *= cr.sum(0, x);
      ans += a;
    }
    {
      modint998244353 a = 1;
      a *= cr.sum(0, x);
      a *= cl.sum(x + 1, N);
      a *= A[i];
      ans += a;
    }
    cl.add(x, 1);
    sl.add(x, A[i]);
    cr.add(x, -1);
    sr.add(x, -A[i]);
  }
  cout << ans.val() << endl;

  return 0;
}
0