#P9867. [POI 2021/2022 R2] kon

[POI 2021/2022 R2] kon

Background

Translated from POI2021~2022R2 Day2T2

Problem Description

There is a dance party. At the beginning, there are only persons 11 and 22, and both of them are willing to dance with each other.

Then there are qq events, corresponding to the following operations:

  • W x: A new person joins. They and person xx are mutually willing to dance with each other.
  • Z x: A new person joins. Initially, they are mutually willing to dance with everyone that person xx is willing to dance with.
  • ? x: Query how many people are willing to dance with xx.

The new person’s index is the current number of people plus one.

Input Format

The first line contains an integer q (1q106)q\ (1 \leq q \leq 10^6).

Then follow qq lines. Each line contains a character and an integer xx, with the meaning as described above.

Output Format

For each ? operation, output one line with the answer.

7
? 1
Z 2
? 1
Z 1
W 2
? 2
? 3
1
2
3
2

Hint

Sample explanation:

Subtasks:

Subtask ID Special property Score
11 q5000q \leq 5000 2020
22 Only operations Z and ? 1010
33 ? always appears in the final part of the qq operations 3535
44 No additional constraints

Translated by ChatGPT 5