Mixture
Time limit2sMemory limit256 MB
After each add or remove of a bottle, find the fewest shelf bottles whose nonnegative combination gives the target salt:pepper:garlic ratio, or 0 if none exists.
- Level
Hard8 of 10
- Topics
- Geometry, Segment tree, Math
- Solved
- No attempts yet
Problem
Serge is the chef of the famous restaurant "Salt, Pepper & Garlic". He wants to earn his first Michelin star. He has been told that a secret expert will visit the restaurant this evening.
The expert's name is not disclosed, but Serge knows which dish the expert will order and what the expert likes. The expert requires a very precise ratio of salt, pepper, and garlic powder in the dish.
Serge keeps bottles of mixtures of salt, pepper, and garlic powder on a special shelf. For each bottle, he knows the exact amount of each ingredient in kilograms. He can combine any number of bottled mixtures, or use one bottle directly, to get a mixture with the ratio the dish needs.
The amount of mixture added to a dish is very small, so you may assume the bottles always contain enough ingredients. The ratio values can be very large, however.
Serge wants to know whether he can make the expert's favourite mixture from the bottles on the shelf and, if so, the minimum number of bottles needed. The shelf changes over time as Serge receives new bottles or lends his bottles to other chefs, so he needs the answer after each change.
Table 1: Bottles on the shelf
The expert's favourite mixture is 1 : 1 : 1, and the shelf has three bottles (Table 1). Bottles 1 and 2 used in equivalent amounts are enough to make the desired mixture. If bottle 2 is removed, the desired mixture can no longer be made.
Input
The first line contains three non-negative integers , , and (, ), the amounts of salt, pepper, and garlic powder in the expert's favourite mixture. For any real , is also a favourite mixture.
The second line contains a positive integer (), the number of changes to the shelf. The shelf is empty at the start. Each of the next lines describes one change.
- To add a bottle, the line starts with the letter A, followed by three non-negative integers , , and (, ). Bottles are numbered consecutively from 1, so the -th added bottle has number .
- To remove a bottle, the line starts with the letter R, followed by the bottle number . All removed numbers are distinct, and never exceeds the number of bottles added so far.
Output
Print lines. The -th line contains the minimum number of bottles needed to make a mixture with the expert's favourite proportions, using the bottles on the shelf after the first changes. Print 0 if the mixture cannot be made.
Hint
Bottles 1 and 3 contain salt, pepper, and garlic powder in the same proportions.