Valeriy and Deque
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with nn elements. The ii -th element is aiai (ii = 1,2,…,n1,2,…,n ). He gradually takes the first two leftmost elements from the deque (let's call them AA and BB , respectively), and then does the following: if A>BA>B , he writes AA to the beginning and writes BB to the end of the deque, otherwise, he writes to the beginning BB , and AA writes to the end of the deque. We call this sequence of actions an operation.
For example, if deque was [2,3,4,5,1][2,3,4,5,1] , on the operation he will write B=3B=3 to the beginning and A=2A=2 to the end, so he will get [3,4,5,1,2][3,4,5,1,2] .
The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him qq queries. Each query consists of the singular number mjmj (j=1,2,…,q) . It is required for each query to answer which two elements he will pull out on the mjmj -th operation.
Note that the queries are independent and for each query the numbers AA and BB should be printed in the order in which they will be pulled out of the deque.
Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides.
Input
The first line contains two integers nn and qq (2≤n≤1052≤n≤105 , 0≤q≤3⋅1050≤q≤3⋅105 ) — the number of elements in the deque and the number of queries. The second line contains nn integers a1 , a2 , ..., an , where aiai (0≤ai≤109)(0≤ai≤109) — the deque element in ii -th position. The next qq lines contain one number each, meaning mjmj (1≤mj≤10181≤mj≤1018 ).
Output
For each teacher's query, output two numbers AA and BB — the numbers that Valeriy pulls out of the deque for the mjmj -th operation.
Examples
Input
5 3 1 2 3 4 5 1 2 10
Output
1 2 2 3 5 2
Input
2 0 0 0
Output
Note
- Consider all 10 steps for the first test in detail:
- [1,2,3,4,5] — on the first operation, A and B are 1 and 2 , respectively.
So, 22 we write to the beginning of the deque, and 1 — to the end.
We get the following status of the deque: [2,3,4,5,1].
- [2,3,4,5,1]⇒A=2,B=3 .
- [3,4,5,1,2]
- [4,5,1,2,3]
- [5,1,2,3,4]
- [5,2,3,4,1]
- [5,3,4,1,2]
- [5,4,1,2,3]
- [5,1,2,3,4]
- [5,2,3,4,1]⇒A=5,B=2 .
双端队列的考察。
deque容器为一个给定类型的元素进行线性处理,像向量一样,它能够快速地随机访问任一个元素,并且能够高效地插入和删除容器的尾部元素。但它又与vector不同,deque支持高效插入和删除容器的头部元素,因此也叫做双端队列。
#include<iostream>
#include<deque>//双向队列
using namespace std;
#define ll long long
const int MAXN=300010;
int n,m,maxx=0,num=0,ans[MAXN];
deque<ll> q;
struct node{
int r,l;
}t[MAXN];
void solve(){
while(1){
int x=q.front();
q.pop_front();
int y=q.front();
q.pop_front();
if(x==maxx){
q.push_front(y);
for(int i=1;i<=n-1;i++){
y=q.front();
q.pop_front();
ans[i]=y;
}
break;
}
else{
num++;
t[num].l=x;
t[num].r=y;
if(x>y){
q.push_front(x);
q.push_back(y);
}
else{
q.push_front(y);
q.push_back(x);
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
int a;
cin>>a;
maxx=max(a,maxx);
q.push_back(a);//放在队尾(右)
}
solve();//进行模拟操作
for(int i=0;i<m;i++){
ll c;
cin>>c;
if(c<=num) cout<<t[c].l<<" "<<t[c].r<<endl;
else{
c-=num;
c%=(n-1);
if(c==0) c=n-1;
cout<<maxx<<" "<<ans[c]<<endl;
}
}
return 0;
}
/* deque容器为一个给定类型的元素进行线性处理,
像向量一样,它能够快速地随机访问任一个元素,并
且能够高效地插入和删除容器的尾部元素。但它又与
vector不同,deque支持高效插入和删除容器的头部元
素,因此也叫做双端队列。*/