結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー けーむけーむ
提出日時 2020-07-20 15:08:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 2,207 bytes
コンパイル時間 1,722 ms
コンパイル使用メモリ 178,652 KB
実行使用メモリ 13,840 KB
最終ジャッジ日時 2023-08-25 11:46:18
合計ジャッジ時間 7,883 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 136 ms
12,548 KB
testcase_04 AC 162 ms
13,576 KB
testcase_05 AC 135 ms
12,508 KB
testcase_06 AC 121 ms
11,760 KB
testcase_07 AC 159 ms
13,832 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 60 ms
9,120 KB
testcase_10 AC 103 ms
13,684 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 148 ms
13,652 KB
testcase_13 AC 153 ms
13,840 KB
testcase_14 AC 147 ms
13,644 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 10 ms
4,380 KB
testcase_24 AC 45 ms
7,052 KB
testcase_25 AC 110 ms
11,504 KB
testcase_26 AC 22 ms
5,212 KB
testcase_27 AC 56 ms
7,748 KB
testcase_28 AC 80 ms
9,168 KB
testcase_29 AC 112 ms
11,836 KB
testcase_30 AC 141 ms
13,288 KB
testcase_31 AC 28 ms
5,912 KB
testcase_32 AC 19 ms
4,824 KB
testcase_33 AC 117 ms
12,568 KB
testcase_34 AC 1 ms
4,384 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,384 KB
testcase_39 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

template<class T>
struct SegmentTree {
private:
    int n;
    vector<T> node;
    T def;
    function<T(T,T)> lop;
    function<T(T,T)> out;
    
public:
    SegmentTree() {}
    SegmentTree(const vector<T> &v, T _def, function<T(T,T)> _lop, function<T(T,T)> _out) {
        int vl = (int)v.size();
        n = 1;
        while(n < vl) n *= 2;
        def = _def; lop = _lop; out = _out;
        node = vector<T>(2 * n - 1, def);
        for(int i = 0; i < vl; i++) node[i + n - 1] = v[i];
        for(int i = n - 2; i >= 0; i--) node[i] = out(node[i * 2 + 1], node[i * 2 + 2]);
    }
    
    void update(int idx, T val) {
        idx += (n - 1);
        node[idx] = lop(node[idx], val);
        while(idx > 0) {
            idx = (idx - 1) / 2;
            node[idx] = out(node[idx * 2 + 1], node[idx * 2 + 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 def;
        if ((a <= l) && (r <= b)) return node[k];
        return out(query(a, b, 2 * k + 1, l, (l + r) / 2), query(a, b, 2 * k + 2, (l + r) / 2, r));
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n;
    cin >> n;
    vector<ll> a(n), b(n);
    rep(i, n) cin >> a[i];
    rep(i, n) cin >> b[i];
    map<ll, ll> idx;
    rep(i, n) idx[a[i]] = i;
    auto lop = [](ll a, ll b){ return b; };
    auto out = [](ll a, ll b){ return a + b; };
    SegmentTree<ll> st(vector<ll>(n, 0), 0, lop, out);
    ll ans = 0;
    rep(i, n) {
        ll cnt = st.query(idx[b[i]], n);
        st.update(idx[b[i]], 1);
        ans += cnt;
    }
    cout << ans << endl;
    return 0;
}
0