The problem is as follows:

There is a long string of paper, of length at most $$$10^{6}$$$ and we have $$$n$$$ pieces of colored tape with us, of varying lengths. We have to process some queries **online**. In a single query, we have two options: apply a single tape on a segment of the paper, or remove a previously applied tape from the paper. If any segment has multiple tapes covering it, only the queried tape will be removed. After each query, we have to print the amount of paper which has at least one tape covering it.

e: Suppose n=3, and the tapes are of lengths [3,4,5], and the queries are as follows:

1. Apply tape-1 on segment [8,10]

2. Apply tape-2 on segment [10,13]

3. Apply tape-3 on segment [5,9]

4. Remove tape-2

5. Remove tape-1

6. Remove tape-3

7. Apply tape-2 on segment [1,4]

Then, the output is:

3

6

9

6

5

0

4

You can assume that the queries are valid, ie: a tape has to be applied before it can be removed, and the number of times a tape is applied is either the same number of times it's removed in the queries or one more than it, in any prefix of the list of queries. Tapes are reusable. The length of segment covered in a query is same as the length of tape, for each apply type query.

How to solve it? All my segment tree ideas for this problem are wrong.