結果
| 問題 |
No.1115 二つの数列 / Two Sequences
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-01-11 13:22:00 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 138 ms / 2,000 ms |
| コード長 | 1,169 bytes |
| コンパイル時間 | 2,181 ms |
| コンパイル使用メモリ | 200,868 KB |
| 最終ジャッジ日時 | 2025-01-17 16:08:13 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 35 |
ソースコード
typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
struct fenwick_tree {
typedef int T;
T n;
vector<T> bit;
// 各要素の初期値は 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;
}
}
// [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);
}
};
int main() {
ll n;
std::cin >> n;
vector<ll> a(n),b(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
map<ll,ll> m;
for (int i = 0; i < n; i++) {
std::cin >> b[i];
m[b[i]] = i+1;
}
for (int i = 0; i < n; i++) {
a[i] = m[a[i]];
}
fenwick_tree f_tree(n);
ll ans = 0;
for (int j = 0; j < n; j++) {
ans += j - f_tree.sum(a[j]);
f_tree.add(a[j], 1);
}
std::cout << ans << std::endl;
}