DMOPC '19 Contest 7 P5 - Soriya's Programming Project

View as PDF

Submit solution


Points: 20 (partial)
Time limit: 3.0s
Memory limit: 128M

Author:
Problem types

For her current programming project, Soriya needs to first construct the array a which has length N. However, to entertain herself, she decides to add the elements in the order described by the permutation p. That is, for her ith insertion, she will add the element api to its position in the array. Since Soriya has nothing better to do with her life, she wonders: how many inversions will the array a have after each insertion? An inversion is a pair of indices (i,j) such that 1i<jN and ai>aj.

Input Specification

The first line contains the positive integer N.
The second line consists of N positive integers, the array a1,a2,,aN.
The third line consists of N positive integers, the permutation p1,p2,,pN.

Output Specification

Output N lines, the ith of which containing a single integer, the number of inversions after Soriya adds her ith element.

Constraints

In all subtasks,
1N500000
1ai,piN

Subtask 1 [15%]

pi=i for 1iN

Subtask 2 [25%]

1ai2

Subtask 3 [60%]

No additional constraints.

Sample Input 1

Copy
5
4 3 1 2 3
1 2 3 4 5

Sample Output 1

Copy
0
1
3
5
6

Sample Input 2

Copy
5
4 3 1 2 3
3 5 1 4 2

Sample Output 2

Copy
0
0
2
3
6

Comments

There are no comments at the moment.