Maintain an array under point increments and decrements, and after each update answer the sum of ceil(t_i / k) for a given k.
Medium7MathPrefix sumArrayImplementationInterviewNo attempts yetTime limit2sMemory limit256 MBAndrew has written n programming problems for a contest and now he plans to prepare them. He estimates that preparing problem i takes ti minutes in total. Andrew wants to invite his friends to share the work.
He does not know how many friends will agree to help, but he wants to split the work fairly. So for problem i he picks an integer xi, and every friend who helps spends xi minutes on that problem. Problem i is completely prepared once the total time his friends spend on it is at least ti minutes. With k friends helping, xi is the smallest integer that satisfies k×xi≥ti.
Whenever Andrew realizes that he misjudged the difficulty of a problem, he increases or decreases ti by 1.
You are given the initial estimates ti. Process m queries in the given order. Each query is one of the following.
1 i: Andrew increases ti by 1.2 i: Andrew decreases ti by 1.3 k: Andrew wants to know how many minutes one friend spends preparing all n problems when k friends help.The first line contains two integers n and m, the number of problems and the number of queries. (1≤n,m≤105)
The second line contains n integers t1,t2,…,tn, where ti is Andrew's initial estimate for problem i. (1≤ti≤5×105)
Each of the next m lines contains one query as two integers q and v. If q is 1, then tv increases by 1. If q is 2, then tv decreases by 1. In both cases 1≤v≤n. If q is 3, then you must report the time one friend spends when v friends help. In this case 1≤v≤5×105.
After every query all values of ti stay between 1 and 5×105.
For every query with q equal to 3, print on its own line the total time one friend spends preparing all n problems, that is x1+x2+⋯+xn.