GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Tuesday 7 June 2022

[Solution] Count the Trains Codeforces Solution


G. Count the Trains
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n of independent carriages on the rails. The carriages are numbered from left to right from 1 to n. The carriages are not connected to each other. The carriages move to the left, so that the carriage with number 1 moves ahead of all of them.

The i-th carriage has its own engine, which can accelerate the carriage to ai km/h, but the carriage cannot go faster than the carriage in front of it. See example for explanation.

All carriages start moving to the left at the same time, and they naturally form trains. We will call trains — consecutive moving carriages having the same speed.

For example, we have n=5 carriages and array a=[10,13,5,2,6]. Then the final speeds of the carriages will be [10,10,5,2,2]. Respectively, 3 of the train will be formed.

There are also messages saying that some engine has been corrupted:

  • message "k d" means that the speed of the k-th carriage has decreased by d (that is, there has been a change in the maximum speed of the carriage ak=akd).

Messages arrive sequentially, the processing of the next message takes into account the changes from all previous messages.

Solution Click Below:-  CLICK HERE

After each message determine the number of formed trains.

Input

The first line of input data contains a single integer t (1t104) —the number of input test cases.

This is followed by descriptions of the test cases.

The first line of each test case is empty.

The second line of the test case contains two integers n and m (1n,m105) —the number of carriages and the number of messages to slow down the carriage, respectively.

The third line contains n integers: a1,a2,,an (0ai109) — the number ai means that the carriage with number i can reach a speed of ai km/h.

The next m lines contain two integers kj and dj (1kjn0djakj) —this is the message that the speed of the carriage with number kj has decreased by dj. In other words, there has been a change in its maximum speed akj=akjdj. Note that at any time the speed of each carriage is non-negative. In other words, aisi, where si —is the sum of such dj that kj=i.

It is guaranteed that the sum of n over all test cases does not exceed 105. Similarly, it is guaranteed that the sum of m over all test cases does not exceed 105.

Output

Print t lines. On each line print the answer for the corresponding test case.

For each test case print m numbers: the number of trains formed after each message.


No comments:

Post a Comment