-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeque.java
More file actions
166 lines (142 loc) · 4.6 KB
/
Copy pathTeque.java
File metadata and controls
166 lines (142 loc) · 4.6 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.*;
public class Teque {
public static void main(String[] args) {
FastIO fio = new FastIO();
int n = fio.nextInt();
int[] a = new int[500000];
int[] b = new int[500000];
int aHead = 0;
int bHead = 0;
int aTail = 0;
int bTail = 0;
int aSize = 0;
int bSize = 0;
for (int i = 0; i < n; i++ ) {
String method = fio.next();
int x = fio.nextInt();
if (method.equals("push_back")) {
// if (bHead == bTail && bHead == 0) {
// b[bHead] = x;
// bTail = (bTail + 1) % 500000;
// bSize = bSize + 1;
// } else {
b[bTail] = x;
bTail = (bTail + 1) % 500000;
bSize = bSize + 1;
if ( bSize - aSize > 0) {
bSize = bSize - 1;
aSize = aSize + 1;
a[aTail] = b[bHead];
bHead = (bHead + 1) % 500000;
aTail = (aTail + 1) % 500000;
}
// }
} else if (method.equals("push_front")) {
// if (aHead == aTail && aHead == 0) {
// a[aHead] = x;
// aTail = aTail + 1 % 500000;
// aSize = aSize + 1;
// } else {
aHead = (aHead - 1 + 500000) % 500000;
a[aHead] = x;
aSize = aSize + 1;
if (aSize - bSize > 1) {
aSize = aSize - 1;
bSize = bSize + 1;
bHead = (bHead - 1 + 500000) % 500000;
b[bHead] = a[(aTail - 1 + 500000) % 500000];
aTail = (aTail - 1 + 500000) % 500000;
}
// }
} else if (method.equals("push_middle")) {
if (aSize <= bSize) {
a[aTail] = x;
aTail = (aTail + 1) % 500000;
aSize = aSize + 1;
} else {
bHead = (bHead - 1 + 500000) % 500000;
b[bHead] = x;
bSize = bSize + 1;
}
} else if (method.equals("get")) {
if (x < aSize) {
fio.println(a[(aHead + x) % 500000]);
} else {
fio.println(b[(bHead + (x - aSize)) % 500000]);
}
} else {
fio.println("Error in method");
}
}
// for (int i = 0; i < a.length ; i ++) {
// if ( a[i] != 0) {
// fio.println("this is in a :" + a[i] +" at index "+i+ " aHead is at " +aHead+" aTail is at " +aTail);
// }
// if (b[i] != 0) {
// fio.println("this is in b :" + b[i]+" at index "+i+" bHead is at " +bHead+" bTail is at " + bTail);
// }
// }
fio.close();
}
}
/**
* Fast I/O
* @source https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/
*/
class FastIO extends PrintWriter
{
BufferedReader br;
StringTokenizer st;
public FastIO()
{
super(new BufferedOutputStream(System.out));
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}