Similarity
Time limit1sMemory limit1024 MB
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 , where is the number of items. It denotes a list of the preference magnitudes of the user for items. Given two sequences and , a similar tuple is defined as a tuple such that and . For given two sequences and , the similarity is defined as the number of similar tuples.
For example, if the given two sequences are and , the similar tuples are , , , and , and the similarity of the two sequences is .
Given two sequences and , write a program to output their similarity.
Input
Your program reads from standard input. The input starts with a line containing one integer (), where is the length of a sequence. In the following two lines, each line contains integers in the range 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.