All problems

Seat Booking Ranges

Medium · prefix sum

A cinema row has n seats, numbered 1 to n, and every seat starts empty. You are given ops, a list of actions in the order they happen. Each action is a list of whole numbers, and its first number says what kind of action it is:

Return a list with the answer to every [3, l, r] action, in the order those actions appear.

Examples

Example 1

Input: n = 5, ops = [[1, 2], [1, 4], [3, 1, 5], [2, 2], [3, 1, 3]]

Output: [2, 0]

Seats 2 and 4 are booked, so the first count is 2. After seat 2 is cancelled, seats 1 to 3 hold no bookings.

Example 2

Input: n = 3, ops = [[1, 1], [1, 1], [3, 1, 1], [2, 2], [3, 1, 3]]

Output: [1, 1]

Booking seat 1 a second time changes nothing, and cancelling empty seat 2 changes nothing, so both counts are 1.

Example 3

Input: n = 1, ops = [[3, 1, 1]]

Output: [0]

Nothing has been booked yet.

Constraints