forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ.cpp
More file actions
100 lines (88 loc) · 2.65 KB
/
J.cpp
File metadata and controls
100 lines (88 loc) · 2.65 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
using namespace std;
struct Node {
int x;
Node* next, *prev;
Node(int x = 0) : x(x), next(NULL), prev(NULL) {}
} *head, *tail;
void append(int x) {
if (head == NULL) {
head = tail = new Node(x);
return ;
}
Node *tmp = new Node(x);
tail->next = tmp;
tmp->prev = tail;
tail = tmp;
}
int a[200111];
char tmp[1011];
int main() {
int n, p;
while (scanf("%d%d", &n, &p) == 2) {
head = tail = NULL;
FOR(i,1,n) {
scanf("%d", &a[i]);
append(a[i]);
}
int q; scanf("%d", &q);
--p;
Node* cur = head;
while (p--) {
cur = cur->next;
}
while (q--) {
scanf("%s", tmp);
if (tmp[0] == 'p') {
printf("%d\n", cur->x);
}
else if (tmp[4] == 'L') {
if (cur->prev) cur = cur->prev;
}
else if (tmp[4] == 'R') {
if (cur->next) cur = cur->next;
}
else if (tmp[6] == 'L') {
int tmp; scanf("%d", &tmp);
if (cur->prev) {
Node *x = cur->prev;
Node *y = new Node(tmp);
y->next = cur;
cur->prev = y;
x->next = y;
y->prev = x;
}
else {
Node *y = new Node(tmp);
y->next = cur;
cur->prev = y;
}
}
else {
int tmp; scanf("%d", &tmp);
if (cur->next) {
Node *x = cur->next;
Node *y = new Node(tmp);
y->next = x;
x->prev = y;
cur->next = y;
y->prev = cur;
}
else {
Node *y = new Node(tmp);
cur->next = y;
y->prev = cur;
}
}
}
}
return 0;
}