A11579 | A Leapfrog in the Array
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.
Let's consider that initially array contains $n$ numbers from $1$ to $n$ and the number $i$ is located in the cell with the index $2i-1$ (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all $n$ numbers will appear in the first $n$ cells of the array. For example if $n=4$ , the array is changing as follows:
You have to write a program that allows you to determine what number will be in the cell with index $x$ ( $1<=x<=n$ ) after Dima's algorithm finishes.
Let's consider that initially array contains $n$ numbers from $1$ to $n$ and the number $i$ is located in the cell with the index $2i-1$ (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all $n$ numbers will appear in the first $n$ cells of the array. For example if $n=4$ , the array is changing as follows:
You have to write a program that allows you to determine what number will be in the cell with index $x$ ( $1<=x<=n$ ) after Dima's algorithm finishes.
输入格式
The first line contains two integers $n$ and $q$ ( $1<=n<=10^{18}$ , $1<=q<=200000$ ), the number of elements in the array and the number of queries for which it is needed to find the answer.
Next $q$ lines contain integers $x_{i}$ ( $1<=x_{i}<=n$ ), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.
Next $q$ lines contain integers $x_{i}$ ( $1<=x_{i}<=n$ ), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.
输出格式
For each of $q$ queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
输入输出样例
输入 #1
4 3 2 3 4
输出 #1
3 2 4
输入 #2
13 4 10 5 4 8
输出 #2
13 3 8 9
The first example is shown in the picture.
In the second example the final array is $[1,12,2,8,3,11,4,9,5,13,6,10,7]$ .
In the second example the final array is $[1,12,2,8,3,11,4,9,5,13,6,10,7]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted