CCCGuide

Problem S3: Pretty Pens

2025 · S3 · Original problem · DMOJ

Problem Description

You are taking an art class, and your current art assignment is very algorithmic.

You have NN pens, each of which has a single colour, represented by an integer from 11 to MM. Initially, the colour of the ii-th pen is given by CiC_i. In addition, your pens are pretty, with the ii-th pen having a prettiness of PiP_i.

For your assignment, you need to create a picture using MM strokes, each from a pen of a different colour. The prettiness of your picture is the sum of the prettiness of the pens used to create the picture.

Your teacher has given you some room for artistic expression: before you create this pretty picture, you are allowed to change at most one pen to any other colour. After this picture is drawn, the pen will revert back to its original colour.

Your teacher will give you 13\frac{1}{3} of the marks (55 of 1515 total marks) for the art assignment based on creating the prettiest picture you can.

To push your creative limits, your teacher also has QQ more pictures for you to create, which compose the remaining 23\frac{2}{3} of the marks (1010 of 1515 total marks) for your art assignment. Before each picture, there will be one of two possible changes to the set of pens available:

The changes are executed sequentially (so the first change modifies the initial setup, the second change modifies the result of applying the first change, and so on).

As in the first picture you created, you are allowed to change the colour of at most one pen before the picture is created. Note that if you do change the colour of one pen, it affects only the next picture you draw, and the pen will revert to its previous colour before the next change is applied (if any).

What is the prettiness of the prettiest Q+1Q+1 pictures you can create?

Input Specification

The first line of input contains three space-separated integers NN, MM, QQ (1≤M≤N≤200 000(1 \leq M \leq N \leq 200\ 000, 0≤Q≤200 000)0 \leq Q \leq 200\ 000).

The next NN lines each contain two space-separated integers, CiC_i and PiP_i (1≤Ci≤M,1≤Pi≤109)(1 \le C_i \le M, 1 \le P_i \le 10^9), indicating the colour and prettiness of the ii-th pen.

The next QQ lines each contain three space-separated integers, beginning with 11 or 22. If the first integer is 11, it is followed by two integers iji_j and cjc_j (1≤ij≤N,1≤cj≤M)(1 \le i_j \le N, 1 \le c_j \le M), representing a change of the colour of the iji_j-th pen to cjc_j. If the first integer is 22, it is followed by two integers iji_j and pjp_j (1≤ij≤N,1≤pj≤109)(1 \le i_j \le N, 1 \le p_j \le 10^9), representing a change of the prettiness of the iji_j-th pen to pjp_j.

It is guaranteed that initially and after each change, there is at least one pen with each of the MM colours.

The following table shows how the available 1515 marks are distributed:

MarksBounds on NN and MMBounds on QQAdditional constraints
551≤M≤N≤200 0001 \le M \le N \le 200\ 000Q=0Q = 0None
22M=1M=1; 1≤N≤200 0001 \le N \le 200\ 0000≤Q≤200 0000 \le Q \le 200\ 000None
22M=2M=2; 2≤N≤200 0002 \le N \le 200\ 0000≤Q≤200 0000 \le Q \le 200\ 000None
22M≤10M \le 10; 1≤M≤N≤200 0001 \le M \le N \le 200\ 0000≤Q≤200 0000 \le Q \le 200\ 000None
221≤M≤N≤200 0001 \le M \le N \le 200\ 0000≤Q≤200 0000 \le Q \le 200\ 000All prettiness values are distinct
221≤M≤N≤200 0001 \le M \le N \le 200\ 0000≤Q≤200 0000 \le Q \le 200\ 000None

Output Specification

The output is Q+1Q+1 lines, with the jj-th line containing one integer, the largest possible prettiness value obtainable after the first j−1j-1 changes have been performed.

Sample Input 1

6 3 0
1 6
2 9
3 4
2 7
3 9
1 3

Output for Sample Input 1

25

Explanation of Output for Sample Input 1

There are six pens available in three different colours. The set of pens is:

If we do not change the colour of any pens, the prettiest picture has prettiness 6+9+9=246+9+9=24. However, if we change the colour of pen 44 from colour 22 to colour 11, we can create a picture with prettiness 7+9+9=257+9+9=25, which is the prettiest picture that can be created.

Sample Input 2

3 2 2
1 20
2 30
1 10
1 3 2
2 3 25

Output for Sample Input 2

50
50
55

Explanation of Output for Sample Input 2

There are three pens with two different colours available.

Before the first change, using pen 11 and pen 22, with colours 11 and 22, respectively, achieves a prettiness of 20+30=5020+30=50.

After the first change, pen 33 has colour 22. There is no alteration in the maximum prettiness, even if we could switch one pen: picking pens 11 and 22 will yield the maximum prettiness.

After the second change, pen 33 will have colour 22 and prettiness 2525. Before the final picture is created, we can change the colour of pen 22 to colour 11, and use pens 22 and 33 to achieve the maximum prettiness of 30+25=5530+25=55.