結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー ninoinuininoinui
提出日時 2020-07-19 02:07:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 2,103 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 203,928 KB
実行使用メモリ 9,700 KB
最終ジャッジ日時 2023-08-21 14:35:14
合計ジャッジ時間 5,509 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 35 ms
8,888 KB
testcase_04 AC 40 ms
9,700 KB
testcase_05 AC 34 ms
8,540 KB
testcase_06 AC 34 ms
8,308 KB
testcase_07 AC 42 ms
9,680 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 17 ms
6,812 KB
testcase_10 AC 28 ms
9,572 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 41 ms
9,624 KB
testcase_13 AC 43 ms
9,676 KB
testcase_14 AC 42 ms
9,632 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 14 ms
5,452 KB
testcase_25 AC 31 ms
7,988 KB
testcase_26 AC 8 ms
4,380 KB
testcase_27 AC 17 ms
6,064 KB
testcase_28 AC 24 ms
6,984 KB
testcase_29 AC 31 ms
8,244 KB
testcase_30 AC 39 ms
9,440 KB
testcase_31 AC 10 ms
4,544 KB
testcase_32 AC 7 ms
4,384 KB
testcase_33 AC 30 ms
8,808 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T> class FenwickTree {
private:
  int n, mx;
  vector<T> bit;
public:
  FenwickTree(int sz) : n(sz + 1), mx(1), bit(n, 0) {
      while (mx * 2 <= n) mx *= 2;
  }
  FenwickTree(const vector<T> &v) : n((int) v.size() + 1), mx(1), bit(n, 0) {
    for (int i = 0; i < n - 1; i++) add(i, v.at(i));
    while (mx * 2 <= n) mx *= 2;
  }
  void add(int i, T x) {
    i++;
    while (i < n) bit.at(i) += x, i += i & -i;
  }
  void add(int l, int r, T x) {
    add(l, x);
    add(r + 1 , -x);
  }
  T sum(int i) {
    i++;
    T ret = 0;
    while (i > 0) ret += bit.at(i), i -= i & -i;
    return ret;
  }
  T sum(int l, int r) {
    return sum(r) - sum(l - 1);
  }
  int lower_bound(T w) {
    if (w <= 0) return 0;
    int x = 0;
    for (int k = mx; k > 0; k /= 2) {
      if (x + k <= n - 1 && bit.at(x + k) < w) {
        w -= bit.at(x + k);
        x += k;
      }
    }
    return x;
  }
  int upper_bound(T w) {
    if (w < 0) return 0;
    int x = 0;
    for (int k = mx; k > 0; k /= 2) {
      if (x + k <= n - 1 && bit.at(x + k) <= w) {
        w -= bit.at(x + k);
        x += k;
      }
    }
    return x;
  }
};

long inv_count(const vector<int> &u) {
  int n = (int) u.size();
  FenwickTree<int> FT(n);
  long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += i - FT.sum(u.at(i));
    FT.add(u.at(i), 1);
  }
  return ans;
}

long inv_count(const vector<int> &u, const vector<int> &v) {
  int n = (int) u.size();
  vector<vector<int>> p(n);
  FenwickTree<int> FT(n);
  for (int i = n - 1; i >= 0; i--) {
    p.at(u.at(i)).push_back(i);
  }
  long ans = 0;
  for (int i = 0; i < n; ++i) {
    int pos = p.at(v.at(i)).back();
    p.at(v.at(i)).pop_back();
    ans += pos - FT.sum(pos);
    FT.add(pos, 1);
  }
  return ans;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    A.at(i)--;
  }
  vector<int> B(N);
  for (int i = 0; i < N; i++) {
    cin >> B.at(i);
    B.at(i)--;
  }
  cout << inv_count(A, B) << "\n";
}
0