結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー kyo1kyo1
提出日時 2020-09-14 17:07:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,274 bytes
コンパイル時間 3,749 ms
コンパイル使用メモリ 205,528 KB
実行使用メモリ 8,916 KB
最終ジャッジ日時 2023-09-04 00:44:51
合計ジャッジ時間 7,243 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 70 ms
8,092 KB
testcase_04 AC 82 ms
8,804 KB
testcase_05 AC 68 ms
8,148 KB
testcase_06 AC 62 ms
7,532 KB
testcase_07 AC 82 ms
8,836 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 30 ms
6,476 KB
testcase_10 AC 53 ms
8,780 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 83 ms
8,816 KB
testcase_13 AC 83 ms
8,808 KB
testcase_14 AC 87 ms
8,916 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 25 ms
5,188 KB
testcase_25 AC 59 ms
7,236 KB
testcase_26 AC 12 ms
4,428 KB
testcase_27 AC 33 ms
5,672 KB
testcase_28 AC 46 ms
6,456 KB
testcase_29 AC 64 ms
7,820 KB
testcase_30 AC 79 ms
8,668 KB
testcase_31 AC 18 ms
4,800 KB
testcase_32 AC 11 ms
4,380 KB
testcase_33 AC 68 ms
7,988 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <typename T>
class FenwickTree {
 private:
  std::vector<T> tree;

 public:
  explicit FenwickTree(const std::size_t n) : tree(n + 1, 0) {}

  explicit FenwickTree(const std::vector<T> &vec) : tree(vec.size() + 1) {
    for (std::size_t i = 0; i < vec.size(); i++) {
      add(i, vec[i]);
    }
  }

  std::size_t size() const { return tree.size() - 1; }

  void add(const std::size_t idx, const T x) {
    for (std::size_t i = idx + 1; i < tree.size(); i += i & (~i + 1)) {
      tree[i] += x;
    }
  }

  T query(const std::size_t idx) const {
    T res = 0;
    for (std::size_t i = idx; i != 0; i &= i - 1) {
      res += tree[i];
    }
    return res;
  }

  T query(const std::size_t l, const std::size_t r) const { return query(r) - query(l); }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  map<int, int> table;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    table[A[i]] = i;
  }
  for (int i = 0; i < N; i++) {
    int b;
    cin >> b;
    B[i] = table[b];
  }
  FenwickTree<int> ft(N);
  int64_t res = 0;
  for (int i = 0; i < N; i++) {
    res += i - ft.query(B[i]);
    ft.add(B[i], 1);
  }
  cout << res << '\n';
  return 0;
}
0