結果

問題 No.1300 Sum of Inversions
ユーザー simansiman
提出日時 2022-03-19 03:35:33
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,829 bytes
コンパイル時間 1,680 ms
コンパイル使用メモリ 140,708 KB
実行使用メモリ 17,252 KB
最終ジャッジ日時 2024-04-14 16:22:52
合計ジャッジ時間 8,865 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 94 ms
16,244 KB
testcase_34 AC 151 ms
16,272 KB
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

const ll MOD = 988244353;

struct Node {
  int idx;
  ll value;

  Node(int idx = -1, ll value = -1) {
    this->idx = idx;
    this->value = value;
  }

  bool operator<(const Node &n) const {
    if (value == n.value) {
      return idx > n.idx;
    } else {
      return value > n.value;
    }
  }
};

class BinaryIndexTree {
  public:
    vector <ll> bit;
    int N;

    BinaryIndexTree(int n) {
      N = n;

      for (int i = 0; i <= N; ++i) {
        bit.push_back(0);
      }
    }

    ll sum(int i) {
      ll ret = 0;

      while (i > 0) {
        ret += bit[i];
        ret %= MOD;
        i -= i & -i;
      }

      return ret;
    }

    void add(int i, ll x) {
      while (i <= N) {
        bit[i] += x;
        bit[i] %= MOD;
        i += i & -i;
      }
    }
};

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  vector<Node> nodes;

  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    nodes.push_back(Node(i + 1, A[i]));
  }

  sort(nodes.begin(), nodes.end());

  BinaryIndexTree bit1_1(N + 1);
  BinaryIndexTree bit1_2(N + 1);
  BinaryIndexTree bit2_1(N + 1);
  BinaryIndexTree bit2_2(N + 1);
  ll ans = 0;

  for (Node &node : nodes) {
    ll cnt_1 = bit1_1.sum(node.idx - 1);
    ll cnt_2 = bit2_1.sum(node.idx - 1);
    ll sum_1 = bit1_2.sum(node.idx - 1);
    ll sum_2 = bit2_2.sum(node.idx - 1);
    bit1_1.add(node.idx, 1);
    bit1_2.add(node.idx, node.value);
    bit2_1.add(node.idx, cnt_1);
    bit2_2.add(node.idx, sum_1 + cnt_1 * node.value);
    ans += sum_2 + cnt_2 * node.value;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}
0