Advertisement
gt22

Untitled

Oct 10th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.67 KB | None | 0 0
  1.  
  2. /**
  3.  * Author: Sergey Kopeliovich (Burunduk30@gmail.com)
  4.  */
  5.  
  6. #define VERSION "0.1.5"
  7. #define _GLIBCXX_DEBUG
  8. #include <cassert>
  9. #include <cstdio>
  10. #include <algorithm>
  11.  
  12. /** Fast allocation */
  13.  
  14. #ifdef FAST_ALLOCATOR_MEMORY
  15. int allocator_pos = 0;
  16.     char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
  17.     inline void * operator new ( size_t n ) {
  18.         char *res = allocator_memory + allocator_pos;
  19.         allocator_pos += n;
  20.         assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  21.         return (void *)res;
  22.     }
  23.     inline void operator delete ( void * ) noexcept { }
  24.     //inline void * operator new [] ( size_t ) { assert(0); }
  25.     //inline void operator delete [] ( void * ) { assert(0); }
  26. #endif
  27.  
  28. /** Fast input-output */
  29.  
  30. template <class T = int> inline T readInt();
  31. inline double readDouble();
  32. inline int readUInt();
  33. inline int readChar(); // first non-blank character
  34. inline void readWord( char *s );
  35. inline bool readLine( char *s ); // do not save '\n'
  36. inline bool isEof();
  37. inline int getChar();
  38. inline int peekChar();
  39. inline bool seekEof();
  40. inline void skipBlanks();
  41.  
  42. template <class T> inline void writeInt( T x, char end = 0, int len = -1 );
  43. inline void writeChar( int x );
  44. inline void writeWord( const char *s );
  45. inline void writeDouble( double x, int len = 0 ); // works correct only for |x| < 2^{63}
  46. inline void flush();
  47.  
  48. static struct buffer_flusher_t {
  49.     ~buffer_flusher_t() {
  50.         flush();
  51.     }
  52. } buffer_flusher;
  53.  
  54. /** Read */
  55.  
  56. static const int buf_size = 4096;
  57.  
  58. static unsigned char buf[buf_size];
  59. static int buf_len = 0, buf_pos = 0;
  60.  
  61. inline bool isEof() {
  62.     if (buf_pos == buf_len) {
  63.         buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
  64.         if (buf_pos == buf_len)
  65.             return 1;
  66.     }
  67.     return 0;
  68. }
  69.  
  70. inline int getChar() {
  71.     return isEof() ? -1 : buf[buf_pos++];
  72. }
  73.  
  74. inline int peekChar() {
  75.     return isEof() ? -1 : buf[buf_pos];
  76. }
  77.  
  78. inline bool seekEof() {
  79.     int c;
  80.     while ((c = peekChar()) != -1 && c <= 32)
  81.         buf_pos++;
  82.     return c == -1;
  83. }
  84.  
  85. inline void skipBlanks() {
  86.     while (!isEof() && buf[buf_pos] <= 32U)
  87.         buf_pos++;
  88. }
  89.  
  90. inline int readChar() {
  91.     int c = getChar();
  92.     while (c != -1 && c <= 32)
  93.         c = getChar();
  94.     return c;
  95. }
  96.  
  97. inline int readUInt() {
  98.     int c = readChar(), x = 0;
  99.     while ('0' <= c && c <= '9')
  100.         x = x * 10 + c - '0', c = getChar();
  101.     return x;
  102. }
  103.  
  104. template <class T>
  105. inline T readInt() {
  106.     int s = 1, c = readChar();
  107.     T x = 0;
  108.     if (c == '-')
  109.         s = -1, c = getChar();
  110.     else if (c == '+')
  111.         c = getChar();
  112.     while ('0' <= c && c <= '9')
  113.         x = x * 10 + c - '0', c = getChar();
  114.     return s == 1 ? x : -x;
  115. }
  116.  
  117. inline double readDouble() {
  118.     int s = 1, c = readChar();
  119.     double x = 0;
  120.     if (c == '-')
  121.         s = -1, c = getChar();
  122.     while ('0' <= c && c <= '9')
  123.         x = x * 10 + c - '0', c = getChar();
  124.     if (c == '.') {
  125.         c = getChar();
  126.         double coef = 1;
  127.         while ('0' <= c && c <= '9')
  128.             x += (c - '0') * (coef *= 1e-1), c = getChar();
  129.     }
  130.     return s == 1 ? x : -x;
  131. }
  132.  
  133. inline void readWord( char *s ) {
  134.     int c = readChar();
  135.     while (c > 32)
  136.         *s++ = c, c = getChar();
  137.     *s = 0;
  138. }
  139.  
  140. inline bool readLine( char *s ) {
  141.     int c = getChar();
  142.     while (c != '\n' && c != -1)
  143.         *s++ = c, c = getChar();
  144.     *s = 0;
  145.     return c != -1;
  146. }
  147.  
  148. /** Write */
  149.  
  150. static int write_buf_pos = 0;
  151. static char write_buf[buf_size];
  152.  
  153. inline void writeChar( int x ) {
  154.     if (write_buf_pos == buf_size)
  155.         fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  156.     write_buf[write_buf_pos++] = x;
  157. }
  158.  
  159. inline void flush() {
  160.     if (write_buf_pos) {
  161.         fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
  162.         fflush(stdout);
  163.     }
  164. }
  165.  
  166. template <class T>
  167. inline void writeInt( T x, char end, int output_len ) {
  168.     if (x < 0)
  169.         writeChar('-'), x = -x;
  170.  
  171.     char s[24];
  172.     int n = 0;
  173.     while (x || !n)
  174.         s[n++] = '0' + x % 10, x /= 10;
  175.     while (n < output_len)
  176.         s[n++] = '0';
  177.     while (n--)
  178.         writeChar(s[n]);
  179.     if (end)
  180.         writeChar(end);
  181. }
  182.  
  183. inline void writeWord( const char *s ) {
  184.     while (*s)
  185.         writeChar(*s++);
  186. }
  187.  
  188. inline void writeDouble( double x, int output_len ) {
  189.     if (x < 0)
  190.         writeChar('-'), x = -x;
  191.     assert(x <= (1LLU << 63) - 1);
  192.     long long t = (long long)x;
  193.     writeInt(t), x -= t;
  194.     writeChar('.');
  195.     for (int i = output_len - 1; i > 0; i--) {
  196.         x *= 10;
  197.         t = std::min(9, (int)x);
  198.         writeChar('0' + t), x -= t;
  199.     }
  200.     x *= 10;
  201.     t = std::min(9, (int)(x + 0.5));
  202.     writeChar('0' + t);
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement