This page is still under construction.

Parts of this page are still being built. What you see may change.

Similarity

Time limit1sMemory limit1024 MB

Summary
Given two length-n sequences p and q, count triples i<j<k with p_i<p_j<p_k and q_i<q_j<q_k.
Level

Hard8 of 10

Topics
Divide and conquer, Sorting, Segment tree, Combinatorics
Solved
No attempts yet

Problem

In modern application systems, recommendation systems are very widely used to recommend books, music, ads, items, and so on. A recommendation system needs to attract other users by providing the most interesting items to each user. One way of recommending is to find the user most similar to the current user, then recommend the items that the most similar user purchased to the current user. To help the recommendation system, we design a similarity measure.

A user is represented by a sequence p=p1,p2,…,pnp = p_1, p_2, \dots, p_n, where nn is the number of items. It denotes a list of the preference magnitudes of the user for items. Given two sequences p=p1,p2,…,pnp = p_1, p_2, \dots, p_n and q=q1,q2,…,qnq = q_1, q_2, \dots, q_n, a similar tuple is defined as a tuple (i,j,k)(i, j, k) such that pi<pj<pkp_i < p_j < p_k and qi<qj<qkq_i < q_j < q_k. For given two sequences p=p1,p2,…,pnp = p_1, p_2, \dots, p_n and q=q1,q2,…,qnq = q_1, q_2, \dots, q_n, the similarity is defined as the number of similar tuples.

For example, if the given two sequences are p=2,5,9,5,1p = 2, 5, 9, 5, 1 and q=1,4,5,3,2q = 1, 4, 5, 3, 2, the similar tuples are (1,2,3)(1, 2, 3), (1,4,3)(1, 4, 3), (5,2,3)(5, 2, 3), and (5,4,3)(5, 4, 3), and the similarity of the two sequences is 44.

Given two sequences p=p1,p2,…,pnp = p_1, p_2, \dots, p_n and q=q1,q2,…,qnq = q_1, q_2, \dots, q_n, write a program to output their similarity.

Input

Your program reads from standard input. The input starts with a line containing one integer nn (1≤n≤100,0001 \le n \le 100,000), where nn is the length of a sequence. In the following two lines, each line contains nn integers in the range [0,106][0, 10^6] that represent a sequence.

Output

Your program writes to standard output. Print exactly one line. The line should contain the similarity of the two sequences.

Examples2

  1. Example 1

    Input
    5
    2 5 9 5 1
    1 4 5 3 2
    
    Expected output
    4
    
  2. Example 2

    Input
    4
    3 2 1 1
    3 2 1 1
    
    Expected output
    2