CF2178E.Flatten or Concatenate

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is an interactive problem.

While procrastinating at work, Dilhan the elf stumbled upon two arrays aa and bb. Initially, both of them consist of a single integer 2k2^k (i.e., a=b=[2k]a=b=[2^k]), where kk is a non-negative integer.

Dilhan then applied the following two types of operations an arbitrary number of times (possibly zero), in any order:

  1. Flatten — Choose either aa or bb, and select any element xx that is maximal within that array (xx does not need to be maximal in the other array). Then, replace xx with two copies of x2\frac x2 in the same position. This operation can only be applied if xx is even.
  2. Concatenate — Set both aa and bb to be a+ba+b, where ++ denotes array concatenation.

After performing these operations, Dilhan discards bb, hides aa from you, and challenges you to a game.

Let nn be the length of the hidden array aa. You may make the following query:

  • Choose an interval [l,r][l, r] (1≤l≤r≤n1\le l\le r\le n), and Dilhan will tell you the sum al+al+1+⋯+ara_l+a_{l+1}+\cdots+a_r.

Determine the value of the maximum element of aa by making at most 300300 queries.

这是一个交互式问题。

精灵迪尔汉在工作时拖延,偶然发现了两个数组 aa 和 bb。初始时,它们均仅包含一个整数 2k2^k(即 a=b=[2k]a = b = [2^k]),其中 kk 是一个非负整数。

随后,迪尔汉以任意顺序、任意次数(包括零次)执行以下两类操作:

  1. 展平(Flatten) —— 任选数组 aa 或 bb,并在该数组中选择一个极大元 xx(即 xx 在所选数组中为最大值;它无需在另一数组中也为最大值)。然后将该位置上的 xx 替换为两个 x2\frac{x}{2}(即在原位置插入两个相等的副本)。该操作仅当 xx 为偶数时才可执行。
  2. 拼接(Concatenate) —— 将 aa 和 bb 同时设为 a+ba + b,其中 ++ 表示数组拼接(即连接)。

执行完这些操作后,迪尔汉丢弃数组 bb,并将数组 aa 隐藏起来,向你发起挑战。

设隐藏数组 aa 的长度为 nn。你可以进行如下查询:

  • 选择一个区间 [l,r][l, r](满足 1≤l≤r≤n1 \le l \le r \le n),迪尔汉会告诉你子段和 al+al+1+⋯+ara_l + a_{l+1} + \cdots + a_r。

你需要在至多 300300 次查询内,确定数组 aa 中最大元素的值。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001 \le t \le 100). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the length of aa.

It is guaranteed that 1≤ai≤2301\le a_i\le 2^{30}, and the array aa can be generated by the process described in the statements.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1001 \le t \le 100)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)—— 表示数组 aa 的长度。

保证 1≤ai≤2301\le a_i\le 2^{30},且数组 aa 可由题目描述中的过程生成。

保证所有测试用例中 nn 的总和不超过 10510^5。

输入输出样例

  • 输入#1

    4
    11
    
    9
    
    4
    
    12
    
    1
    
    1
    
    8
    
    4
    
    4
    
    4
    
    4
    
    8

    输出#1

    ? 3 8
    
    ? 1 4
    
    ? 5 11
    
    ! 2
    
    ? 1 1
    
    ! 1
    
    ? 1 1
    
    ? 2 2
    
    ? 3 3
    
    ? 4 4
    
    ! 4
    
    ! 1073741824

说明/提示

In the first test case, Dilhan's hidden array aa is [1,1,1,1,2,2,2,1,1,2,2][1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2]. It can be generated by the following process:

Type

aa after operation

bb after operation

0

[4][4]

[4][4]

1

Flatten aa

[4‾]→[2,2][\underline 4] \to {[2, 2]}

[4][4]

2

Flatten bb

[2,2][2, 2]

[4‾]→[2,2][\underline 4] \to {[2, 2]}

3

Flatten aa

[2,2‾]→[2,1,1][2, \underline 2] \to {[2, 1, 1]}

[2,2][2, 2]

4

Concatenate

[2,1,1,2,2][2, 1, 1, 2, 2]

[2,1,1,2,2][2, 1, 1, 2, 2]

5

Flatten aa

[2‾,1,1,2,2]→[1,1,1,1,2,2][\underline 2, 1, 1, 2, 2]\to {[1, 1, 1, 1, 2, 2]}

[2,1,1,2,2][2, 1, 1, 2, 2]

6

Concatenate

[1,1,1,1,2,2,2,1,1,2,2][1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2]

[1,1,1,1,2,2,2,1,1,2,2][1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2]

  • For the query ?  3  8\mathtt{?\;3\;8}, the jury responded with 1+1+2+2+2+1=91+1+2+2+2+1=9;
  • For the query ?  1  4\mathtt{?\;1\;4}, the jury responded with 1+1+1+1=41+1+1+1=4;
  • For the query ?  5  11\mathtt{?\;5\;11}, the jury responded with 2+2+2+1+1+2+2=122+2+2+1+1+2+2=12.

And the value of the maximum element of aa is 22.

In the second test case, the array aa has only one element. By the query, the value of the only element is 11, and it must also be the maximum value.

In the third test case, Dilhan's hidden array aa is [4,4,4,4,4,4,4,4][4, 4, 4, 4, 4, 4, 4, 4]. It can be generated by the following process:

Type

aa after operation

bb after operation

0

[4][4]

[4][4]

1

Concatenate

[4,4][4, 4]

[4,4][4, 4]

2

Concatenate

[4,4,4,4][4, 4, 4, 4]

[4,4,4,4][4, 4, 4, 4]

3

Concatenate

[4,4,4,4,4,4,4,4][4, 4, 4, 4, 4, 4, 4, 4]

[4,4,4,4,4,4,4,4][4, 4, 4, 4, 4, 4, 4, 4]

And the value of the maximum element of aa is 44.

In the fourth test case, Dilhan's hidden array aa is [229,229,230,229,228,228,229,229][2^{29}, 2^{29}, 2^{30}, 2^{29}, 2^{28}, 2^{28}, 2^{29}, 2^{29}].

在第一个测试用例中,Dilhan 的隐藏数组 aa 为 [1,1,1,1,2,2,2,1,1,2,2][1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2]。它可通过以下过程生成:

类型

操作后的 aa

操作后的 bb

0

[4][4]

[4][4]

1

展开 aa

[4‾]→[2,2][\underline 4] \to {[2, 2]}

[4][4]

2

展开 bb

[2,2][2, 2]

[4‾]→[2,2][\underline 4] \to {[2, 2]}

3

展开 aa

[2,2‾]→[2,1,1][2, \underline 2] \to {[2, 1, 1]}

[2,2][2, 2]

4

拼接

[2,1,1,2,2][2, 1, 1, 2, 2]

[2,1,1,2,2][2, 1, 1, 2, 2]

5

展开 aa

[2‾,1,1,2,2]→[1,1,1,1,2,2][\underline 2, 1, 1, 2, 2]\to {[1, 1, 1, 1, 2, 2]}

[2,1,1,2,2][2, 1, 1, 2, 2]

6

拼接

[1,1,1,1,2,2,2,1,1,2,2][1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2]

[1,1,1,1,2,2,2,1,1,2,2][1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2]

  • 对于查询 ?  3  8\mathtt{?\;3\;8},评测系统返回 1+1+2+2+2+1=91+1+2+2+2+1=9;
  • 对于查询 ?  1  4\mathtt{?\;1\;4},评测系统返回 1+1+1+1=41+1+1+1=4;
  • 对于查询 ?  5  11\mathtt{?\;5\;11},评测系统返回 2+2+2+1+1+2+2=122+2+2+1+1+2+2=12。

且数组 aa 中最大元素的值为 22。

在第二个测试用例中,数组 aa 仅含一个元素。通过查询可知该唯一元素的值为 11,它也必然是最大值。

在第三个测试用例中,Dilhan 的隐藏数组 aa 为 [4,4,4,4,4,4,4,4][4, 4, 4, 4, 4, 4, 4, 4]。它可通过以下过程生成:

类型

操作后的 aa

操作后的 bb

0

[4][4]

[4][4]

1

拼接

[4,4][4, 4]

[4,4][4, 4]

2

拼接

[4,4,4,4][4, 4, 4, 4]

[4,4,4,4][4, 4, 4, 4]

3

拼接

[4,4,4,4,4,4,4,4][4, 4, 4, 4, 4, 4, 4, 4]

[4,4,4,4,4,4,4,4][4, 4, 4, 4, 4, 4, 4, 4]

且数组 aa 中最大元素的值为 44。

在第四个测试用例中,Dilhan 的隐藏数组 aa 为 [229,229,230,229,228,228,229,229][2^{29}, 2^{29}, 2^{30}, 2^{29}, 2^{28}, 2^{28}, 2^{29}, 2^{29}]。

输入解题思路,AI测评打分。不知道怎么写?

首页