結果
| 問題 | 
                            No.1115 二つの数列 / Two Sequences
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-11-13 15:24:27 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 63 ms / 2,000 ms | 
| コード長 | 985 bytes | 
| コンパイル時間 | 867 ms | 
| コンパイル使用メモリ | 82,604 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-22 20:02:09 | 
| 合計ジャッジ時間 | 4,508 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 5 | 
| other | AC * 35 | 
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
using namespace std;
struct BIT{
    vector<long long> num;
    int N;
    BIT(int n){
        N = n;
        num.resize(N + 1, 0);
    }
    long long sum(int t){
        long long res = 0;
        while(t > 0){
            res += num[t];
            t -= t & -t;
        }
        return res;
    }
    void add(int ind, long long t){
        ind++;
        while(ind <= N){
            num[ind] += t;
            ind += ind & -ind;
        }
    }
};
int main(){
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    vector<int> ind(n);
    for(int i = 0; i < n; i++) ind[a[i] - 1] = i;
    BIT bit(n);
    long long cnt = 0;
    for(int i = 0; i < n; i++){
        bit.add(ind[b[i] - 1], 1);
        cnt += bit.sum(n) - bit.sum(ind[b[i] - 1] + 1);
    }
    cout << cnt << endl;
}