結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー hedwig100hedwig100
提出日時 2020-08-10 22:53:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,732 bytes
コンパイル時間 1,761 ms
コンパイル使用メモリ 164,864 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-17 06:25:49
合計ジャッジ時間 4,760 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 55 ms
6,940 KB
testcase_04 AC 63 ms
6,940 KB
testcase_05 AC 55 ms
6,940 KB
testcase_06 AC 50 ms
6,940 KB
testcase_07 AC 63 ms
6,944 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 36 ms
6,940 KB
testcase_10 AC 62 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 63 ms
6,940 KB
testcase_13 AC 64 ms
6,940 KB
testcase_14 AC 64 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 7 ms
6,940 KB
testcase_24 AC 24 ms
6,940 KB
testcase_25 AC 48 ms
6,940 KB
testcase_26 AC 13 ms
6,944 KB
testcase_27 AC 29 ms
6,940 KB
testcase_28 AC 39 ms
6,940 KB
testcase_29 AC 50 ms
6,944 KB
testcase_30 AC 60 ms
6,940 KB
testcase_31 AC 16 ms
6,940 KB
testcase_32 AC 12 ms
6,944 KB
testcase_33 AC 54 ms
6,940 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

//T is longlong or int
//1-indexed
template<class T> 
struct BinaryIndexedTree {
    int N,power = 1;
    vector<T> bit;

    BinaryIndexedTree(int N = 0): N(N){
        bit.assign(N + 1,0);
        while (power <= N) power <<= 1; //power > N
    }
    //add x to a_i
    void add(int i,T x) {
        for (int idx = i; idx <= N; idx += (idx & -idx)) {
            bit[idx] += x;
        }
    }
    //return sum of a_1 + a_2 + a_3 + .. + a_k
    T sum(int k) {
        T ret = 0;
        for (int idx = k; idx > 0; idx -= (idx & -idx)) {
            ret += bit[idx];
        }
        return ret;
    }
    // return min index s.t. a_1 + a_2 + a_3 + .. + a_x >= w
    int lower_bound(T w) {
        if (w <= 0) return 0;
        int x = 0;
        for (int r = power; r > 0; r >>= 1) {
            if (x + r <= N && bit[x + r] < w) {
                w -= bit[x + r];
                x += r;
            }
        } 
        return x + 1;
    }
};

int main() {
    int N; cin >> N;
    vector<int> A(N),B(N),C(N + 1);
    rep(i,N) cin >> A[i];
    rep(i,N) cin >> B[i];
    rep(i,N) C[B[i]] = i;
    rep(i,N) A[i] = C[A[i]];

    BinaryIndexedTree<int> bit(N);
    ll ans = 0;
    rep(i,N) {
        ans += i - bit.sum(A[i] + 1);
        bit.add(A[i] + 1,1);
    }
    cout << ans << '\n';
    return 0;
}
0