Train Maintenance
Contest: Codeforces Round 745 (Div. 1)
Difficulty rating: 2200
Tags: brute force, data structures, implementation

Kawasiro Nitori is excellent in engineering. Thus she has been appointed to help maintain trains.

There are $$$n$$$ models of trains, and Nitori's department will only have at most one train of each model at any moment. In the beginning, there are no trains, at each of the following $$$m$$$ days, one train will be added, or one train will be removed. When a train of model $$$i$$$ is added at day $$$t$$$, it works for $$$x_i$$$ days (day $$$t$$$ inclusive), then it is in maintenance for $$$y_i$$$ days, then in work for $$$x_i$$$ days again, and so on until it is removed.

In order to make management easier, Nitori wants you to help her calculate how many trains are in maintenance in each day.

On a day a train is removed, it is not counted as in maintenance.

Input format:
The first line contains two integers $$$n$$$, $$$m$$$ ($$$1 \le n,m \le 2 \cdot 10^5$$$).

The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i,y_i$$$ ($$$1 \le x_i,y_i \le 10^9$$$).

Each of the next $$$m$$$ lines contains two integers $$$op$$$, $$$k$$$ ($$$1 \le k \le n$$$, $$$op = 1$$$ or $$$op = 2$$$). If $$$op=1$$$, it means this day's a train of model $$$k$$$ is added, otherwise the train of model $$$k$$$ is removed. It is guaranteed that when a train of model $$$x$$$ is added, there is no train of the same model in the department, and when a train of model $$$x$$$ is removed, there is such a train in the department.

Output format:
Print $$$m$$$ lines, The $$$i$$$-th of these lines contains one integers, denoting the number of trains in maintenance in the $$$i$$$-th day.

Constraints:
Time limit: 1.0 s
Memory limit: 512.0 MB

Note:
Consider the first example:

The first day: Nitori adds a train of model $$$3$$$. Only a train of model $$$3$$$ is running and no train is in maintenance.

The second day: Nitori adds a train of model $$$1$$$. A train of model $$$1$$$ is running and a train of model $$$3$$$ is in maintenance.

The third day: Nitori removes a train of model $$$1$$$. The situation is the same as the first day.

The fourth day: Nitori removes a train of model $$$3$$$. There are no trains at all.

Example input:
3 4
10 15
12 10
1 1
1 3
1 1
2 1
2 3

Example output:
0
1
0
0

Example input:
5 4
1 1
10000000 100000000
998244353 1
2 1
1 2
1 5
2 5
1 5
1 1

Example output:
0
0
0
1

Editorial:
Let's distinguish the trains according to $$$x_i + y_i$$$.
If $$$x_i + y_i > \sqrt{m}$$$, the total times of maintenance and running don't exceed $$$\frac{m}{\sqrt{m}}=\sqrt{m}$$$. So we can find every date that the train of model $$$i$$$ begin or end maintenance in $$$O(\sqrt{m})$$$, and we can maintain a differential sequence. We can add 1 to the beginning date and minus 1 to the end date, and the prefix sum of this sequence is the number of trains in maintenance.
If $$$x_i + y_i \le \sqrt{m}$$$, suppose the train of model $$$i$$$ is repaired at $$$s_i$$$ day. For a date $$$t$$$ that the train of model $$$i$$$ is in maintenance if and only if $$$(t-s_i) \ mod \ (x_i+y_i) \geq x_i$$$. Thus for each $$$a \le \sqrt{m}$$$, we can use an array of length $$$a$$$ to record the date of all trains that satisfy $$$x_i + y_i = a$$$ are in maintenance modulo $$$a$$$. And for one period of maintenance, the total days aren't exceed $$$\sqrt{m}$$$. So we can maintain $$$(t - s_i) \ mod \ (x_i + y_i)$$$ in $$$O(\sqrt{m})$$$.
Thus the intended time complexity is $$$O(m\sqrt{m})$$$ and the intended memory complexity is $$$O(n + m)$$$.