結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー itohdakitohdak
提出日時 2020-07-17 22:17:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,162 bytes
コンパイル時間 2,240 ms
コンパイル使用メモリ 203,848 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-07 09:16:11
合計ジャッジ時間 4,002 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 21 ms
6,940 KB
testcase_04 AC 24 ms
6,940 KB
testcase_05 AC 21 ms
6,944 KB
testcase_06 AC 19 ms
6,944 KB
testcase_07 AC 22 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 13 ms
6,944 KB
testcase_10 AC 22 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 24 ms
6,944 KB
testcase_13 AC 23 ms
6,940 KB
testcase_14 AC 24 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 10 ms
6,940 KB
testcase_25 AC 17 ms
6,940 KB
testcase_26 AC 6 ms
6,940 KB
testcase_27 AC 11 ms
6,940 KB
testcase_28 AC 16 ms
6,940 KB
testcase_29 AC 19 ms
6,940 KB
testcase_30 AC 23 ms
6,940 KB
testcase_31 AC 7 ms
6,940 KB
testcase_32 AC 5 ms
6,940 KB
testcase_33 AC 19 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;

template< typename T >
struct BinaryIndexedTree {
  vector< T > data;

  BinaryIndexedTree(int sz) {
    data.assign(++sz, 0);
  }

  T sum(int k) {
    T ret = 0;
    for(++k; k > 0; k -= k & -k) ret += data[k];
    return (ret);
  }

  void add(int k, T x) {
    for(++k; k < data.size(); k += k & -k) data[k] += x;
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n; cin >> n;
  vector<int> A(n), B(n), pos(n);
  rep(i, n) {
    cin >> A[i]; A[i]--;
    pos[A[i]] = i;
  }
  rep(i, n) { cin >> B[i]; B[i]--; }
  vector<int> C(n);
  rep(i, n) C[pos[B[i]]] = i;
  BinaryIndexedTree<int> bit(n);
  vector<int> bias(n);
  rrep(i, n) {
    bias[C[i]] = bit.sum(C[i]);
    bit.add(C[i], 1);
  }
  ll ans = 0;
  rep(i, n) {
    ans += i+bias[C[i]]-C[i];
  }
  cout << ans << "\n";
  return 0;
}
0