Guest User

Untitled

a guest
May 26th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. /**
  4. * @author Minho Kim <minho.kim093@gmail.com>
  5. */
  6. public class FastIO {
  7.  
  8. public static final int DEFAULT_BUFFER_SIZE = 65536;
  9. public static final int DEFAULT_INTEGER_SIZE = 10;
  10. public static final int DEFAULT_LONG_SIZE = 19;
  11. public static final int DEFAULT_WORD_SIZE = 32;
  12. public static final int DEFAULT_LINE_SIZE = 256;
  13. public static final int EOF = -1;
  14.  
  15. private final InputStream in;
  16. private final OutputStream out;
  17.  
  18. private byte[] inBuffer;
  19. private int nextIn, inLength;
  20. private byte[] outBuffer;
  21. private int nextOut;
  22.  
  23. private char[] charBuffer;
  24. private byte[] byteBuffer;
  25.  
  26. public FastIO(InputStream in, OutputStream out, int inBufferSize, int outBufferSize) {
  27. this.in = in;
  28. this.inBuffer = new byte[inBufferSize];
  29. this.nextIn = 0;
  30. this.inLength = 0;
  31.  
  32. this.out = out;
  33. this.outBuffer = new byte[outBufferSize];
  34. this.nextOut = 0;
  35.  
  36. this.charBuffer = new char[DEFAULT_LINE_SIZE];
  37. this.byteBuffer = new byte[DEFAULT_LONG_SIZE];
  38. }
  39.  
  40. public FastIO(InputStream in, OutputStream out) {
  41. this(in, out, DEFAULT_BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
  42. }
  43.  
  44. public FastIO(InputStream in, OutputStream out, int bufferSize) {
  45. this(in, out, bufferSize, bufferSize);
  46. }
  47.  
  48. public char nextChar() {
  49. return (char) read();
  50. }
  51.  
  52. public String next() {
  53. byte b;
  54. while (isSpace(b = read()))
  55. ;
  56.  
  57. int pos = 0;
  58. do {
  59. charBuffer[pos++] = (char) b;
  60. } while (!isSpace(b = read()));
  61.  
  62. return new String(charBuffer, 0, pos);
  63. }
  64.  
  65. public String nextLine() {
  66. byte b;
  67. int pos = 0;
  68.  
  69. while (!isLine(b = read()))
  70. charBuffer[pos++] = (char) b;
  71.  
  72. return new String(charBuffer, 0, pos);
  73. }
  74.  
  75. public int nextInt() {
  76. byte b;
  77. while (isSpace(b = read()))
  78. ;
  79.  
  80. boolean negative = false;
  81. int result = b - '0';
  82. if (b == '-') {
  83. negative = true;
  84. result = 0;
  85. }
  86.  
  87. while (isDigit(b = read()))
  88. result = (result * 10) + (b - '0');
  89.  
  90. return negative ? -result : result;
  91. }
  92.  
  93. public long nextLong() {
  94. byte b;
  95. while (isSpace(b = read()))
  96. ;
  97.  
  98. boolean negative = false;
  99. long result = b - '0';
  100. if (b == '-') {
  101. negative = true;
  102. result = 0;
  103. }
  104.  
  105. while (isDigit(b = read()))
  106. result = (result * 10) + (b - '0');
  107.  
  108. return negative ? -result : result;
  109. }
  110.  
  111. public float nextFloat() {
  112. byte b;
  113. while (isSpace(b = read()))
  114. ;
  115.  
  116. int pos = 0;
  117. do {
  118. charBuffer[pos++] = (char) b;
  119. } while (!isSpace(b = read()));
  120.  
  121. return Float.parseFloat(new String(charBuffer, 0, pos));
  122. }
  123.  
  124. public float nextFloat2() {
  125. byte b;
  126. while (isSpace(b = read()))
  127. ;
  128.  
  129. boolean negative = false;
  130. float result = b - '0';
  131. if (b == '-') {
  132. negative = true;
  133. result = 0;
  134. }
  135.  
  136. while (isDigit(b = read()))
  137. result = (result * 10) + (b - '0');
  138.  
  139. float d = 1;
  140. if (b == '.') {
  141. while (isDigit(b = read()))
  142. result += (b - '0') / (d *= 10);
  143. }
  144.  
  145. return negative ? -result : result;
  146. }
  147.  
  148. public double nextDouble() {
  149. byte b;
  150. while (isSpace(b = read()))
  151. ;
  152.  
  153. int pos = 0;
  154. do {
  155. charBuffer[pos++] = (char) b;
  156. } while (!isSpace(b = read()));
  157.  
  158. return Double.parseDouble(new String(charBuffer, 0, pos));
  159. }
  160.  
  161. public double nextDouble2() {
  162. byte b;
  163. while (isSpace(b = read()))
  164. ;
  165.  
  166. boolean negative = false;
  167. double result = b - '0';
  168. if (b == '-') {
  169. negative = true;
  170. result = 0;
  171. }
  172.  
  173. while (isDigit(b = read()))
  174. result = (result * 10) + (b - '0');
  175.  
  176. double d = 1;
  177. if (b == '.') {
  178. while (isDigit(b = read()))
  179. result += (b - '0') / (d *= 10);
  180. }
  181.  
  182. return negative ? -result : result;
  183. }
  184.  
  185. public int[] nextIntArray(int size) {
  186. int[] array = new int[size];
  187. for (int i = 0; i < size; i++)
  188. array[i] = nextInt();
  189.  
  190. return array;
  191. }
  192.  
  193. public long[] nextLongArray(int size) {
  194. long[] array = new long[size];
  195. for (int i = 0; i < size; i++)
  196. array[i] = nextLong();
  197.  
  198. return array;
  199. }
  200.  
  201. public int[][] nextInt2DArray(int Y, int X) {
  202. int[][] array = new int[Y][X];
  203. for (int y = 0; y < Y; y++)
  204. for (int x = 0; x < X; x++)
  205. array[y][x] = nextInt();
  206.  
  207. return array;
  208. }
  209.  
  210. public void print(char c) {
  211. write((byte) c);
  212. }
  213.  
  214. public void print(String s) {
  215. for (int i = 0; i < s.length(); i++)
  216. write((byte) s.charAt(i));
  217. }
  218.  
  219. public void print(int i) {
  220. if (i == 0) {
  221. write((byte) '0');
  222. return;
  223. }
  224. if (i == Integer.MIN_VALUE) {
  225. write((byte) '-');
  226. write((byte) '2');
  227. write((byte) '1');
  228. write((byte) '4');
  229. write((byte) '7');
  230. write((byte) '4');
  231. write((byte) '8');
  232. write((byte) '3');
  233. write((byte) '6');
  234. write((byte) '4');
  235. write((byte) '8');
  236. return;
  237. }
  238.  
  239. if (i < 0) {
  240. write((byte) '-');
  241. i = -i;
  242. }
  243.  
  244. int pos = 0;
  245. while (i > 0) {
  246. byteBuffer[pos++] = (byte) ((i % 10) + '0');
  247. i /= 10;
  248. }
  249.  
  250. while (pos-- > 0)
  251. write(byteBuffer[pos]);
  252. }
  253.  
  254. public void print(long l) {
  255. if (l == 0) {
  256. write((byte) '0');
  257. return;
  258. }
  259. if (l == Long.MIN_VALUE) {
  260. write((byte) '-');
  261. write((byte) '9');
  262. write((byte) '2');
  263. write((byte) '2');
  264. write((byte) '3');
  265. write((byte) '3');
  266. write((byte) '7');
  267. write((byte) '2');
  268. write((byte) '0');
  269. write((byte) '3');
  270. write((byte) '6');
  271. write((byte) '8');
  272. write((byte) '5');
  273. write((byte) '4');
  274. write((byte) '7');
  275. write((byte) '7');
  276. write((byte) '5');
  277. write((byte) '8');
  278. write((byte) '0');
  279. write((byte) '8');
  280. return;
  281. }
  282.  
  283. if (l < 0) {
  284. write((byte) '-');
  285. l = -l;
  286. }
  287.  
  288. int pos = 0;
  289. while (l > 0) {
  290. byteBuffer[pos++] = (byte) ((l % 10) + '0');
  291. l /= 10;
  292. }
  293.  
  294. while (pos-- > 0)
  295. write(byteBuffer[pos]);
  296. }
  297.  
  298. public void print(float f) {
  299. String sf = Float.toString(f);
  300. for (int i = 0; i < sf.length(); i++)
  301. write((byte) sf.charAt(i));
  302. }
  303.  
  304. public void print(double d) {
  305. String sd = Double.toString(d);
  306. for (int i = 0; i < sd.length(); i++)
  307. write((byte) sd.charAt(i));
  308. }
  309.  
  310. public void println(char c) {
  311. print(c);
  312. write((byte) '\n');
  313. }
  314.  
  315. public void println(String s) {
  316. print(s);
  317. write((byte) '\n');
  318. }
  319.  
  320. public void println(int i) {
  321. print(i);
  322. write((byte) '\n');
  323. }
  324.  
  325. public void println(long l) {
  326. print(l);
  327. write((byte) '\n');
  328. }
  329.  
  330. public void println(float f) {
  331. print(f);
  332. write((byte) '\n');
  333. }
  334.  
  335. public void println(double d) {
  336. print(d);
  337. write((byte) '\n');
  338. }
  339.  
  340. public void printf(String format, Object... args) {
  341. String s = String.format(format, args);
  342. for (int i = 0; i < s.length(); i++)
  343. write((byte) s.charAt(i));
  344. }
  345.  
  346. public void fprint(char c) {
  347. print(c);
  348. flushBuffer();
  349. }
  350.  
  351. public void fprint(String s) {
  352. print(s);
  353. flushBuffer();
  354. }
  355.  
  356. public void fprint(int i) {
  357. print(i);
  358. flushBuffer();
  359. }
  360.  
  361. public void fprint(long l) {
  362. print(l);
  363. flushBuffer();
  364. }
  365.  
  366. public void fprint(float f) {
  367. print(f);
  368. flushBuffer();
  369. }
  370.  
  371. public void fprint(double d) {
  372. print(d);
  373. flushBuffer();
  374. }
  375.  
  376. public void fprintf(String format, Object... args) {
  377. printf(format, args);
  378. flushBuffer();
  379. }
  380.  
  381. private byte read() {
  382. if (nextIn >= inLength) {
  383. if ((inLength = fill()) == EOF)
  384. return EOF;
  385. nextIn = 0;
  386. }
  387.  
  388. return inBuffer[nextIn++];
  389. }
  390.  
  391. private void write(byte b) {
  392. if (nextOut >= outBuffer.length)
  393. flushBuffer();
  394.  
  395. outBuffer[nextOut++] = b;
  396. }
  397.  
  398. private int fill() {
  399. try {
  400. return in.read(inBuffer, 0, inBuffer.length);
  401. } catch (Exception e) {
  402. throw new RuntimeException(e);
  403. }
  404. }
  405.  
  406. public void flush() {
  407. if (nextOut == 0)
  408. return;
  409.  
  410. flushBuffer();
  411. }
  412.  
  413. private void flushBuffer() {
  414. try {
  415. out.write(outBuffer, 0, nextOut);
  416. } catch (Exception e) {
  417. throw new RuntimeException(e);
  418. }
  419.  
  420. nextOut = 0;
  421. }
  422.  
  423. public void close() {
  424. flush();
  425. }
  426.  
  427. private boolean isDigit(byte b) {
  428. return b >= '0' && b <= '9';
  429. }
  430.  
  431. private boolean isLine(byte b) {
  432. return b == '\n' || b == '\r' || b == EOF;
  433. }
  434.  
  435. private boolean isSpace(byte b) {
  436. return b == ' ' || b == '\t' || b == '\n' || b == '\r' || b == '\f' || b == EOF;
  437. }
  438. }
Add Comment
Please, Sign In to add comment