Fix by reference, find with lower_bound
Store the name, price and stock of n items in a std::vector of structs, change them with m commands, then answer q queries.
The function that finds an item by name returns the element inside the vector as a reference (Item&), and the function that changes an item takes that reference and changes the element in place.
After all commands, sort with std::sort and a lambda comparator by price from low to high, and by name in dictionary order on equal prices.
Answer queries with std::lower_bound.
The first line has n, and the next n lines have "name price stock".
The next line has m, and the next m lines each have a command: "sell name k", "restock name k" or "price name p".
sell subtracts k from the stock if there are at least k; otherwise it changes nothing.
restock adds k to the stock.
price changes the price to p.
The next line has q, and the next q lines have one integer x each.
For each query, print the number c of items in the sorted list whose price is lower than x, and the first item whose price is at least x, as "c name price stock".
If there is no such item, print "c none".
- Input
- 1 ≤ n ≤ 1000, 0 ≤ m ≤ 1000, 1 ≤ q ≤ 1000.
Names are 1 to 8 lowercase English letters, all different, and commands use only existing names.
1 ≤ price, p ≤ 1,000,000,000.
Initial stock is 0 to 1000, 1 ≤ k ≤ 1,000,000.
1 ≤ x ≤ 1,000,000,001 (all integers) - Output
- one line per query
4 pear 7 1 fig 7 2 apple 9 3 kiwi 2 4 3 sell fig 2 sell kiwi 5 price apple 7 4 7 3 1 8
1 apple 7 3 1 apple 7 3 0 kiwi 2 4 4 none
2 tea 10 5 jam 20 5 2 restock tea 3 price tea 30 3 20 21 31
0 jam 20 5 1 tea 30 8 2 none