A15340 | Swap and Maximum Block
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given an array of length $2^n$ . The elements of the array are numbered from $1$ to $2^n$ .
You have to process $q$ queries to this array. In the $i$ -th query, you will be given an integer $k$ ( $0 \le k \le n-1$ ). To process the query, you should do the following:
- for every $i \in [1, 2^n-2^k]$ in ascending order, do the following: if the $i$ -th element was already swapped with some other element during this query, skip it; otherwise, swap $a_i$ and $a_{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 $a$ is $[-3, 5, -3, 2, 8, -20, 6, -1]$ , and $k = 1$ , the query is processed as follows:
- the $1$ -st element wasn't swapped yet, so we swap it with the $3$ -rd element;
- the $2$ -nd element wasn't swapped yet, so we swap it with the $4$ -th element;
- the $3$ -rd element was swapped already;
- the $4$ -th element was swapped already;
- the $5$ -th element wasn't swapped yet, so we swap it with the $7$ -th element;
- the $6$ -th element wasn't swapped yet, so we swap it with the $8$ -th element.
So, the array becomes $[-3, 2, -3, 5, 6, -1, 8, -20]$ . The subsegment with the maximum sum is $[5, 6, -1, 8]$ , and the answer to the query is $18$ .
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.
You have to process $q$ queries to this array. In the $i$ -th query, you will be given an integer $k$ ( $0 \le k \le n-1$ ). To process the query, you should do the following:
- for every $i \in [1, 2^n-2^k]$ in ascending order, do the following: if the $i$ -th element was already swapped with some other element during this query, skip it; otherwise, swap $a_i$ and $a_{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 $a$ is $[-3, 5, -3, 2, 8, -20, 6, -1]$ , and $k = 1$ , the query is processed as follows:
- the $1$ -st element wasn't swapped yet, so we swap it with the $3$ -rd element;
- the $2$ -nd element wasn't swapped yet, so we swap it with the $4$ -th element;
- the $3$ -rd element was swapped already;
- the $4$ -th element was swapped already;
- the $5$ -th element wasn't swapped yet, so we swap it with the $7$ -th element;
- the $6$ -th element wasn't swapped yet, so we swap it with the $8$ -th element.
So, the array becomes $[-3, 2, -3, 5, 6, -1, 8, -20]$ . The subsegment with the maximum sum is $[5, 6, -1, 8]$ , and the answer to the query is $18$ .
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.
输入格式
The first line contains one integer $n$ ( $1 \le n \le 18$ ).
The second line contains $2^n$ integers $a_1, a_2, \dots, a_{2^n}$ ( $-10^9 \le a_i \le 10^9$ ).
The third line contains one integer $q$ ( $1 \le q \le 2 \cdot 10^5$ ).
Then $q$ lines follow, the $i$ -th of them contains one integer $k$ ( $0 \le k \le n-1$ ) describing the $i$ -th query.
The second line contains $2^n$ integers $a_1, a_2, \dots, a_{2^n}$ ( $-10^9 \le a_i \le 10^9$ ).
The third line contains one integer $q$ ( $1 \le q \le 2 \cdot 10^5$ ).
Then $q$ lines follow, the $i$ -th of them contains one integer $k$ ( $0 \le k \le n-1$ ) describing the $i$ -th query.
输出格式
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] \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]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted