Codeforces Round #569 (Div. 2)
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is ai (i = 1,2,…,n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A>B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A 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], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3,4,5,1,2].
The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number mj (j=1,2,…,q). It is required for each query to answer which two elements he will pull out on the mj-th operation.
Note that the queries are independent and for each query the numbers A and B 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 n and q (2≤n≤105, 0≤q≤3⋅105) — the number of elements in the deque and the number of queries. The second line contains n integers a1, a2, ..., an, where ai (0≤ai≤109) — the deque element in i-th position. The next qlines contain one number each, meaning mj (1≤mj≤1018).
Output
For each teacher's query, output two numbers A and B — the numbers that Valeriy pulls out of the deque for the mj-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, 2 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.[2,3,4,5,1]⇒A=2,B=3.
3.[3,4,5,1,2]
4.[4,5,1,2,3]
5.[5,1,2,3,4]
6.[5,2,3,4,1]
7.[5,3,4,1,2]
8.[5,4,1,2,3]
9.[5,1,2,3,4]
10.[5,2,3,4,1]⇒A=5,B=2.
The meaning of problems: Title number n means that you put the deque which has one operation: before removing two numbers, put in front of a large number of small or as large as a discharge later, you ask after operation m times What two numbers before Yes.
Thinking: Study as a bit deque, feel good (I am more vegetables, before also will not orzorz), by looking at a sample of explanation we can find,
When the first team deque becomes maximum after several operations, and then later operate in the same cycle results after n-1 times,
Because the maximum value in the team must be operated when the first and the second number into the tail, after understanding this situation,
We need only a part of the solution after pretreatment does not circulate the front part of the solution and the cycle can be directly output ......
1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<cmath>
5 #include<algorithm>
6 #include<map>
7 #include<set>
8 #include<vector>
9 #include<queue>
10 using namespace std;
11 #define ll long long
12 const int mod=1e9+7;
13 const int inf=1e9+7;
14
15 deque<int> D; // deque
16
. 17 int main ()
18 is {
. 19 iOS :: sync_with_stdio ( to false ); cin.tie ( 0 ); cout.tie ( 0 );
20 is
21 is d.clear ();
22 is
23 is int n, op;
24 CIN >> >> n-OP; // number n, op operations
25
26 is int Maxx = - . 1 ; // find the maximum value of
27 int NUM;
28 for ( int I = 0 ; I <n- ; I ++ )
29 {
30 CIN >> NUM; // input
31 is IF (NUM> Maxx)
32 Maxx NUM =; // refresh maximum value
33 is d.push_back (NUM); // tail introduced deque
34 is }
35
36 IF (OP = = 0 ) // Laid determination wave
37 [ return 0 ;
38 is
39 Vector <pair < int , int >> V1; // this is stored loop-free part of the solution before
40 Vector <pair < int , int >> V2;// This is the part of the solution after kept back loop
41 is
42 is v1.clear ();
43 is v2.clear ();
44 is
45 int A, B;
46 is
47 the while (D [ 0 !] = Maxx) // first pretreatment Solutions for finding out portion
48 {
49 A = D [ 0 ]; // A first number is
50 B = D [ . 1 ]; // B is the second character
51 is
52 is d.pop_front ();
53 is D. pop_front ();
54 is // by title intended for operation deque wave
55 IF (A> B)
56 is {
57 is d.push_front (A);
58 d.push_back (B);
59 }
60 the else
61 is {
62 is d.push_front (B);
63 is d.push_back (A);
64 }
65
66 v1.push_back ({A, } B); // the solution introduced into the array v1
67
68 }
69
70 // for (int I = 0; I <v1.size (); I ++) // can look at the front part of the solution
71 is // COUT << V1 [I] .first << "" << V1 [I] .second << endl;
72
73 is LL int= N- xunhuan . 1 ; // cycle after a portion of the section length
74
75 LL int m = v1.size (); // length of the front portion of the solution
76
77 for ( int I = 0 ; I <xunhuan; I ++) // the loop processing section
78 {
79 A = D [ 0 ];
80 B = D [ . 1 ];
81 d.pop_front ();
82 d.pop_front ();
83
84 IF (A> B)
85 {
86 d.push_front (A);
87 d.push_back (B);
88 }
89 the else
90 {
91 is d.push_front (B);
92 d.push_back (A);
93 }
94
95 v2.push_back ({A, B});
96 }
97
98 / / for (int I = 0; I <v2.size (); I ++) // portion of the cycle can look at the rear portion of the solution
99 // COUT << V2 [I] .first << "" << V2 [ I] .second << endl;
100
101 LL int caozuo;
102
103 for ( int I = 0; I <OP; I ++ )
104 {
105 CIN >> caozuo;
106
107 IF (caozuo> = . 1 && caozuo <= v1.size ()) // is the solution of the first part
108 {
109 COUT << V1 [caozuo- . 1 ] .first << " " << V1 [caozuo- . 1 ] .second << endl;
110 }
111 the else
112 {
113 caozuo- v1.size = (); // here again to lose film loop section, %% %
114 caozuo% = xunhuan;
115 IF(caozuo==0)//0-1越界了,特判一下orzorz
116 cout<<v2[v2.size()-1].first<<" "<<v2[v2.size()-1].second<<endl;
117 else
118 cout<<v2[caozuo-1].first<<" "<<v2[caozuo-1].second<<endl;
119 }
120
121 }
122
123 return 0;
124 }