B. A Leapfrog in the Array
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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.

Input

The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), 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 xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.

Output

For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.

Examples
input

Copy
4 3
2
3
4
output
3
2
4
input

Copy
13 4
10
5
4
8
output
13
3
8
9
Note

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].

题意:题中四张图已经说明题意

数组中隔一个放个数字,然后找最后一个往最近的空子里塞

q个询问,求最后在某个位置上的数字是什么。

题解:把过程倒过来考虑:

从最终状态开始,按照规则把被塞进的数移动回数组最后。

就是按照4,3,2,1的顺序看题目中的图,递归模拟,注意细节。

 /*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=(xx<<)+(xx<<)+ch-'';ch=getchar();}
return xx*ff;
}
long long READ(){
long long xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=(xx<<)+(xx<<)+ch-'';ch=getchar();}
return xx*ff;
}
long long N,q;
int Q;
inline long long can(long long a,long long b){//计算在连续区间内有几个元素是需要移动的(下标为偶数)
long long seq=(b-a+);
if(seq<=)
return ;
if(seq&){
if(b&)
return seq/;
else
return seq/+;
}
else
return seq/;
}
void dfs(long long now,long long L,long long R){//now:询问元素所在的位置 L:可能移动的连续区间左端点 R:右端点
long long t=R+can(L,R);
if(R<now)
dfs(now,R+,t);
else{
long long np=can(L,now-)+R+;//np:now移动后应该在的位置
if(np>N||(now&)){
printf("%I64d\n",now/+);
return;
}
dfs(np,now+,np);
}
}
int main(){
//freopen("in","r",stdin);
N=READ()*-,Q=read();
for(int i=;i<=Q;i++){
q=READ();
long long r=N/+;
dfs(q,,r);
}
return ;
}

codeforces 949B A Leapfrog in the Array的更多相关文章

  1. codeforces 949B :A Leapfrog in the Array 找规律

    题意: 现在给你一个n,表示有2*n-1个方格,第奇数方格上会有一个数字 1-n按顺序放.第偶数个方格上是没有数字的.变动规则是排在最后一个位置的数字,移动到它前边最近的空位 . 直到数字之间没有空位 ...

  2. Codeforces 950D A Leapfrog in the Array (思维)

    题目链接:A Leapfrog in the Array 题意:给出1-n的n个数,从小到大每隔一个位置放一个数.现在从大到小把数往前移动,每次把最右边的数移动最靠右边的空格处直到n个数都在前n个位置 ...

  3. CodeForces - 950D A Leapfrog in the Array 玄学题

    题意:n个数1~n(n<=1e18)依次放在一个数组中,第i个数位置为2i-1,其它地方是空的.现在重复以下操作:将最右边的数放到离其左边最近的空的位置,直到所有数移到前一半的位置中.有q< ...

  4. Codeforces 950D A Leapfrog in the Array ( 思维 && 模拟 )

    题意 : 给出 N 表示有标号 1~N 的 N 个数,然后从下标 1 开始将这 N 个数每隔一位放置一个,直到 N 个数被安排完,现在有一个操作就是每次将数列中最右边的数向离其左边最近的空缺处填上,一 ...

  5. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

  6. Codeforces 950 D. A Leapfrog in the Array

    http://codeforces.com/contest/950/problem/D 前n/2个格子的奇数下标的数没有参与移动 候n/2个格子的奇数下标的数一定是一路移向偶数下标移 所以还原数的初始 ...

  7. Codeforces Round #181 (Div. 2) A. Array 构造

    A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...

  8. B. A Leapfrog in the Array

    http://codeforces.com/problemset/problem/949/B Dima is a beginner programmer. During his working pro ...

  9. Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配

    题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...

随机推荐

  1. LTTng

    Waiting for dig... http://frederic-wou.net/lttng/

  2. 20.IO流部分笔记

    20.IO流部分笔记 2018/09/06 1.IO流  1.1 创建字节输出流对象,如果没有就自动创建一个 FileOutputStram fos = new FileOutputStram(&qu ...

  3. springmvc视图解析

    SpringMVC 视图解析的几种方式: 在视图解析的过程中,需要知道逻辑view的名字,model的名字以访问model和view. 使用jsp进行解析,InternalResourceViewRe ...

  4. css--小白入门篇6(终)

    一.相对定位 定位有三种,分别是相对定位.绝对定位.固定定位. 相对定位: 1 position:relative; 绝对定位: 1 position:absolute; 固定定位: 1 positi ...

  5. Luogu P1692 部落卫队

    解题思路 数据范围不是很大,那应该不是那些普遍的图论的算法.考虑搜索,用暴力解决.从1到N枚举每一个点的位置,搜索这个点事选还是不选.如果在这个点之前选到的点中又和他冲突的点,那就不选,要么就选. 附 ...

  6. centos7+VMware Workstation创建共享文件夹

    1.第一步设置宿主机共享文件夹路径 2.挂载VMware Tools,如下操作会将tools以光盘挂载点的方式进入到系统中. 3.centos7 挂载存有VMware Tools的光盘并进行安装 1) ...

  7. 阿里云ECS屏蔽25端口,官方建议使用465 SSL端口发送邮件

    阿里云ECS  VPC网络,搭建了zabbix,想通过三方邮件系统发送邮件,本机开虚拟机测试发邮件一切正常,到阿里ECS的时候邮件各种发不出去,到处找原因,最后度娘告诉了我真想,原来阿里把25端口屏蔽 ...

  8. 调用subprocess调用shell命令时屏蔽标准输出

    import os, subprocessp = subprocess.Popen(args, stdout = subprocess.PIPE,stderr = subprocess.STDOUT)

  9. 腾讯云:iptables基础

    iptables 基础 iptables 基本命令 任务时间:5min ~ 10min iptables 可以简单理解为 Linux 系统内核级防火墙 netfilter 的用户态客户端. Linux ...

  10. [bzoj2141][排队] (分块大法好)

    Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和.红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的 ...