結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー renjyaku_intrenjyaku_int
提出日時 2020-08-30 16:43:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 2,916 bytes
コンパイル時間 2,290 ms
コンパイル使用メモリ 201,096 KB
実行使用メモリ 6,292 KB
最終ジャッジ日時 2023-08-09 15:24:23
合計ジャッジ時間 4,487 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 21 ms
5,700 KB
testcase_04 AC 24 ms
6,224 KB
testcase_05 AC 21 ms
5,680 KB
testcase_06 AC 18 ms
5,496 KB
testcase_07 AC 25 ms
6,224 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 14 ms
4,816 KB
testcase_10 AC 22 ms
6,224 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 24 ms
6,140 KB
testcase_13 AC 24 ms
6,288 KB
testcase_14 AC 24 ms
6,292 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 9 ms
4,380 KB
testcase_25 AC 18 ms
5,512 KB
testcase_26 AC 6 ms
4,380 KB
testcase_27 AC 12 ms
4,380 KB
testcase_28 AC 15 ms
4,808 KB
testcase_29 AC 20 ms
5,432 KB
testcase_30 AC 23 ms
5,960 KB
testcase_31 AC 7 ms
4,376 KB
testcase_32 AC 5 ms
4,376 KB
testcase_33 AC 21 ms
5,664 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,384 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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