結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー 眼精疲労がやばい眼精疲労がやばい
提出日時 2022-02-28 23:01:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,575 bytes
コンパイル時間 1,528 ms
コンパイル使用メモリ 166,976 KB
実行使用メモリ 6,412 KB
最終ジャッジ日時 2023-09-21 07:53:03
合計ジャッジ時間 5,963 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 23 ms
5,684 KB
testcase_04 AC 26 ms
6,412 KB
testcase_05 AC 23 ms
5,624 KB
testcase_06 AC 20 ms
5,404 KB
testcase_07 AC 26 ms
6,208 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 14 ms
4,892 KB
testcase_10 AC 24 ms
6,148 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 26 ms
6,296 KB
testcase_13 AC 25 ms
6,228 KB
testcase_14 AC 26 ms
6,236 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 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 4 ms
4,380 KB
testcase_24 AC 10 ms
4,380 KB
testcase_25 AC 20 ms
5,436 KB
testcase_26 AC 6 ms
4,380 KB
testcase_27 AC 13 ms
4,376 KB
testcase_28 AC 16 ms
4,896 KB
testcase_29 AC 21 ms
5,500 KB
testcase_30 AC 25 ms
5,980 KB
testcase_31 AC 8 ms
4,384 KB
testcase_32 AC 5 ms
4,376 KB
testcase_33 AC 22 ms
5,676 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (ll i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vec;
using Graph = vector<vector<ll>>;
const long long INF = 1LL<<60;
const long long MOD = 1000000007;

template<typename T>
struct BIT {
private:
    vector<T> array;
    const int n;

public:
    BIT(int _n) :array(_n + 1, 0), n(_n) {};

    //1番目からi番目までの累積和
    T sum(int i) {
        T s = 0;
        while (i > 0) {
            s += array[i];
            i -= i & -i;//LSB減算
        }
        return s;
    }

    //[i,j]の要素の和
    T sum(int i, int j) {
        T ret_i = sum(i - 1);
        T ret_j = sum(j);
        return ret_j - ret_i;
    }

    //i番目に要素xを追加する
    void add(int i, T x) {
        while (i <= n) {
            array[i] += x;
            i += i & -i;//LSB加算
        }
    }
};

int main()
{  
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin>>n;
    vec a(n+1),b(n);
    rep(i,n){
        ll x;cin>>x;
        a[x]=i;
    }
    rep(i,n)cin>>b[i];
    vec A(n);
    rep(i,n){
        A[a[b[i]]]=i+1;
    }
    ll ans=0;
    BIT<ll> bit(n);
    rep(i,n){
        ans+=bit.sum(A[i],n);
        bit.add(A[i],1);
    }
    cout<<ans<<endl;
    return 0;
}
0