結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー rokahikou1rokahikou1
提出日時 2020-08-06 03:22:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 2,153 bytes
コンパイル時間 966 ms
コンパイル使用メモリ 98,280 KB
実行使用メモリ 6,540 KB
最終ジャッジ日時 2023-10-19 03:43:25
合計ジャッジ時間 3,593 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 57 ms
6,012 KB
testcase_04 AC 69 ms
6,540 KB
testcase_05 AC 57 ms
6,012 KB
testcase_06 AC 51 ms
5,748 KB
testcase_07 AC 65 ms
6,540 KB
testcase_08 AC 5 ms
4,348 KB
testcase_09 AC 35 ms
5,220 KB
testcase_10 AC 59 ms
6,540 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 68 ms
6,540 KB
testcase_13 AC 67 ms
6,540 KB
testcase_14 AC 66 ms
6,540 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 7 ms
4,348 KB
testcase_24 AC 23 ms
4,428 KB
testcase_25 AC 49 ms
5,748 KB
testcase_26 AC 12 ms
4,348 KB
testcase_27 AC 29 ms
4,692 KB
testcase_28 AC 39 ms
5,220 KB
testcase_29 AC 51 ms
5,748 KB
testcase_30 AC 63 ms
6,276 KB
testcase_31 AC 16 ms
4,348 KB
testcase_32 AC 11 ms
4,348 KB
testcase_33 AC 56 ms
6,012 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

// 参考:https://qiita.com/drken/items/1b7e6e459c24a83bb7fd
// 1-indexed BIT
// add:log(N),sum:log(N),get_k-th:log(N)
// https://codeforces.com/contest/1354/problem/D
struct BIT {
    vector<ll> dat;

    BIT(int n) {
        dat.resize(n + 1);
        for(int i = 0; i < (int)dat.size(); i++)
            dat[i] = 0;
    }

    // a is 1-indexed
    void add(int a, ll x) {
        for(int i = a; i < (int)dat.size(); i += i & -i)
            dat[i] = dat[i] + x;
    }

    // [1,a], a is 1-indexed
    ll sum(int a) {
        ll res = 0;
        for(int i = a; i > 0; i -= i & -i) {
            res = res + dat[i];
        }
        return res;
    }

    // [a,b), a and b are 1-indexed
    ll sum(int a, int b) { return sum(b - 1) - sum(a - 1); }

    // k-th number (k is 1-indexed)
    // lower_bound
    int get(ll k) {
        if(k <= 0)
            return 0;
        int res = 0;
        int N = 1;
        while(N < (int)dat.size())
            N *= 2;
        for(int i = N / 2; i > 0; i /= 2) {
            if(res + i < (int)dat.size() && dat[res + i] < k) {
                k = k - dat[res + i];
                res = res + i;
            }
        }
        return res + 1;
    }
};

int main() {
    int N;
    cin >> N;
    vector<ll> A(N), B(N);
    vector<ll> idx(N + 1);
    rep(i, N) {
        cin >> A[i];
        idx[A[i]] = i + 1;
    }
    rep(i, N) cin >> B[i];
    BIT bit(N + 1);
    ll res = 0;
    rep(i, N) {
        res += i - bit.sum(idx[B[i]]);
        bit.add(idx[B[i]], 1);
    }
    cout << res << endl;
    return 0;
}
0