結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー shinchanshinchan
提出日時 2023-10-09 19:27:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,723 bytes
コンパイル時間 2,430 ms
コンパイル使用メモリ 205,508 KB
実行使用メモリ 6,964 KB
最終ジャッジ日時 2023-10-09 19:27:39
合計ジャッジ時間 4,993 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 43 ms
6,492 KB
testcase_04 AC 50 ms
6,680 KB
testcase_05 AC 43 ms
6,416 KB
testcase_06 AC 39 ms
6,164 KB
testcase_07 AC 49 ms
6,964 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 24 ms
4,944 KB
testcase_10 AC 40 ms
6,700 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 47 ms
6,732 KB
testcase_13 AC 48 ms
6,676 KB
testcase_14 AC 48 ms
6,804 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 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,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 5 ms
4,384 KB
testcase_24 AC 17 ms
4,588 KB
testcase_25 AC 38 ms
6,148 KB
testcase_26 AC 9 ms
4,380 KB
testcase_27 AC 23 ms
4,896 KB
testcase_28 AC 30 ms
5,156 KB
testcase_29 AC 39 ms
6,240 KB
testcase_30 AC 48 ms
6,656 KB
testcase_31 AC 12 ms
4,376 KB
testcase_32 AC 8 ms
4,376 KB
testcase_33 AC 38 ms
6,528 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 2 ms
4,380 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;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

struct SegmentTree {
private:
    ll n;
    vector<ll> node;
    ll ID = 0; // min -> INF, max, sum -> 0
    inline ll func(ll a, ll b){return a + b;} /////////

public:
    SegmentTree(vector<ll> v) {
        int sz = v.size();
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, ID);
        for(int i=0; i<sz; i++) node[i+n-1] = v[i];
        for(int i=n-2; i>=0; i--) node[i] = func(node[2*i+1], node[2*i+2]);
    }

    void update(ll x, ll val) {
        x += (n - 1);
        node[x] = val;
        while(x > 0) {
            x = (x - 1) / 2;
            node[x] = func(node[2*x+1], node[2*x+2]);
        }
    }

    ll get(ll a, ll b, ll k=0, ll l=0, ll r=-1) {
        if(r < 0) r = n;
        if(r <= a || b <= l) return ID;
        if(a <= l && r <= b) return node[k];

        ll vl = get(a, b, 2*k+1, l, (l+r)/2);
        ll vr = get(a, b, 2*k+2, (l+r)/2, r);
        return func(vl, vr);
    }
};
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<int> ord1(n), ord(n);
    rep(i, n) {
        int a;
        cin >> a;
        ord1[a - 1] = i;
    }
    rep(i, n) {
        int a;
        cin >> a;
        ord[ord1[a - 1]] = i;
    }

    ll ans = 0;
    SegmentTree seg(vector<ll> (n, 0));
    foa(e, ord) {
        ans += seg.get(e, n);
        seg.update(e, 1);
    }
    cout << ans << endl;
    
    return 0;
}
0