結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー cn_449cn_449
提出日時 2020-07-17 21:33:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 2,290 bytes
コンパイル時間 1,021 ms
コンパイル使用メモリ 114,224 KB
実行使用メモリ 6,772 KB
最終ジャッジ日時 2023-08-19 23:57:43
合計ジャッジ時間 3,235 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 28 ms
6,336 KB
testcase_04 AC 31 ms
6,628 KB
testcase_05 AC 28 ms
6,396 KB
testcase_06 AC 25 ms
6,164 KB
testcase_07 AC 30 ms
6,604 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 17 ms
4,764 KB
testcase_10 AC 28 ms
6,604 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 31 ms
6,628 KB
testcase_13 AC 32 ms
6,772 KB
testcase_14 AC 31 ms
6,760 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 12 ms
4,776 KB
testcase_25 AC 24 ms
6,132 KB
testcase_26 AC 6 ms
4,376 KB
testcase_27 AC 14 ms
4,804 KB
testcase_28 AC 19 ms
5,152 KB
testcase_29 AC 25 ms
6,132 KB
testcase_30 AC 30 ms
6,624 KB
testcase_31 AC 9 ms
4,376 KB
testcase_32 AC 6 ms
4,376 KB
testcase_33 AC 26 ms
6,368 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << x << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

template<class T> class segtree {
	int n;
	vector<T> data;
	T id = 0;
	T operation(T a, T b) { return a + b; };
public:
	segtree(int _n) {
		n = 1;
		while (n < _n + 2) n <<= 1;
		data = vector<T>(2 * n, id);
	}
	void change(int i, T x) {
		i += n;
		data[i] = x;
		while (i > 1) {
			i >>= 1;
			data[i] = operation(data[i << 1], data[i << 1 | 1]);
		}
	}
	void add(int i, T x) { change(i, data[i + n] + x); }
	T get(int a, int b) {
		T left = id; T right = id;
		a += n; b += n;
		while (a < b) {
			if (a & 1) left = operation(left, data[a++]);
			if (b & 1) right = operation(data[--b], right);
			a >>= 1; b >>= 1;
		}
		return operation(left, right);
	}
	T get_all() { return data[1]; }
	T operator[](int i) { return data[i + n]; }
};

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);

	int n;
	cin >> n;
	vector<int> a(n), b(n), ainv(n), p(n);
	rep(i, n) {
		cin >> a[i];
		a[i]--;
		ainv[a[i]] = i;
	}
	rep(i, n) cin >> b[i], b[i]--;
	rep(i, n) p[i] = ainv[b[i]];
	segtree<ll> seg(n);
	ll ans = 0;
	rep(i, n) {
		ans += seg.get(p[i] + 1, n + 1);
		seg.add(p[i], 1);
	}
	cout << ans << '\n';
}
0