Guest User

Untitled

a guest
Mar 17th, 2021
106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <unistd.h>
  2.  
  3. template <class T> using V = vector<T>;
  4. template <class T> using VV = V<V<T>>;
  5. struct Scanner {
  6. int fd = -1;
  7. char line[(1 << 15) + 1];
  8. size_t st = 0, ed = 0;
  9. void reread() {
  10. memmove(line, line + st, ed - st);
  11. ed -= st;
  12. st = 0;
  13. ed += ::read(fd, line + ed, (1 << 15) - ed);
  14. line[ed] = '\0';
  15. }
  16. bool succ() {
  17. while (true) {
  18. if (st == ed) {
  19. reread();
  20. if (st == ed) return false;
  21. }
  22. while (st != ed && isspace(line[st])) st++;
  23. if (st != ed) break;
  24. }
  25. if (ed - st <= 50) {
  26. bool sep = false;
  27. for (size_t i = st; i < ed; i++) {
  28. if (isspace(line[i])) {
  29. sep = true;
  30. break;
  31. }
  32. }
  33. if (!sep) reread();
  34. }
  35. return true;
  36. }
  37. template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
  38. bool read_single(T& ref) {
  39. if (!succ()) return false;
  40. while (true) {
  41. size_t sz = 0;
  42. while (st + sz < ed && !isspace(line[st + sz])) sz++;
  43. ref.append(line + st, sz);
  44. st += sz;
  45. if (!sz || st != ed) break;
  46. reread();
  47. }
  48. return true;
  49. }
  50. template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  51. bool read_single(T& ref) {
  52. if (!succ()) return false;
  53. bool neg = false;
  54. if (line[st] == '-') {
  55. neg = true;
  56. st++;
  57. }
  58. ref = T(0);
  59. while (isdigit(line[st])) {
  60. ref = 10 * ref + (line[st++] & 0xf);
  61. }
  62. if (neg) ref = -ref;
  63. return true;
  64. }
  65. template <class T> bool read_single(V<T>& ref) {
  66. for (auto& d : ref) {
  67. if (!read_single(d)) return false;
  68. }
  69. return true;
  70. }
  71. template <class T> bool read_single(array<T,2>& ref) {
  72. if (!read_single(ref[0])) return false;
  73. if (!read_single(ref[1])) return false;
  74. return true;
  75. }
  76. template <class T> bool read_single(array<T,3>& ref) {
  77. if (!read_single(ref[0])) return false;
  78. if (!read_single(ref[1])) return false;
  79. if (!read_single(ref[2])) return false;
  80. return true;
  81. }
  82. template <class T> bool read_single(array<T,4>& ref) {
  83. if (!read_single(ref[0])) return false;
  84. if (!read_single(ref[1])) return false;
  85. if (!read_single(ref[2])) return false;
  86. if (!read_single(ref[3])) return false;
  87. return true;
  88. }
  89. void read() {}
  90. template <class H, class... T> void read(H& h, T&... t) {
  91. bool f = read_single(h);
  92. assert(f);
  93. read(t...);
  94. }
  95. Scanner(FILE* fp) : fd(fileno(fp)) {}
  96. };
  97.  
  98. struct Printer {
  99. public:
  100. template <bool F = false> void write() {}
  101. template <bool F = false, class H, class... T>
  102. void write(const H& h, const T&... t) {
  103. if (F) write_single(' ');
  104. write_single(h);
  105. write<true>(t...);
  106. }
  107. template <class... T> void writeln(const T&... t) {
  108. write(t...);
  109. write_single('\n');
  110. }
  111.  
  112. Printer(FILE* _fp) : fp(_fp) {}
  113. ~Printer() { flush(); }
  114.  
  115. private:
  116. static constexpr size_t SIZE = 1 << 15;
  117. FILE* fp;
  118. char line[SIZE], small[50];
  119. size_t pos = 0;
  120. void flush() {
  121. fwrite(line, 1, pos, fp);
  122. pos = 0;
  123. }
  124. void write_single(const char& val) {
  125. if (pos == SIZE) flush();
  126. line[pos++] = val;
  127. }
  128. template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  129. void write_single(T val) {
  130. if (pos > (1 << 15) - 50) flush();
  131. if (val == 0) {
  132. write_single('0');
  133. return;
  134. }
  135. if (val < 0) {
  136. write_single('-');
  137. val = -val; // todo min
  138. }
  139. size_t len = 0;
  140. while (val) {
  141. small[len++] = char(0x30 | (val % 10));
  142. val /= 10;
  143. }
  144. for (size_t i = 0; i < len; i++) {
  145. line[pos + i] = small[len - 1 - i];
  146. }
  147. pos += len;
  148. }
  149. void write_single(const string& s) {
  150. for (char c : s) write_single(c);
  151. }
  152. void write_single(const char* s) {
  153. size_t len = strlen(s);
  154. for (size_t i = 0; i < len; i++) write_single(s[i]);
  155. }
  156. template <class T> void write_single(const V<T>& val) {
  157. auto n = val.size();
  158. for (size_t i = 0; i < n; i++) {
  159. if (i) write_single(' ');
  160. write_single(val[i]);
  161. }
  162. }
  163. };
  164.  
  165. Scanner sc = Scanner(stdin);
  166. Printer pr = Printer(stdout);
RAW Paste Data