## DMOPC '19 Contest 7 P7 - Fluid Dynamics

View as PDF

Points: 35 (partial)
Time limit: 5.0s
Memory limit: 256M

Authors:
Problem types

You have discovered a new type of fluid! You're extra excited because it comes in many different colours (which means more marketing potential, of course). It can be shown that these fluids can be mainly described by two characteristics: its volume and its colour. While messing around performing rigorous experiments, you have discovered that when you place two of these fluids together, one on top of the other, sometimes they will swap places! In particular, you discover that if you place a fluid of volume and colour on top of another fluid of volume and colour , the pair will have a potential, , of . If the change in potential, (defined as ), when the two fluids exchange places is greater than or equal to (a value you've already determined), then the fluids will swap places. Otherwise, they will return to their original configuration (i.e. no swapping occurs).

As you are very interested in this process, you've decided to place a bunch of these fluids in a jar and see the result. Because of how the swapping process works, every time you place a new fluid into the jar, that fluid will keep exchanging places with the fluid directly below it in the jar, as long as it satisfies the swapping condition described above.

However, as doing this manually is incredibly laborious and you're very lazy busy, you've decided to create a program to simulate the results instead!

#### Input Specification

The first line will contain three integers, , , and .

The next lines will contain two integers, and . Each pair corresponds to a fluid to insert. Place these fluids one by one in the given order at the top of the jar.

The next lines will each contain a query. There are two types of queries:

• INSERT v c means to place a fluid of volume v and colour c at the top of the jar.
• K-TH k means to output the fluid's (counting from the bottom of the jar, starting from 1) characteristics (its volume and colour) separated by a single space.

Fluids do not combine! (Even if they are adjacent and have the same colour.)

#### Output Specification

For every query of type K-TH, output that fluid's volume and colour separated by a space on a new line.

#### Constraints

It is guaranteed that K-TH queries will refer to a valid index in the jar (i.e., greater than 1 and less than equal to the current number of fluids present in the jar).

5 5 7
15 2
20 12
7 16
7 12
20 3
K-TH 2
INSERT 1 10
INSERT 8 1
INSERT 9 1
K-TH 6

20 3
7 12

#### Explanation of Sample Output 1

For this sample input, .

We start by placing the fluid into the jar. Then we place fluid into the jar. Before exchanging places, the potential of this pair is . After exchanging, the potential will be . Since , the two fluids will not swap places.

We then place the third fluid, into the jar. The change in potential between and is , so and will not swap places.

We then place the fourth fluid, into the jar. The change in potential between and is , so and will swap places. However, change in potential between and the next fluid down, , is , so will not swap with .

Finally, for the fifth fluid, , this is the chain of events that will happen:

Fluid Directly Below Will Swap Places?
Yes
Yes
Yes
No

Thus, for the first query, the second fluid from the bottom is . For the last query, the jar will look like:

1 10
7 16
7 12
20 12
9 1
8 1
20 3
15 2

So the fluid from the bottom would be .

10 10 25
41 12
35 7
18 49
25 47
19 47
28 25
4 6
8 29
30 24
35 26
INSERT 22 43
INSERT 2 19
INSERT 20 42
K-TH 7
K-TH 2
K-TH 10
K-TH 3
K-TH 11
INSERT 10 30
K-TH 8

25 47
41 12
19 47
35 26
18 49
22 43