CF2173F Isla's Memory Thresholds
Description
I hope one day, you'll be reunited with the one you cherish.
— Plastic Memories
In the world of Plastic Memories, Isla is collecting $ n $ memory fragments. The $ i $ -th fragment has size $ a_i $ , and the sizes are non-increasing, that is, $ a_1 \ge a_2 \ge \cdots \ge a_n $ .
During retrieval, Isla processes fragments on a range and stores their sizes into a buffer. Whenever the buffer reaches a given threshold $ x $ , it overflows: one capsule is recorded, and the buffer is cleared to zero.
There are $ q $ independent queries, each described by a triple $ (l,r,x) $ . For each query, $ x $ denotes Isla's memory capacity. Isla then picks up the $ i $ -th memory fragment one by one for each $ l \le i \le r $ . At any moment, if the total size of the fragments she is currently holding is at least $ x $ , she clears her memory completely (keeping nothing). You must determine how many times Isla clears her memory and the final sum of the size of the fragments she holds.
Input Format
Each test contains multiple test cases. The first line contains the number of test cases $ t $ ( $ 1 \le t \le 1000 $ ). The description of the test cases follows.
The first line of each test case contains two integers $ n $ and $ q $ ( $ 1 \le n, q \le 150,000 $ ) — the length of $ a $ and the number of queries.
The second line of each test case contains $ n $ integers $ a_1,a_2,\ldots,a_n $ ( $ 1 \le a_i \le 10^9 $ ) — the elements of $ a $ . It is guaranteed that $ a_1 \ge a_2 \ge \cdots \ge a_n $ .
Then $ q $ lines follow, each containing three integers $ l $ , $ r $ , and $ x $ ( $ 1 \le l \le r \le n $ , $ 1 \le x \le 10^9 $ ) — a query.
It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 150,000 $ .
It is guaranteed that the sum of $ q $ over all test cases does not exceed $ 150,000 $ .
Output Format
For each test case, output two integers for each query — the number of times Isla clears her memory and the final sum of the size of the fragments she holds.
Explanation/Hint
Let $ \texttt{cnt} $ be how many times Isla has cleared her memory, and $ \texttt{sum} $ be the total size of the fragments she is holding.
In the first test case, for the first query $ (l, r, x) = (1, 3, 10) $ :
- start with $ \texttt{sum}=0 $ ;
- add $ a_1=7 \Rightarrow \texttt{sum}=7 $ (still $ < 10 $ );
- add $ a_2=5 \Rightarrow \texttt{sum}=12 \ge 10 \Rightarrow \texttt{sum} \gets 0 $ , $ \texttt{cnt} \gets 1 $ ;
- add $ a_3=5 \Rightarrow \texttt{sum}=5 $ .
Thus, we print $ \texttt{cnt}=1 $ and $ \texttt{sum}=5 $ .
In the second test case, for the fifth query $ (l, r, x) = (2, 5, 3) $ :
- start with $ \texttt{sum}=0 $ ;
- add $ a_2=6 \Rightarrow \texttt{sum}=6 \ge 3 \Rightarrow \texttt{sum} \gets 0 $ , $ \texttt{cnt} \gets 1 $ ;
- add $ a_3=5 \Rightarrow \texttt{sum}=5 \ge 3 \Rightarrow \texttt{sum} \gets 0 $ , $ \texttt{cnt} \gets 2 $ ;
- add $ a_4=3 \Rightarrow \texttt{sum}=3 \ge 3 \Rightarrow \texttt{sum} \gets 0 $ , $ \texttt{cnt} \gets 3 $ ;
- add $ a_5=2 \Rightarrow \texttt{sum}=2 $ (still $ < 3 $ );
Thus, we print $ \texttt{cnt}=3 $ and $ \texttt{sum}=2 $ .