luogu#P5210. [ZJOI2017] 线段树
[ZJOI2017] 线段树
Background
A segment tree is a data structure that Jiu Tiao Ke Lian likes very much. It has a simple structure, good complexity, and powerful functions, so Ke Lian once spent a long time studying some properties of segment trees.
Problem Description
A segment tree is a data structure that Jiu Tiao Ke Lian likes very much. It has a simple structure, good complexity, and powerful functions, so Ke Lian once spent a long time studying some properties of segment trees.
Recently, Ke Lian started studying segment trees again. The difference is that she focused on a more general segment tree. In a normal segment tree, for an interval , we take , and then split this interval into two sub-intervals and . In a generalized segment tree, is not required to be exactly the midpoint of the interval, but it still must satisfy . It is not hard to see that in a generalized segment tree, the depth of the tree can reach .
For example, the following tree is a generalized segment tree:

For convenience, we number all nodes of the segment tree by preorder traversal. For example, in the figure above, the number of is , and the number of is . It is not hard to see that a generalized segment tree built on has a total of nodes.
Consider transplanting the interval-location operation on a segment tree (this is what is done when applying lazy tags) to a generalized segment tree. You can find that on a generalized segment tree, you can still locate an interval using the traditional segment tree method. For example, in the figure above, the blue nodes and blue edges are the nodes and edges passed when locating interval , and the final located nodes are and .
If you are not familiar with segment trees, here is a formal definition of the interval-location operation. Given an interval , find as few segment tree nodes as possible whose intervals are pairwise disjoint, such that the union of their intervals is exactly .
Define as the set of nodes obtained by locating interval . For example, in the figure above, . Define the distance between two nodes on the segment tree as the number of edges on the shortest path from to on the segment tree. For example, in the figure above, .
Now, Ke Lian gives you a generalized segment tree on and queries. Each query gives three integers . Ke Lian wants to know .
Input Format
The first line contains an integer .
The next line contains space-separated integers. In increasing order of node number, they give the split position of every non-leaf node in the generalized segment tree. It is not hard to see that this information uniquely determines a generalized segment tree on .
The next line contains an integer .
Then follow lines. Each line contains three integers , representing a query.
Output Format
For each query, output one integer as the answer.
10
3 1 2 9 6 4 5 7 8
3
7 6 7
18 4 5
14 5 6
7
11
3
Hint
| Test Point ID | Other Constraints | ||
|---|---|---|---|
| None | |||
| None | |||
For of the testdata, it is guaranteed that .
Translated by ChatGPT 5