Home BOJ. Factory (7578)
Post
Cancel

BOJ. Factory (7578)

[Link] https://www.acmicpc.net/problem/7578


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define endl "\n"
typedef long long ll;
const int INF = 987654321;
const int MAX = 100;
const int N = 5e5;
int n;
vector<int> seg(2*N);
vector<int> idx_vec;

void insert(int val) {
  int idx = N + val; seg[idx]++;
  while(idx > 1) {
    idx >>= 1;
    seg[idx] = seg[idx << 1] + seg[idx << 1 | 1];
  }
}

int get(int val) {
  int ans = 0;
  for(int l = N + val, r = 2 * N; l < r; l>>=1, r>>=1) {
    if(l & 1) ans+=seg[l++];
    if(r & 1) ans+=seg[--r];
  }
  return ans;
}

void solve() {
  ll ans = 0;
  for(int e: idx_vec) {
    ans += get(e);
    insert(e);
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin >> n;
  idx_vec.resize(n);
  map<int, int> number_idx;
  for0(i, n) {
    int in; cin >> in;
    number_idx[in] = i;
  }
  for0(i, n) {
    int in; cin >> in;
    idx_vec[number_idx[in]] = i;
  }
  solve();
}
This post is licensed under CC BY 4.0 by the author.