結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー kyo1kyo1
提出日時 2020-09-09 11:40:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,396 bytes
コンパイル時間 772 ms
コンパイル使用メモリ 86,820 KB
実行使用メモリ 8,904 KB
最終ジャッジ日時 2023-08-21 06:38:04
合計ジャッジ時間 5,207 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 71 ms
7,988 KB
testcase_04 AC 85 ms
8,812 KB
testcase_05 AC 72 ms
8,064 KB
testcase_06 AC 63 ms
7,560 KB
testcase_07 AC 86 ms
8,816 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 30 ms
6,392 KB
testcase_10 AC 54 ms
8,904 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 87 ms
8,888 KB
testcase_13 AC 86 ms
8,860 KB
testcase_14 AC 87 ms
8,828 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 6 ms
4,376 KB
testcase_24 AC 26 ms
5,160 KB
testcase_25 AC 60 ms
7,520 KB
testcase_26 AC 13 ms
4,380 KB
testcase_27 AC 33 ms
5,652 KB
testcase_28 AC 47 ms
6,532 KB
testcase_29 AC 66 ms
7,900 KB
testcase_30 AC 82 ms
8,744 KB
testcase_31 AC 17 ms
4,912 KB
testcase_32 AC 11 ms
4,376 KB
testcase_33 AC 70 ms
8,016 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://atcoder.jp/contests/practice2/tasks/practice2_b

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

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

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

  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> mp;
  for (int i = 0; i < N; i++) {
    mp[A[i]] = i;
  }
  for (int i = 0; i < N; i++) {
    int b;
    std::cin >> b;
    B[i] = mp[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);
  }
  std::cout << res << '\n';
  return 0;
}
0