結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー kyo1kyo1
提出日時 2020-09-27 01:00:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 1,364 bytes
コンパイル時間 2,823 ms
コンパイル使用メモリ 86,148 KB
実行使用メモリ 8,864 KB
最終ジャッジ日時 2023-09-12 13:34:24
合計ジャッジ時間 4,892 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 81 ms
8,064 KB
testcase_04 AC 97 ms
8,864 KB
testcase_05 AC 81 ms
7,960 KB
testcase_06 AC 72 ms
7,552 KB
testcase_07 AC 95 ms
8,772 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 31 ms
6,388 KB
testcase_10 AC 56 ms
8,792 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 96 ms
8,748 KB
testcase_13 AC 96 ms
8,748 KB
testcase_14 AC 101 ms
8,760 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 7 ms
4,380 KB
testcase_24 AC 27 ms
5,084 KB
testcase_25 AC 66 ms
7,468 KB
testcase_26 AC 13 ms
4,480 KB
testcase_27 AC 34 ms
5,576 KB
testcase_28 AC 51 ms
6,596 KB
testcase_29 AC 73 ms
7,708 KB
testcase_30 AC 95 ms
8,652 KB
testcase_31 AC 18 ms
4,764 KB
testcase_32 AC 12 ms
4,432 KB
testcase_33 AC 78 ms
8,116 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstddef>
#include <iostream>
#include <map>
#include <vector>

template <typename T = int>
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() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int N;
  std::cin >> N;
  std::vector<int> A(N), B(N);
  for (auto&& e : A) {
    std::cin >> e;
  }
  std::map<int, int> table;
  for (int i = 0; i < N; i++) {
    table[A[i]] = i;
  }
  for (int i = 0; i < N; i++) {
    int b;
    std::cin >> b;
    B[i] = table[b];
  }
  FenwickTree ft(N);
  int64_t res = 0;
  for (int i = 0; i < N; i++) {
    res += i - ft.query(B[i]);
    ft.add(B[i], 1);
  }
  std::cout << res << '\n';
  return 0;
}
0