#P5062. [Ynoi Easy Round 2014] 在太阳西斜的这个世界里

[Ynoi Easy Round 2014] 在太阳西斜的这个世界里

Description

Chtholly needs to maintain a red-black tree that is initially empty, and perform nn operations.

For duplicate integers, we consider the one inserted later to be smaller.

There are two types of operations:

  • op=1op=1: Insert an integer xx into the red-black tree.
  • op=2op=2: Given xx, suppose we choose an integer uniformly at random from 11 to xx 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 expected value×x\text{expected value} \times x (it can be proven that this is an integer).

Input Format

The first line contains an integer nn.

The next nn lines each contain two integers op,xop,x separated by a space, representing one operation.

Output Format

For each operation with op=2op=2, 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: 1n2×1051\leq n\leq 2\times 10^51op21\leq op\leq 21x1081\leq x\leq 10^8

Translated by ChatGPT 5