結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー 👑 CleyL
提出日時 2022-02-20 11:40:39
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 930 bytes
コンパイル時間 622 ms
コンパイル使用メモリ 70,912 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-29 10:45:54
合計ジャッジ時間 3,627 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
template<typename T>
struct BIT{//1_Indexed
  int n;
  vector<T> bit;
  BIT(int n_):n(n_+1),bit(n,0){}

  T sum(int a){
    int ret = 0;
    for(int i = a; i > 0; i -= i & -i) ret += bit[i];
    return ret;
  }

  void add(int a,int w){
    for(int i = a; i <= n; i += i & -i)bit[i] += w;
  }

  T query(int r,int l){
    return sum(l-1)-sum(r-1);
  }

};
int main(){
    int n;cin>>n;
    vector<int> A(n);
    for(int i = 0; n > i; i++){
        int t;cin>>t;t--;
        A[t] = i;//A[2] = 4
    }
    vector<int> B(n);
    for(int i = 0; n > i; i++){
        int x;cin>>x;
        x--;
        B[i] = A[x]+1;
        
    }

    BIT<long long> C(n+1);
    long long ans = 0;
    for(int i = 0; n > i; i++){
        //cout << B[i] << " " << n << " " << C.query(B[i],n+1) << endl;
        ans += C.query(B[i],n+1);
        C.add(B[i],1);
    }
    cout << ans << endl;
}
0