Advertisement
Emiliatan

fastIO (zj only)

Nov 25th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #ifdef __GNUC__
  2.         #include <unistd.h>
  3.         #include <fcntl.h>
  4.         #include <sys/types.h>
  5.         #include <sys/stat.h>
  6.  
  7.         #define fread fread_unlocked
  8.         #define putchar putchar_unlocked
  9. #else
  10.         #define getc getchar
  11. #endif
  12.  
  13. namespace IO
  14. {
  15.         constexpr int buf_s = 1 << 20;
  16.     static char ch;
  17.     static bool negative;
  18.     inline int getc()
  19.     {
  20.         static char buf[buf_s], *p1 = buf, *p2 = buf;
  21.         return p1 == p2 && (p2 = (p1 = buf) + read(STDIN_FILENO, buf, buf_s), p1 == p2) ? EOF : *p1++;
  22.     }
  23.     inline int nextInt(int &out)
  24.     {
  25.         if (ch == EOF) return -1;
  26.  
  27.         static int tmp;
  28.         tmp = 0;
  29.         negative = false;
  30.  
  31.         ch = getc();
  32.         if (ch != '-' && (ch < '0' || ch > '9'))
  33.             return (ch == EOF ? -1 : ch);
  34.         (ch == '-' ? negative = true : tmp = ch - '0');
  35.  
  36.         while ((ch = getc()) && ch >= '0' && ch <= '9')
  37.             tmp = tmp * 10 + ch - '0';
  38.  
  39.         out = (negative ? -tmp : tmp);
  40.  
  41.         return 1;
  42.     }
  43.     inline int nextUint(uint &out)
  44.     {
  45.         if (ch == EOF) return -1;
  46.  
  47.         static uint tmp;
  48.         tmp = 0;
  49.  
  50.         while ((ch = getc()) && ch >= '0' && ch <= '9')
  51.             tmp = tmp * 10 + ch - '0';
  52.  
  53.         out = tmp;
  54.  
  55.         return 1;
  56.     }
  57.  
  58.     inline int nextInt64(int64 &out)
  59.     {
  60.         if (ch == EOF) return -1;
  61.  
  62.         static int64 tmp;
  63.         tmp = 0;
  64.         negative = false;
  65.  
  66.         ch = getc();
  67.         if (ch != '-' && (ch < '0' || ch > '9'))
  68.             return (ch == EOF ? -1 : ch);
  69.         (ch == '-' ? negative = true : tmp = (int64)ch - '0');
  70.  
  71.         while ((ch = getc()) && ch >= '0' && ch <= '9')
  72.             tmp = tmp * 10 + ch - '0';
  73.  
  74.         out = (negative ? -tmp : tmp);
  75.  
  76.         return 1;
  77.     }
  78.     inline int nextUint64(uint64 &out)
  79.     {
  80.         if (ch == EOF) return -1;
  81.  
  82.         static uint64 tmp;
  83.         tmp = 0;
  84.  
  85.         while ((ch = getc()) && ch >= '0' && ch <= '9')
  86.             tmp = tmp * 10 + ch - '0';
  87.  
  88.         out = tmp;
  89.         return 1;
  90.     }
  91.  
  92.     inline int nextDouble(double &out)
  93.     {
  94.         if (ch == EOF) return -1;
  95.  
  96.         static double tmp;
  97.         double len = 0.1;
  98.         negative = false;
  99.  
  100.         tmp = 0.0;
  101.  
  102.         ch = getc();
  103.         if (ch != '-' && (ch < '0' || ch > '9'))
  104.             return (ch == EOF ? -1 : ch);
  105.         (ch == '-' ? negative = true : tmp = (double)ch - '0');
  106.  
  107.         while ((ch = getc()) && ch >= '0' && ch <= '9')
  108.             tmp = tmp * 10 + ch - '0';
  109.  
  110.         if (ch == '.')
  111.             while ((ch = getc()) && ch >= '0' && ch <= '9')
  112.                 tmp += (double)(ch - '0') * len, len *= 0.1;
  113.  
  114.         out = (negative ? -tmp : tmp);
  115.  
  116.         return 1;
  117.     }
  118.  
  119.     inline int nextChar(char &out)
  120.     {
  121.         ch = getc();
  122.         return (ch == EOF ? -1 : out = ch);
  123.     }
  124.  
  125.     static char s[25];
  126.     static int16 index;
  127.     inline void printInt(int x)
  128.     {
  129.         if (x == 0) putchar('0');
  130.         negative = (x < 0 && (x = -x));
  131.  
  132.         index = 0;
  133.         while (x) s[index++] = x % 10 + '0', x /= 10;
  134.  
  135.         (negative ? s[index++] = '-' : s[index]);
  136.  
  137.         while (index) putchar(s[--index]);
  138.     }
  139.     inline void printUint(uint x)
  140.     {
  141.         if (x == 0) putchar('0');
  142.  
  143.         index = 0;
  144.         while (x) s[index++] = x % 10 + '0', x /= 10;
  145.  
  146.         while (index) putchar(s[--index]);
  147.     }
  148.  
  149.     inline void printInt64(int64 x)
  150.     {
  151.         if (x == 0) putchar('0');
  152.         negative = (x < 0 && (x = -x));
  153.  
  154.         index = 0;
  155.         while (x) s[index++] = x % 10 + '0', x /= 10;
  156.  
  157.         (negative ? s[index++] = '-' : s[index]);
  158.  
  159.         while (index) putchar(s[--index]);
  160.     }
  161.     inline void printUint64(uint64 x)
  162.     {
  163.         if (x == 0) putchar('0');
  164.  
  165.         index = 0;
  166.         while (x) s[index++] = x % 10 + '0', x /= 10;
  167.  
  168.         while (index) putchar(s[--index]);
  169.     }
  170.     inline void printChar(char ch)
  171.     {
  172.         putchar(ch);
  173.     }
  174.  
  175.     inline void printString(const char *p)
  176.     {
  177.         while (*p)
  178.             putchar(*(p++));
  179.     }
  180.  
  181. }
  182.  
  183. using IO::nextInt;
  184. using IO::printInt;
  185.  
  186. using IO::nextUint;
  187. using IO::printUint;
  188.  
  189. using IO::nextInt64;
  190. using IO::printInt64;
  191.  
  192. using IO::nextUint64;
  193. using IO::printUint64;
  194.  
  195. using IO::nextDouble;
  196.  
  197. using IO::nextChar;
  198. using IO::printChar;
  199. using IO::printString;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement