#P10834. [COTS 2023] 题 Zadatak

    ID: 12288 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>树上启发式合并2023O2优化COCI(克罗地亚)线段树合并

[COTS 2023] 题 Zadatak

Background

Translated from Izborne Pripreme 2023 (Croatian IOI/CEOI Team Selection) D2T3。1s,0.5G\texttt{1s,0.5G}

Happy birthday to NaCly_Fish! (2024.7.28).

Problem Description

Jura has NN squares, numbered 1N1\sim N. The side length of the ii-th square is aia_i, and 2ai2\mid a_i. Initially, all these squares are black.

Jura decides to spend (N1)(N-1) seconds of his life playing with these squares. In the ii-th second, Jura merges the xix_i-th and the yiy_i-th squares into the (N+i)(N+i)-th square (after the merge, the xix_i-th and yiy_i-th squares no longer exist).

When merging squares, align the centers of the two squares, and place them on the plane with their edges parallel. The new square has the side length of the larger one among the two squares being merged. Its color is the “xor sum” of the original colors (black + black = white, white + white = white, black + white = black, white + black = black). The cost of a merge is the area of the region that is black in both squares within their intersection before merging (but after placing them as required above).

You need to output the cost of each merge operation.

The figure below shows an example of merging squares:

Input Format

The first line contains a positive integer NN, denoting the number of squares.

The second line contains NN positive integers describing the array aa.

The next (N1)(N-1) lines each contain two positive integers xi,yix_i,y_i, describing one operation.

Output Format

Output (N1)(N-1) lines, each containing one integer, denoting the cost of the operation.

6 
8 6 2 4 2 6
1 2
3 4
5 7
6 8
9 10
36
4
0
12
4
7 
4 2 6 6 2 4 2
1 2
3 8
4 9
5 10
6 11
7 12
4
12
24
0
16
0 
8
4 10 2 10 6 8 4 12
1 2
3 4
5 6
7 8
9 10
11 12
13 14
16
4
36
16
84
28
0

Hint

Sample Explanation

The last operation of sample 11 is shown in the figure below:

Constraints

For 100%100\% of the testdata, it is guaranteed that:

  • 1N1051\le N\le 10^5.
  • 2ai1062\le a_i\le 10^6.
  • 2ai2\mid a_i.
  • 1xi,yiN+i11\le x_i,y_i\le N+i-1.
  • Before each operation, the squares exist, and xiyix_i\neq y_i.
Subtask ID Points Constraints
11 1414 N5000N\le 5\, 000
22 2525 x1=1,y1=2x_1=1,y_1=22iN1\forall 2\le i\le N-1xi=i+1,yi=N+i1x_i=i+1,y_i=N+i-1
33 1717 kN\exists k\in \mathbb{N},such that 2k=N2^k=Nxi=2i1,yi=2ix_i=2i-1,y_i=2i
44 2121 n30000n\le 30\, 000
55 2323 No additional constraints

Translated by ChatGPT 5