DMOPC '19 Contest 3 P5 - Festival

View as PDF

Submit solution

Points: 20
Time limit: 2.5s
Memory limit: 256M

Author:
Problem type

To prepare for his village's annual festival, Rexzae plans on putting on a display of fireworks. According to the village's spiritual beliefs, each firework is associated with a particular number, which they call its "colour". Currently, he has already set up N of these fireworks, the ith of which has "colour" ci. In the following Q minutes, one of three events may occur. On the jth minute, Rexzae may add a new firework that has colour dj, he may decide to adjust all of the fireworks he currently has set up by adding aj to all of their colours, or he might want to know the minimum cost for setting up a fireworks display that has a festivity of kj. The cost for this fireworks display is the minimum number of moves that it will take for Rexzae to make all of his fireworks have a colour that is a multiple of kj. (Here, a move is defined as adding or subtracting 1 from the colour of a single firework.) Since Rexzae is greedy, each subsequent query of the third type must have a strictly larger value of kj than the ones that came before.

Constraints

In all subtasks,
1N,Q300000
106aj106
1012ci,dj1012
1kj1012
The range of the colours of the different fireworks will not exceed 200000 at any time.

Subtask 1 [20%]

1N,Q2000

Subtask 2 [30%]

There are no queries of the first or second type.

Subtask 3 [50%]

No additional constraints.

Input Specification

The first line contains two space-separated integers, N and Q.
The second line contains N space-separated integers, c1,c2,,cN.
Q lines will follow, each of which will be in the form of one of the following:

1 d - Rexzae adds a new firework with colour d.
2 a - Rexzae adds a to the colour of all of his fireworks.
3 k - Rexzae wants to know the minimum cost for setting up a fireworks display that has a festivity of k.

Note: Rexzae does not actually change the colours of his fireworks during a k operation.

Output Specification

For each query of the third type, output the minimum cost for setting up a fireworks display that has a festivity of kj.

Sample Input

Copy
5 9
1 3 5 7 9
3 1
3 2
2 -3
3 3
1 14
2 5
3 5
3 7
3 10

Sample Output

Copy
0
5
3
7
12
14

Comments

There are no comments at the moment.