#P15595. [ICPC 2020 Jakarta R] Shopping Changes

[ICPC 2020 Jakarta R] Shopping Changes

题目描述

Felix and his MM friends are having a shopping frenzy today. From a recent cash transaction, he received a change with NN banknotes. Felix would like to slip the banknotes he received into his wallet without changing their order.

For example, let’s assume that Felix received a change with N=4N = 4 banknotes in the following order: C1 C2 C3 C4C_1\ C_2\ C_3\ C_4. Supposed there are 33 banknotes in Felix’s wallet in the following order: W1 W2 W3W_1\ W_2\ W_3, then there are four possible ways to slip the change into Felix’s wallet.

  1. Slip the change before the 1st1^\text{st} banknote. After slipping the change, the banknotes in his wallet are in the following order: C1 C2 C3 C4 W1 W2 W3C_1\ C_2\ C_3\ C_4\ W_1\ W_2\ W_3.
  2. Slip the change between the 1st1^\text{st} and 2nd2^\text{nd} banknotes. After slipping the change, the banknotes in his wallet are in the following order: W1 C1 C2 C3 C4 W2 W3W_1\ C_1\ C_2\ C_3\ C_4\ W_2\ W_3.
  3. Slip the change between the 2nd2^\text{nd} and 3rd3^\text{rd} banknotes. After slipping the change, the banknotes in his wallet are in the following order: W1 W2 C1 C2 C3 C4 W3W_1\ W_2\ C_1\ C_2\ C_3\ C_4\ W_3.
  4. Slip the change after the 3rd3^\text{rd} banknote. After slipping the change, the banknotes in his wallet are in the following order: W1 W2 W3 C1 C2 C3 C4W_1\ W_2\ W_3\ C_1\ C_2\ C_3\ C_4.

Being a tidy person, Felix would like the banknotes in his wallet to be as sorted as possible. Therefore, he would like to slip the change in a way that minimizes the inversion count of his wallet after the slip. The inversion count of a wallet is the number of pairs of banknotes (x,y)(x, y) in the wallet such that:

  • banknote xx is closer to the front than banknote yy, and
  • banknote xx has strictly more value than banknote yy.

Feeling a bit generous today, instead of keeping the change, Felix would like to give them to one of his friends and slip the change into their wallet. The ithi^\text{th} friend has a wallet containing LiL_i banknotes which values are Wi[1],Wi[2],,Wi[Li]W_i[1], W_i[2], \dots, W_i[L_i] respectively from the front-most to the back-most of the wallet. Felix would like to give the change to his friend that can minimize the inversion count of their wallet after slipping the change. Therefore, for each of his friends, Felix needs to count the minimum inversion count of their wallet if he slips the change into their wallet.

输入格式

Input begins with a line containing two integers: N MN\ M (1N,M1000001 \leq N, M \leq 100\,000) representing the number of banknotes in the change and the number of Felix’s friends, respectively. The next line contains NN integers: CiC_i (1Ci1091 \leq C_i \leq 10^9) representing the value of the banknotes in the change. The next MM lines each begin with an integer LiL_i (1Li2000001 \leq L_i \leq 200\,000) representing the number of banknotes in the ithi^\text{th} friend’s wallet, followed by LiL_i integers: Wi[j]W_i[j] (1Wi[j]1091 \leq W_i[j] \leq 10^9) representing the value of the banknotes in the wallet. The sum of all LiL_i is not more than 200000200\,000.

输出格式

For each friend in the same order as input, output in a line an integer representing the minimum inversion count of their wallet if Felix slips the change into their wallet.

3 3
5 6 7
6 2 3 4 8 9 10
2 100 99
3 5 6 7
0
1
1
3 2
7 6 5
6 2 3 4 8 9 10
6 10 9 8 4 3 2
3
27

提示

Explanation for the sample input/output #1

For the 1st1^\text{st} friend, by slipping the change between the 3rd3^\text{rd} and 4th4^\text{th} banknotes, the banknotes in the wallet are in the following order: 2 3 4 5 6 7 8 9 102\ 3\ 4\ 5\ 6\ 7\ 8\ 9\ 10. Therefore, the inversion count of the wallet is 00.

For the 2nd2^\text{nd} friend, by slipping the change before the 1st1^\text{st} banknote, the banknotes in the wallet are in the following order: 5 6 7 100 995\ 6\ 7\ 100\ 99. Therefore, the inversion count of the wallet is 11. There is no way to achieve a smaller inversion count.

For the 3rd3^\text{rd} friend, by slipping the change between the 1st1^\text{st} and 2nd2^\text{nd} banknotes, or between the 2nd2^\text{nd} and 3rd3^\text{rd} banknotes, the inversion count of the wallet is 11. There is no way to achieve a smaller inversion count.