Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- constexpr int NMAX = 1e5 + 3;
- int n, arr1[NMAX], arr2[NMAX];
- int posOfElemIn2nd[NMAX], posOfIthFrom1stIn2nd[NMAX];
- void readArr(int arr[NMAX]) {
- for (int i = 1; i <= n; i++) {
- scanf("%d", arr + i);
- }
- }
- struct SegTree {
- int tree[4 * NMAX];
- void update(int node, int le, int ri, int val) {
- if (le == ri) {
- tree[node]++;
- return;
- }
- int mid = (le + ri) / 2;
- int leSon = node * 2, riSon = node * 2 + 1;
- if (val <= mid) {
- update(leSon, le, mid, val);
- } else {
- update(riSon, mid + 1, ri, val);
- }
- tree[node] = tree[leSon] + tree[riSon];
- }
- int query(int node,int le, int ri, int qle, int qri) {
- if (qle > ri || qri < le) {
- return 0;
- }
- if (qle <= le && ri <= qri) {
- return tree[node];
- }
- int mid = (le + ri) / 2;
- int leSon = node * 2, riSon = node * 2 + 1;
- int leAns = query(leSon, le, mid, qle, qri);
- int riAns = query(riSon, mid + 1, ri, qle, qri);
- return leAns + riAns;
- }
- };
- int main() {
- scanf("%d", &n);
- readArr(arr1);
- readArr(arr2);
- for (int i = 1; i <= n; i++) {
- posOfElemIn2nd[arr2[i]] = i;
- }
- for (int i = 1; i <= n; i++) {
- posOfIthFrom1stIn2nd[i] = posOfElemIn2nd[arr1[i]];
- }
- int ans = 0;
- SegTree segTree;
- for (int i = n; i > 0; i--) {
- ans += segTree.query(1, 1, n, 1, posOfIthFrom1stIn2nd[i]);
- segTree.update(1, 1, n, posOfIthFrom1stIn2nd[i]);
- }
- printf("%d\n", ans);
- return 0;
- }
Advertisement
Advertisement