-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastReader&Writer.txt
More file actions
358 lines (319 loc) · 12.1 KB
/
Copy pathFastReader&Writer.txt
File metadata and controls
358 lines (319 loc) · 12.1 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
static final class FastWriter {
private final PrintWriter printWriter;
private FastWriter(final PrintWriter printWriter) {
this.printWriter = printWriter;
}
/**
* Returns a {@link FastWriter} instance that prints output by {@code outputStreamWriter}.
*
* @param outputStream
* @return Returns a {@link FastWriter} instance that prints output by {@code outputStreamWriter}.
*/
public static final FastWriter from(final OutputStream outputStream) {
return new FastWriter(new PrintWriter(new OutputStreamWriter(outputStream)));
}
/**
* Terminates the current line by writing the line separator string.
*/
public void println() {
this.printWriter.println();
this.printWriter.flush();
}
/**
* Prints a boolean value and then terminate the line.
*
* @param x The <code>boolean</code> to be printed.
*/
public void println(boolean x) {
this.printWriter.println(x);
this.printWriter.flush();
}
/**
* Prints a character and then terminate the line.
*
* @param x The <code>char</code> to be printed.
*/
public void println(char x) {
this.printWriter.println(x);
this.printWriter.flush();
}
/**
* Prints an integer and then terminate the line.
*
* @param x The <code>int</code> to be printed.
*/
public void println(int x) {
this.printWriter.println(x);
this.printWriter.flush();
}
/**
* Prints a long integer and then terminate the line.
*
* @param x The <code>long</code> to be printed.
*/
public void println(long x) {
this.printWriter.println(x);
this.printWriter.flush();
}
/**
* Prints a floating-point number and then terminate the line.
*
* @param x The <code>float</code> to be printed.
*/
public void println(float x) {
this.printWriter.println(x);
this.printWriter.flush();
}
/**
* Prints an double-precision floating-point number and then terminate the line.
*
* @param x The <code>double</code> to be printed.
*/
public void println(double x) {
this.printWriter.println(x);
this.printWriter.flush();
}
/**
* Prints an array of characters and then terminate the line.
*
* @param x The array of <code>char</code> to be printed.
*/
public void println(char[] x) {
this.printWriter.println(x);
this.printWriter.flush();
}
/**
* Prints a String and then terminate the line.
*
* @param x The <code>String</code> value to be printed.
*/
public void println(String x) {
this.printWriter.println(x);
this.printWriter.flush();
}
/**
* Prints an Object and then terminate the line.
*
* @param x The <code>Object</code> to be printed.
*/
public void println(Object x) {
this.printWriter.println(x);
this.printWriter.flush();
}
/**
* Prints a boolean.
*
* @param x The <code>boolean</code> to be printed.
*/
public void print(boolean x) {
this.printWriter.print(x);
this.printWriter.flush();
}
/**
* Prints a character.
*
* @param x The <code>char</code> to be printed.
*/
public void print(char x) {
this.printWriter.print(x);
this.printWriter.flush();
}
/**
* Prints an integer.
*
* @param x The <code>int</code> to be printed.
*/
public void print(int x) {
this.printWriter.print(x);
this.printWriter.flush();
}
/**
* Prints a long integer.
*
* @param x The <code>long</code> to be printed.
*/
public void print(long x) {
this.printWriter.print(x);
this.printWriter.flush();
}
/**
* Prints a floating-point number.
*
* @param x The <code>float</code> to be printed.
*/
public void print(float x) {
this.printWriter.print(x);
this.printWriter.flush();
}
/**
* Prints an double-precision floating-point number.
*
* @param x The <code>double</code> to be printed.
*/
public void print(double x) {
this.printWriter.print(x);
this.printWriter.flush();
}
/**
* Prints an array of characters.
*
* @param x The array of <code>char</code> to be printed.
*/
public void print(char[] x) {
this.printWriter.print(x);
this.printWriter.flush();
}
/**
* Prints a String.
*
* @param x The <code>String</code> value to be printed.
*/
public void print(String x) {
this.printWriter.print(x);
this.printWriter.flush();
}
/**
* Prints an Object.
*
* @param x The <code>Object</code> to be printed.
*/
public void print(Object x) {
this.printWriter.print(x);
this.printWriter.flush();
}
/**
* Closes the output stream
*/
public void close() {
this.printWriter.close();
}
}
static final class FastReader {
private final BufferedReader bufferedReader;
/* legacy class preferred over String#split and Scanner for performance */
private StringTokenizer tokenizer;
private FastReader(final BufferedReader bufferedReader) {
this.bufferedReader = bufferedReader;
this.tokenizer = null;
}
/**
* Returns a {@link FastReader} instance that reads input from {@code inputStream}.
*
* @param inputStream
* @return Returns a {@link FastReader} instance that reads input from {@code inputStream}.
*/
public static final FastReader from(final InputStream inputStream) {
return new FastReader(new BufferedReader(new InputStreamReader(inputStream)));
}
/**
* Returns the next word acquired by {@link StringTokenizer}.
* Moves on to the next line if the current line has been processed.
*
* @return Returns the next word acquired by {@link StringTokenizer},
* or null if end of stream has been reached.
* @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}.
*/
public String next() {
return tokenize() ? tokenizer.nextToken() : null;
}
/**
* Checks to see if there are any more words left in the {@code inputStream}.
* Can be used to check if end of stream has been reached, as well.
* If required, reads another line from the {@code inputStream}; i.e this operation
* might perform an I/O; possibly block if end of stream is not reached but stream
* is not yet available to yield a new line.
*
* @return Returns true if there are more words to read in the {@code inputStream}
* and end of stream has not been reached. False otherwise.
* @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}.
*/
public boolean canReadMore() {
return tokenize();
}
private boolean tokenize() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
// read a line, see if end of stream has been reached
String line = null;
try {
if ((line = bufferedReader.readLine()) == null) return false;
} catch (IOException unexpected) {
throw new RuntimeException(unexpected);
}
tokenizer = new StringTokenizer(line);
}
return true;
}
/**
* Returns the next {@code int} acquired by {@link StringTokenizer}
* using {@link Integer#parseInt(String)} on {@link #next()}.
* Moves on to the next line if the current line has been processed.
*
* @return Returns the next {@code int} acquired by {@link StringTokenizer}.
* @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}.
* @throws NumberFormatException If an invalid input is encountered or end of stream has been reached.
*/
public int nextInt() {
return Integer.parseInt(next());
}
/**
* Returns the next {@code long} acquired by {@link StringTokenizer}
* using {@link Long#parseLong(String)} on {@link #next()}.
* Moves on to the next line if the current line has been processed.
*
* @return Returns the next {@code long} acquired by {@link StringTokenizer}.
* @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}.
* @throws NumberFormatException If an invalid input is encountered or end of stream has been reached.
*/
public long nextLong() {
return Long.parseLong(next());
}
/**
* Returns the next {@code double} acquired by {@link StringTokenizer}
* using {@link Double#parseDouble(String)} on {@link #next()}.
* Moves on to the next line if the current line has been processed.
*
* @return Returns the next {@code double} acquired by {@link StringTokenizer}.
* @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}.
* @throws NumberFormatException If an invalid input is encountered or end of stream has been reached.
*/
public double nextDouble() {
return Double.parseDouble(next());
}
/**
* Returns the next {@link BigDecimal} acquired by {@link StringTokenizer}
* using BigDecimal's String constructor on {@link #next()}.
* Moves on to the next line if the current line has been processed.
*
* @return Returns the next {@code BigDecimal} acquired by {@link StringTokenizer}.
* @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}.
* @throws NumberFormatException If an invalid input is encountered or end of stream has been reached.
*/
public BigDecimal nextBigDecimal() {
return new BigDecimal(next());
}
/**
* Returns the next {@link BigInteger} acquired by {@link StringTokenizer}
* using BigInteger's String constructor on {@link #next()}.
* Moves on to the next line if the current line has been processed.
*
* @return Returns the next {@code BigInteger} acquired by {@link StringTokenizer}.
* @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}.
* @throws NumberFormatException If an invalid input is encountered or end of stream has been reached.
*/
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
/**
* Closes the input stream.
*
* @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}.
* @see java.io.BufferedReader#close()
*/
public void close() {
try {
bufferedReader.close();
} catch (IOException unexpected) {
throw new RuntimeException(unexpected);
}
}
}