#P5854. 【模板】笛卡尔树
【模板】笛卡尔树
Background
Some implementations of this problem may trigger a compiler bug in GCC 15.1 under O2. C++ users are advised to submit with C++14 (GCC9).
Problem Description
Given a permutation of , build its Cartesian tree.
That is, build a binary tree that satisfies:
- The index of each node satisfies the property of a binary search tree.
- The weight of node is , and the weights satisfy the min-heap property.
Input Format
The first line contains an integer .
The second line contains a permutation .
Output Format
Let denote the indices of the left and right children of node (use if it does not exist).
Output one line with two integers: and .
5
4 1 3 2 5
19 21
Hint
[Sample Explanation]
[Constraints]
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, .
Translated by ChatGPT 5