結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー Manuel1024
提出日時 2021-04-03 03:10:05
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,098 bytes
コンパイル時間 850 ms
コンパイル使用メモリ 83,448 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-12-24 03:24:41
合計ジャッジ時間 4,971 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
using namespace std;
using ll = long long int;

class BIT{
private:
    vector<ll> a;
    int n;
public:
    BIT(int len){
        a = vector<ll>(len+1, 0);
        n = len;
    }

    void add(int ind, ll x){
        ind++;
        while(ind <= n){
            a[ind] += x;
            ind += ind & (-ind);
        }
    }

    ll sum_sub(int end){
        end++;
        ll ans = 0;
        while(end > 0){
            ans += a[end];
            end -= end & (-end);
        }
        return ans;
    }

    ll sum(int start, int end){
        return sum_sub(end)-sum_sub(start-1);
    }
};

int main(){
    int n;
    cin >> n;
    vector<int> a0(n), b(n);
    for(auto &p: a0) cin >> p;
    for(auto &p: b) cin >> p;

    map<int, int> memo;
    for(int i = 0; i < n; i++) memo[b[i]] = i;

    vector<int> a(n);
    for(int i = 0; i < n; i++) a[i] = memo[a0[i]];

    BIT data(n);
    ll ans = 0;
    for(int i = 0; i < n; i++){
        data.add(a[i], 1);
        ans += data.sum(a[i], n-1)-1;
    }

    cout << ans << endl;
    return 0;
}
0