#P5494. 【模板】线段树分裂

【模板】线段树分裂

Problem Description

Given a multiset aa (ID 11), it supports the following operations:

0 p x y: Move all values in multiset pp that are greater than or equal to xx and less than or equal to yy into a new multiset (the new multiset ID starts from 22 and is the previous newly created multiset ID plus 11).

1 p t: Put all numbers in multiset tt into multiset pp, and clear multiset tt (the testdata guarantees that multiset tt will not appear in subsequent operations).

2 p x q: Add xx copies of the number qq into multiset pp.

3 p x y: Query the number of values in multiset pp that are greater than or equal to xx and less than or equal to yy.

4 p k: Query the kk-th smallest number in multiset pp. If it does not exist, output -1.

Input Format

The first line contains two integers n,mn, m, meaning the numbers in the multiset are in the range 1∼n1 \sim n, and there are mm operations.

The next line contains nn integers, representing the number of occurrences of each number 1∼n1 \sim n in aa (ai≤m)(a_{i} \leq m).

The next mm lines each contain several integers. The first integer is the operation ID optopt (0≤opt≤4)(0 \leq opt \leq 4), as described in the Description.

Output Format

Output the answer for each query operation in order.

5 12
0 0 0 0 0
2 1 1 1
2 1 1 2
2 1 1 3
3 1 1 3
4 1 2
2 1 1 4
2 1 1 5
0 1 2 4
2 2 1 4
3 2 2 4
1 1 2
4 1 3
3
2
4
3

Hint

For 30%30\% of the testdata, 1≤n≤1031 \leq n \leq {10}^3, 1≤m≤1031 \le m \le {10}^3.
For 100%100\% of the testdata, 1≤n≤2×1051 \le n \le 2 \times {10}^5, 1≤x,y,q≤m≤2×1051 \le x, y, q \le m \le 2 \times {10}^5. The testdata is guaranteed to be valid.

If you do not use long long, you will regret it.


Statement by

https://www.luogu.com.cn/user/86625

std by

https://www.luogu.com.cn/user/86625
t tree split).

Tested by

https://www.luogu.com.cn/user/100285
tree, but the merge complexity was incorrectly assumed; the second-to-last test point is hack testdata).

Testdata by

https://www.luogu.com.cn/user/100285

Translated by ChatGPT 5