#P4563. [JXOI2018] 守卫

[JXOI2018] 守卫

Description

On this day she goes mountain climbing. For her safety, her father hires some bodyguards to stay fixed at certain positions on the mountain to monitor Jiutiao Kelian in real time and thus protect her.

Specifically, a mountain can be described as a polyline, and the area below the polyline is rock. This polyline has nn vertices, and there is a pavilion at each vertex. The ii-th vertex has coordinates (i,hi)(i, h_i). Jiutiao Kelian can only play at pavilions, and the bodyguards also only monitor at pavilions.

Due to technical reasons, a guard can only monitor all pavilions he can see whose x-coordinate does not exceed his own position. We say a guard can see a pavilion pp if and only if the segment connecting his own pavilion qq and pp does not pass through any rock. In particular, if this segment happens to pass exactly through any pavilion other than pp and qq, then we consider that the guard cannot see Kelian.

Hiring guards is expensive, so Kelian’s father wants as few guards as possible.

Kelian’s father also wants a detailed hiring plan. He knows some pavilions may be under maintenance. He wants to compute, for all 1lrn1 \le l \le r \le n: if it is known in advance that only the pavilions in the interval [l,r][l, r] can be used for playing (and monitoring), what is the minimum number of guards needed so that every pavilion in [l,r][l, r] is monitored.

Kelian’s father has already obtained a result, and he hopes you can verify whether his result is correct.

Input Format

The first line contains an integer nn denoting the number of pavilions.

The second line contains nn integers, where the ii-th integer hih_i means the ii-th pavilion is at (i,hi)(i, h_i).

Output Format

For all 1lrn1 \le l \le r \le n, compute the minimum number of guards needed if it is known in advance that Kelian will only play at pavilions in [l,r][l, r], so that every pavilion in [l,r][l, r] is monitored. Since the output would be too large, Kelian’s father only asks you to output the XOR of the answers for all [l,r][l, r].

3
2 3 1
3

Hint

Sample Explanation:

  • If rl+12r - l + 1 \le 2, the answer is 11.
  • If l=1l = 1, r=nr = n, then the answer is 22, and two guards are needed at (2,3)(2,3) and (3,1)(3,1) to monitor Kelian.

Constraints:

  • For 30%30\% of the testdata, n20n \le 20.
  • For 70%70\% of the testdata, n500n \le 500.
  • For 100%100\% of the testdata, n5000n \le 5000, 1hi1091 \le h_i \le 10^9.

Translated by ChatGPT 5