#P9507. [BalkanOI 2018] Popa

    ID: 10778 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2018交互题Special Judge构造BalkanOI(巴尔干半岛)

[BalkanOI 2018] Popa

Background

Translated from BalkanOI 2018 Day 2 T2 “Popa”.

"He's an outlaw and he's famous
Andrii Popa the courageous.
Day and night he rides,
He takes his tribute from the main road
And everywhere in the country
The thief catchers are running away as fast as they can"

- "Andrii Popa", Phoenix

Problem Description

Ghiță has a sequence SS of positive integers indexed from 00. Since he is the King of the Carpathians, he wants to construct a binary tree with nodes numbered 0,1,,N10,1,\ldots,N-1 such that:

  • The inorder traversal of the tree lists the node indices in increasing order. The inorder traversal of a binary tree is the concatenation, in order, of the inorder traversal of the subtree rooted at the left child of the root (if it exists), the index of the root, and the inorder traversal of the subtree rooted at the right child of the root (if it exists).
  • If xx is the parent of node yy, then SxS_x divides SyS_y.

A binary tree is a tree structure where each node has at most two children, called the left child and the right child.

Unfortunately, the famous outlaw Andrii Popa stole the sequence SS, so Ghiță cannot access it directly. However, for any two contiguous subarrays [a,b][a,b] and [c,d][c,d], he can use the most advanced technology (his phone) to determine whether gcd[a,b]\gcd[a,b] is equal to gcd[c,d]\gcd[c,d], where gcd[x,y]\gcd[x,y] denotes the greatest common divisor of Sx,Sx+1,Sx+2,,SyS_x,S_{x+1},S_{x+2},\ldots,S_y. Unfortunately, this technology is very expensive—if Ghiță uses it more than QQ times, he will have to pay a huge fine. Please help him construct the desired tree using this technology at most QQ times. It is guaranteed that this is possible. Any valid construction will be accepted.

Interaction

This problem only supports interactive solutions via functions in C++. The contestant’s code does not need to and must not include popa.h.

You need to implement the following function:

int solve(int N, int* Left, int* Right);

The function must return the root of the tree, and set Left[i] and Right[i] to be the left child and the right child of node ii, respectively. If node ii has no left child, then Left[i] should be set to 1-1. If node ii has no right child, then Right[i] should be set to 1-1. Left and Right point to two already-allocated arrays of length exactly NN.

The function solve will be called at most 55 times in a single run. We suggest using global variables carefully.

You may call the following function (note that you must declare this function in your code):

int query(int a, int b, int c, int d);

This function returns 11 if and only if gcd[a,b]=gcd[c,d]\gcd[a,b]=\gcd[c,d], where 0ab<n,0cd<N0\le a\le b<n,0\le c\le d<N. Otherwise it returns 00.

Example

For example, if S=[12,4,16,2,2,20]S=[12, 4, 16, 2, 2, 20], one possible interaction process is as follows:

Call to solve Call to query After calling solve
solve(6, Left, Right)
query(0, 1, 3, 5) returns 00
query(4, 5, 1, 3) returns 11
solve returns 33; Left points to [1,0,1,1,1,1][-1, 0, -1, 1, -1, -1]; Right points to [1,2,1,4,5,1][-1, 2, -1, 4, 5, -1]

In the example, the tree King Ghiță wants is shown in the figure below.

Input Format

See “Interaction”.

Output Format

See “Interaction”.

Hint

Constraints

Subtask ID Limits Score
11 N=100,Q=104N=100,Q=10^4 3737
22 N=103,Q=2×104N=10^3,Q=2\times 10^4 2424
33 N=103,Q=2×103N=10^3,Q=2\times 10^3 3939

Translated by ChatGPT 5