#P5062. [Ynoi Easy Round 2014] 在太阳西斜的这个世界里
[Ynoi Easy Round 2014] 在太阳西斜的这个世界里
Description
Chtholly needs to maintain a red-black tree that is initially empty, and perform operations.
For duplicate integers, we consider the one inserted later to be smaller.
There are two types of operations:
- : Insert an integer into the red-black tree.
- : Given , suppose we choose an integer uniformly at random from to and insert it into the red-black tree. Query the expected number of rotations caused by this insertion. For convenience, you only need to output (it can be proven that this is an integer).
Input Format
The first line contains an integer .
The next lines each contain two integers separated by a space, representing one operation.
Output Format
For each operation with , output one line containing one integer, which is the answer.
10
2 5
1 3
1 4
2 3
2 4
2 5
1 4
2 3
2 4
2 5
0
0
2
3
0
0
0
Hint
A red-black tree is a binary search tree where each node has a color (red/black).
Each time an insertion is performed, the newly inserted node is red, and the insertion process is the same as in a normal binary search tree.
If two red nodes become a parent-child pair, adjust as shown in the figure (the operations are left-right symmetric, and symmetric cases are not repeated in the figure; the black triangle represents null or a subtree rooted at a black node). The adjustment may need to be performed multiple times in a row.
After the adjustment, set the root to black.

Idea: ccz181078, Solution: ccz181078, Code: ccz181078, Data: ccz181078
Constraints: ,,。
Translated by ChatGPT 5
京公网安备 11011102002149号