#P16704. [SEATST 2026] 国家排行 / Country Ranks

[SEATST 2026] 国家排行 / Country Ranks

Problem Description

There are NN students participating in the SEATST contest. Each student represents a country. After the contest ends, all students receive distinct scores.

Prabowo is going to publish the ranking table on the official website. For each student, the ranking table lists their country, score, global rank, and country rank.

  • A student's global rank is defined as the number of people whose score is higher than this student's score.
  • A student's country rank is defined as the number of people who are from the same country as this student and whose score is higher than this student's score.

An example ranking table is as follows:

Country Score Global Rank Country Rank
Singapore 574574 00 00
Malaysia 483483 11
Singapore 466466 22 11
Indonesia 460460 33 00
Singapore 458458 44 22
Malaysia 454454 55 11
Singapore 448448 66 33
Malaysia 440440 77 22
Indonesia 438438 88 11

Note that both global rank and country rank start from 00, and the ranks never skip any numbers (for both global rank and country rank).

However, when the ranking table was uploaded online, Prabowo forgot to publish the students' countries and scores. For each student, we only know their global rank and country rank.

Prabowo tries to save the situation, and gives you a task to help him compute the following two quantities:

  • the number of pairs of students that must belong to the same country, and
  • the number of pairs of students that must belong to different countries.

:::warning[Warning]{open} If there exist two assignments consistent with the global ranks and country ranks such that two students are in the same country in one assignment, but in different countries in the other assignment, then this pair of students should not be counted in either of the two quantities above. :::

Please help Prabowo.

Implementation Details

You need to implement the following two functions.

long long count_same_country(int N, std::vector<int> country_rank)

long long count_diff_country(int N, std::vector<int> country_rank)
  • NN: the number of students.
  • country_rank: an array of length NN representing the country ranks. For all 0≤i≤N−10 \le i \le N - 1, country_rank[i] is the country rank of the student whose global rank is ii.

The first function should return the number of unordered pairs of distinct students such that, in all country assignments consistent with the ranking table, the two students always belong to the same country.

The second function should return the number of unordered pairs of distinct students such that, in all country assignments consistent with the ranking table, the two students always belong to different countries.

In each testdata, each of these two functions will be called at most once.

Input Format

The input format is:

N X
C[0] C[1] ... C[N-1]

Here, X can be the string same or diff, forming a call to the function count_X_country. For all 0≤i≤N−10 \le i \le N - 1, C[i] denotes country_rank[i].

Output Format

Output one integer, the return value of count_X_country.

Hint

Samples

Consider the following function call:

count_same_country(9, [0, 0, 1, 0, 2, 1, 3, 2, 1])

Assume that students 00, 11, and 33 (for convenience, here we number students by their global ranks) represent Singapore, Malaysia, and Indonesia respectively.

Then, the table below lists all assignments that can produce these ranks:

Global Rank Country Rank Assignment 1 Assignment 2 Assignment 3 Assignment 4
00 00 Singapore
11 Malaysia Malaysia
22 11 Singapore
33 00 Indonesia
44 22 Singapore Malaysia
55 11 Malaysia Indonesia Singapore Indonesia
66 33 Singapore Malaysia
77 22 Malaysia Indonesia Singapore Indonesia
88 11 Indonesia Malaysia Indonesia Singapore

There are 44 pairs of students that must always belong to the same country: (2,4)(2, 4), (2,6)(2, 6), (4,6)(4, 6), and (5,7)(5, 7). Therefore, this function should return 44.

count_diff_country(9, [0, 0, 1, 0, 2, 1, 3, 2, 1])

There are 1717 pairs of students that must always belong to different countries: (0,1)(0, 1), (0,3)(0, 3), (1,3)(1, 3), (2,3)(2, 3), (2,5)(2, 5), (2,7)(2, 7), (2,8)(2, 8), (3,4)(3, 4), (3,6)(3, 6), (4,5)(4, 5), (4,7)(4, 7), (4,8)(4, 8), (5,6)(5, 6), (5,8)(5, 8), (6,7)(6, 7), (6,8)(6, 8), (7,8)(7, 8). Therefore, this function should return 1717.

count_same_country(5, [0, 1, 0, 1, 2])

Here there are 22 pairs of students that must always belong to the same country: (0,1)(0, 1) and (2,3)(2, 3). Therefore, this function should return 22.

count_diff_country(5, [0, 1, 0, 1, 2])

There are 44 pairs of students that must belong to two different countries: (0,2)(0, 2), (0,3)(0, 3), (1,2)(1, 2), (1,3)(1, 3). Therefore, this function should return 44.

Constraints

  • 1≤N≤1 000 0001 \le N \le 1\ 000\ 000.
  • It is guaranteed that there exists at least one country assignment satisfying country_rank.

Subtasks

For the first 66 subtasks, only count_same_country will be called.

  1. (33 points) N≤8N \le 8.
  2. (66 points) country_rank contains at most two 00.
  3. (66 points) country_rank does not contain 22.
  4. (33 points) N≤300N \le 300.
  5. (33 points) N≤2000N \le 2000.
  6. (99 points) No additional constraints.

For the last 66 subtasks, only count_diff_country will be called.

  1. (77 points) N≤8N \le 8.
  2. (1414 points) country_rank contains at most two 00.
  3. (1414 points) country_rank does not contain 22.
  4. (77 points) N≤300N \le 300.
  5. (77 points) N≤2000N \le 2000.
  6. (2121 points) No additional constraints.

Translated by ChatGPT 5