結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー iqueue02iqueue02
提出日時 2020-07-17 21:53:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,326 bytes
コンパイル時間 4,576 ms
コンパイル使用メモリ 201,704 KB
実行使用メモリ 5,364 KB
最終ジャッジ日時 2023-08-20 00:49:53
合計ジャッジ時間 5,848 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 65 ms
5,092 KB
testcase_04 AC 74 ms
5,136 KB
testcase_05 AC 64 ms
5,096 KB
testcase_06 AC 59 ms
4,832 KB
testcase_07 AC 75 ms
5,140 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 38 ms
4,376 KB
testcase_10 AC 67 ms
5,364 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 75 ms
5,044 KB
testcase_13 AC 76 ms
5,152 KB
testcase_14 AC 76 ms
5,136 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 7 ms
4,500 KB
testcase_24 AC 27 ms
4,380 KB
testcase_25 AC 56 ms
4,784 KB
testcase_26 AC 14 ms
4,376 KB
testcase_27 AC 33 ms
4,376 KB
testcase_28 AC 45 ms
4,568 KB
testcase_29 AC 59 ms
4,892 KB
testcase_30 AC 71 ms
5,092 KB
testcase_31 AC 18 ms
4,380 KB
testcase_32 AC 12 ms
4,380 KB
testcase_33 AC 59 ms
5,140 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
template <typename T> 
struct SegmentTree {
  
  int n;
  vector<T> node;

  T f(T a, T b) {return (a + b);}

  SegmentTree (int sz, T init = 0) {
    n = 1; while (n < sz) n <<= 1;
    node.assign(2*n, init);
    for (int i = n - 2; i >= 0; i--) node[i] = f(node[2*i+1], node[2*i+2]);
  }

  void update(int k, T x) {
    k += (n - 1);
    node[k] = x;
    while (k > 0) {
      k = (k - 1) >> 1;
      node[k] = f(node[2*k+1], node[2*k+2]);
    } 
  }

  T query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return 0;
    if (a <= l && r <= b) return node[k];
    T vl = query(a, b, 2*k+1, l, (l+r)/2);
    T vr = query(a, b, 2*k+2, (l+r)/2, r);
    return f(vl, vr);
  }

  T getvalue(int k) {
    return query(k,k+1);
  }

};
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  rep(i,n) cin >> a[i];
  rep(i,n) cin >> b[i];
  vector<int> id(n + 1);
  rep(i,n) id[a[i]] = i + 1;
  ll res = 0; 
  SegmentTree<int> seg(n + 1);
  for (int i = 0; i < n; i++) {
    res += i - seg.query(0, id[b[i]]);
    seg.update(id[b[i]] - 1, 1);
  }
  cout << res << endl;
  return 0;
} 
0