-
Notifications
You must be signed in to change notification settings - Fork 3
/
a.cpp
57 lines (57 loc) · 1.3 KB
/
a.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @date 2022-12-16
* @tags linked list
* @difficulty 1500
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL); // Fast IO
int n, q;
cin >> n >> q;
vector<string> N(2*n);
list<int> L;
// store the iterator for each person for type C query
vector<decltype(L)::iterator> P(2*n);
for (int i = 0; i < n; ++i) {
int a = 2*i, b = 2*i+1;
cin >> N[a] >> N[b];
P[a] = L.insert(L.end(), a);
P[b] = L.insert(L.end(), b);
}
string Q;
cin >> Q;
auto itr = L.begin(); // mic
for (char c : Q) {
switch (c) {
case 'F': itr = prev(itr); break;
case 'B': itr = next(itr); break;
case 'R': {
int p = *itr;
itr = L.erase(itr);
if (itr == L.end()) itr = L.begin();
P[p] = L.insert(L.end(), p);
break;
}
case 'C': {
int p = *itr;
int p2 = p/2*2+1-(p%2);
itr = L.erase(itr);
if (itr == L.end()) itr = L.begin();
P[p] = L.insert(next(P[p2]), p);
break;
}
case 'P': {
int p = *itr;
int p2 = p/2*2+1-(p%2);
cout << N[p2] << '\n';
break;
}
}
}
cout << '\n';
for (itr = L.begin(); itr != L.end(); ++itr) {
cout << N[*itr] << '\n';
}
return 0;
}