#P5854. 【模板】笛卡尔树

【模板】笛卡尔树

Description

Given a permutation pp of 1n1 \sim n, build its Cartesian tree.

That is, build a binary tree that satisfies:

  1. The index of each node satisfies the property of a binary search tree.
  2. The weight of node ii is pip_i, and the weights satisfy the min-heap property.

Input Format

The first line contains an integer nn.

The second line contains a permutation p1np_{1 \dots n}.

Output Format

Let li,ril_i, r_i denote the indices of the left and right children of node ii (use 00 if it does not exist).

Output one line with two integers: xori=1ni×(li+1)\operatorname{xor}_{i = 1}^n i \times (l_i + 1) and xori=1ni×(ri+1)\operatorname{xor}_{i = 1}^n i \times (r_i + 1).

5
4 1 3 2 5

19 21

Hint

[Sample Explanation]

ii lil_i rir_i
11 00
22 11 44
33 00
44 33 55
55 00

[Constraints]

For 30%30\% of the testdata, n103n \le 10^3.

For 60%60\% of the testdata, n105n \le 10^5.

For 80%80\% of the testdata, n106n \le 10^6.

For 90%90\% of the testdata, n5×106n \le 5 \times 10^6.

For 100%100\% of the testdata, 1n1071 \le n \le 10^7.

Translated by ChatGPT 5