CF1716E.Swap and Maximum Block

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You are given an array of length 2n2^n. The elements of the array are numbered from 11 to 2n2^n.

You have to process qq queries to this array. In the ii-th query, you will be given an integer kk (0≤k≤n−10 \le k \le n-1). To process the query, you should do the following:

  • for every i∈[1,2n−2k]i \in [1, 2^n-2^k] in ascending order, do the following: if the ii-th element was already swapped with some other element during this query, skip it; otherwise, swap aia_i and ai+2ka_{i+2^k};
  • after that, print the maximum sum over all contiguous subsegments of the array (including the empty subsegment).

For example, if the array aa is [−3,5,−3,2,8,−20,6,−1][-3, 5, -3, 2, 8, -20, 6, -1], and k=1k = 1, the query is processed as follows:

  • the 11-st element wasn't swapped yet, so we swap it with the 33-rd element;
  • the 22-nd element wasn't swapped yet, so we swap it with the 44-th element;
  • the 33-rd element was swapped already;
  • the 44-th element was swapped already;
  • the 55-th element wasn't swapped yet, so we swap it with the 77-th element;
  • the 66-th element wasn't swapped yet, so we swap it with the 88-th element.

So, the array becomes [−3,2,−3,5,6,−1,8,−20][-3, 2, -3, 5, 6, -1, 8, -20]. The subsegment with the maximum sum is [5,6,−1,8][5, 6, -1, 8], and the answer to the query is 1818.

Note that the queries actually change the array, i. e. after a query is performed, the array does not return to its original state, and the next query will be applied to the modified array.

给你一个长度为 2n2^n 的数组。数组元素的编号从 11 到 2n2^n。

你需要处理 qq 个对该数组的查询。在第 ii 个查询中,你将得到一个整数 kk(0≤k≤n−10 \le k \le n-1)。为了处理该查询,你需要执行以下操作:

  • 对每个按升序排列的 i∈[1,2n−2k]i \in [1, 2^n-2^k],执行如下操作:若第 ii 个元素在本次查询中已被与其他元素交换过,则跳过;否则,交换 aia_i 与 ai+2ka_{i+2^k};
  • 然后,输出该数组所有连续子段(包括空子段)的最大和。

例如,若数组 aa 为 [−3,5,−3,2,8,−20,6,−1][-3, 5, -3, 2, 8, -20, 6, -1],且 k=1k = 1,则该查询的处理过程如下:

  • 第 11 个元素尚未被交换,因此将其与第 33 个元素交换;
  • 第 22 个元素尚未被交换,因此将其与第 44 个元素交换;
  • 第 33 个元素已被交换过;
  • 第 44 个元素已被交换过;
  • 第 55 个元素尚未被交换,因此将其与第 77 个元素交换;
  • 第 66 个元素尚未被交换,因此将其与第 88 个元素交换。

于是,数组变为 [−3,2,−3,5,6,−1,8,−20][-3, 2, -3, 5, 6, -1, 8, -20]。具有最大和的连续子段为 [5,6,−1,8][5, 6, -1, 8],该查询的答案为 1818。

注意:这些查询会实际修改数组,即执行完一个查询后,数组不会恢复至原始状态,下一个查询将在已修改的数组上进行。

输入格式

The first line contains one integer nn (1≤n≤181 \le n \le 18).

The second line contains 2n2^n integers a1,a2,…,a2na_1, a_2, \dots, a_{2^n} (−109≤ai≤109-10^9 \le a_i \le 10^9).

The third line contains one integer qq (1≤q≤2⋅1051 \le q \le 2 \cdot 10^5).

Then qq lines follow, the ii-th of them contains one integer kk (0≤k≤n−10 \le k \le n-1) describing the ii-th query.

第一行包含一个整数 nn(1≤n≤181 \le n \le 18)。

第二行包含 2n2^n 个整数 a1,a2,…,a2na_1, a_2, \dots, a_{2^n}(−109≤ai≤109-10^9 \le a_i \le 10^9)。

第三行包含一个整数 qq(1≤q≤2⋅1051 \le q \le 2 \cdot 10^5)。

接下来有 qq 行,其中第 ii 行包含一个整数 kk(0≤k≤n−10 \le k \le n-1),表示第 ii 个查询。

输出格式

For each query, print one integer — the maximum sum over all contiguous subsegments of the array (including the empty subsegment) after processing the query.

对于每个查询,输出一个整数——即处理完该查询后,数组所有连续子段(包括空子段)的最大和。

输入输出样例

  • 输入#1

    3
    -3 5 -3 2 8 -20 6 -1
    3
    1
    0
    1

    输出#1

    18
    8
    13

说明/提示

Transformation of the array in the example: [−3,5,−3,2,8,−20,6,−1]→[−3,2,−3,5,6,−1,8,−20]→[2,−3,5,−3,−1,6,−20,8]→[5,−3,2,−3,−20,8,−1,6][-3, 5, -3, 2, 8, -20, 6, -1] \rightarrow [-3, 2, -3, 5, 6, -1, 8, -20] \rightarrow [2, -3, 5, -3, -1, 6, -20, 8] \rightarrow [5, -3, 2, -3, -20, 8, -1, 6].

示例中的数组变换:[−3,5,−3,2,8,−20,6,−1]→[−3,2,−3,5,6,−1,8,−20]→[2,−3,5,−3,−1,6,−20,8]→[5,−3,2,−3,−20,8,−1,6][-3, 5, -3, 2, 8, -20, 6, -1] \rightarrow [-3, 2, -3, 5, 6, -1, 8, -20] \rightarrow [2, -3, 5, -3, -1, 6, -20, 8] \rightarrow [5, -3, 2, -3, -20, 8, -1, 6]。

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

首页