#P3968. [TJOI2014] 电源插排

    ID: 2900 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2014线段树各省省选平衡树概率论,统计天津

[TJOI2014] 电源插排

Description

There are many power strips in Xiao M’s lab. The outlets are indexed from 11 to nn, arranged in a line from left to right.

Every morning, all outlets are unused. Each time a student comes to the lab, they plug their laptop’s power into some unused outlet.

The students follow a peculiar rule: first, they find the longest contiguous segment of unused outlets.

If there are multiple segments with the same length, they choose the rightmost one. Then they plug into the middle of that segment.

If the segment length is even, they pick the right middle position (i.e., the right one). When a student leaves the lab, they unplug their power.

It is guaranteed that whenever a student arrives, there is at least one empty outlet.

You need to answer how many outlets are already in use within the interval [l,r][l,r].

Input Format

The first line contains two integers nn and qq, the number of outlets and the number of queries.

Then follow qq lines. Each line starts with an integer kk.

If kk is 00, it is followed by two integers ll and rr, indicating a query.

Otherwise, kk indicates that the student with ID kk arrives or leaves. The 11st appearance of kk means arrival, the 22nd appearance means departure, and so on. Each student ID is unique.

Output Format

For each query, output one integer: the number of outlets already in use within the query interval.

7 10
1
2
3
0 1 2
0 4 7
0 2 5
20
0 6 6
99
0 4 6
1
2
2
1
3

Hint

Constraints

For 30%30\% of the testdata, n105,q103n \le 10^5, q \le 10^3.

For 100%100\% of the testdata, $1 \le n \le 10^9, 1 \le q \le 10^5, 0 \le k \le 10^9, 1 \le l \le r \le n$.

Translated by ChatGPT 5