結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー renjyaku_int
提出日時 2020-08-30 16:43:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 2,916 bytes
コンパイル時間 2,038 ms
コンパイル使用メモリ 195,192 KB
最終ジャッジ日時 2025-01-14 00:04:00
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

//#include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const ll INF = 1e9;
const ll LINF = ll(1e18);
const ll MOD = 1000000007;
const ll dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const ll Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (ll i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//-std=gnu++17
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
struct fenwick_tree
{
    typedef ll T;
    T n;
    vector<T> bit;
    T b = 1;
    // 各要素の初期値は 0
    fenwick_tree(T num) : bit(num + 1, 0) { n = num; }

    // a_i += w
    void add(T i, T w)
    {
        for (T x = i; x <= n; x += x & -x)
        {
            bit[x] += w;
        }
    }
    ll get(T i)
    {
        ll s = 0;
        while (i > 0)
        {
            s += bit[i];
            i -= i & -i;
        }
        return s;
    }
    // [1, i] の和を計算.
    T sum(T i)
    {
        T ret = 0;
        for (T x = i; x > 0; x -= x & -x)
        {
            ret += bit[x];
        }
        return ret;
    }
    // [left+1, right] の和を計算.
    T sum(T left, T right)
    {
        return sum(right) - sum(left);
    }
    ll get_index(ll x)
    { //something like lower_bound(all(sum), a). O(logn)
        x--;
        while (b * 2 <= n)
            b *= 2;
        ll r = b;
        ll res = 0;
        while (r > 0)
        {
            if ((r | res) <= n)
            {
                ll rv = bit[r | res];
                if (x >= rv)
                {
                    x -= rv;
                    res += r;
                }
            }
            r >>= 1;
        }
        return res + 1;
    }
};
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    vector<ll>a(n),b(n);
    rep(i,n)cin>>a[i];
    rep(i,n)cin>>b[i];
    vector<ll> ind(n);
    rep(i,n){
        a[i]--;
        b[i]--;
        ind[b[i]]=i;
    }
    //debug(ind);
    rep(i,n){
        a[i]=ind[a[i]]+1;
    }
    fenwick_tree ft(n+1);
    //debug(a);
    ll ans=0;
    rep(i,n){
        ft.add(a[i], 1);
        ans+=i+1-ft.sum(a[i]);
        //cout<<ans<<"\n";
    }
    cout<<ans<<"\n";
}
0