結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー merom686
提出日時 2020-07-17 21:52:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,005 bytes
コンパイル時間 846 ms
コンパイル使用メモリ 93,872 KB
最終ジャッジ日時 2025-01-11 22:34:30
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

template <class T>
struct BIT {
    BIT(int n) : b(n + 1), n(n) {}
    void add(int i, T v) {
        for (int k = i + 1; k <= n; k += k & -k) b[k] += v;
    }
    T sum(int k) {
        T s = 0;
        for (; k > 0; k -= k & -k) s += b[k];
        return s;
    }
    vector<T> b;
    int n;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        int t;
        cin >> t;
        t--;

        a[t] = i;
    }
    for (int i = 0; i < n; i++) {
        int t;
        cin >> t;
        t--;

        b[i] = a[t];
    }

    BIT<int> bt(n);
    ll r = (ll)n * (n - 1) / 2;
    for (int i = 0; i < n; i++) {
        int t = b[i];

        r -= bt.sum(t);
        bt.add(t, 1);
    }

    cout << r << endl;

    return 0;
}
0