#P9247. Magical Queue Memory Management
Magical Queue Memory Management
Magical Queue Memory Management
You are given \(n\) queues, each of type std::queue
. The queues are numbered from 1 to \(n\), and each queue \(i\) has an associated maximum size threshold \(a_i\). After any operation, if the size of the \(i\)th queue exceeds \(a_i\), you must repeatedly perform pop()
operations on that queue until its size becomes \(\le a_i\).
Initially, all queues are empty. You will perform \(q\) operations. Each operation is described by three integers \(l\), \(r\), and \(x\) (denoted as l r x
). In an operation, for every queue with index in the range \([l, r]\), you add the value \(x\) to the back of the queue using push(x)
. After this insertion, you must adjust each affected queue as described above so that its size does not exceed \(a_i\).
After each operation, output the total number of distinct integers currently present across all \(n\) queues. Note that if the same integer appears in different queues or multiple times in one queue, it is counted only once.
Constraints and Hints: The magical behavior of the queues allows each operation to be performed very efficiently. This is a simulation problem, so simply follow the operations step by step while taking care of memory limits for each queue.
inputFormat
The first line contains two integers \(n\) and \(q\) where \(n\) is the number of queues and \(q\) is the number of operations.
The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) where \(a_i\) is the maximum allowed size for the \(i\)th queue.
Each of the following \(q\) lines contains three integers \(l\), \(r\), and \(x\) describing an operation.
outputFormat
After each operation, output on a new line the number of distinct integers currently present in all the queues combined.
sample
3 3
1 1 1
1 2 5
2 3 6
1 3 5
1
2
1
</p>