Advertisement
Um_nik

spoj_wrapper_4

Dec 4th, 2021
894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <spoj.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <vector>
  7. #include <cassert>
  8. #include <string>
  9. #include <cstdint>
  10. #include <limits>
  11. using namespace std;
  12. typedef pair<int, int> pii;
  13. typedef long long ll;
  14. #define mp make_pair
  15.  
  16. bool readToken(FILE* stream, string &s, int maxLen) {
  17.     int c = ' ';
  18.     s = "";
  19.     while(isspace(c)) c = getc(stream);
  20.     if (c == EOF) return false;
  21.     while(!isspace(c) && c != EOF) {
  22.         s.push_back((char)c);
  23.         if ((int)s.length() > maxLen) return false;
  24.         c = getc(stream);
  25.     }
  26.     return true;
  27. }
  28. bool myToULL(string s, uint64_t &x) {
  29.     if (s.empty()) return false;
  30.     if (s == "0") {
  31.         x = 0;
  32.         return true;
  33.     }
  34.     if (s[0] == '0') return false;
  35.     for (char c : s) if (!isdigit(c)) return false;
  36.     if ((int)s.length() > 19) return false;
  37.     uint64_t y = 0;
  38.     for (char c : s) y = y * 10 + (uint64_t)(c - '0');
  39.     return true;
  40. }
  41. bool readLong(FILE* stream, int64_t &x) {
  42.     string s;
  43.     if (!readToken(stream, s, 20)) return false;
  44.     ll sgn = 1;
  45.     if (s[0] == '-') {
  46.         sgn *= -1;
  47.         s = s.substr(1, (int)s.length() - 1);
  48.     }
  49.     uint64_t y = 0;
  50.     if (!myToULL(s, y)) return false;
  51.     if (y > ((uint64_t)1 << 63)) return false;
  52.     if (y == 0 && sgn == -1) return false;
  53.     if (y == ((uint64_t)1 << 63)) {
  54.         if (sgn == -1) {
  55.             x = 1;
  56.             x <<= 62;
  57.             x *= -1;
  58.             x += x;
  59.             return true;
  60.         } else {
  61.             return false;
  62.         }
  63.     }
  64.     x = sgn * (int64_t)y;
  65.     return true;
  66. }
  67. bool readLong(FILE* stream, int64_t &x, int64_t L, int64_t R) {
  68.     if (!readLong(stream, x)) return false;
  69.     if (x < L || x > R) return false;
  70.     return true;
  71. }
  72. bool readInt(FILE* stream, int &x, int L, int R) {
  73.     int64_t xx = 0;
  74.     if (!readLong(stream, xx, L, R)) return false;
  75.     x = xx;
  76.     return true;
  77. }
  78. const int JURY = 0;
  79. const int PART = 1;
  80. const int INPUT = 2;
  81. void myAssert(int who, bool f) {
  82.     if (who == JURY || who == INPUT)
  83.         assert(f);
  84.     else
  85.         spoj_assert(f);
  86. }
  87. struct InStream {
  88.     int who;
  89.     FILE* stream;
  90.  
  91.     InStream() : who(-1), stream() {}
  92.     InStream(int _who) : who(_who) {
  93.         if (who == JURY) {
  94.             stream = spoj_p_out;
  95.         } else if (who == PART) {
  96.             stream = spoj_t_out;
  97.         } else if (who == INPUT) {
  98.             stream = spoj_p_in;
  99.         } else assert(false);
  100.     }
  101.  
  102.     void Assert(bool f) {
  103.         myAssert(who, f);
  104.     }
  105.     int readInt(int L, int R) {
  106.         int x = 0;
  107.         Assert(::readInt(stream, x, L, R));
  108.         return x;
  109.     }
  110.     int readInt() {
  111.         assert(who == INPUT);
  112.         return readInt(numeric_limits<int>::min(), numeric_limits<int>::max());
  113.     }
  114.     int64_t readLong(int64_t L, int64_t R) {
  115.         int64_t x = 0;
  116.         Assert(::readLong(stream, x, L, R));
  117.         return x;
  118.     }
  119.     string readToken(int maxLen) {
  120.         string s = "";
  121.         Assert(::readToken(stream, s, maxLen));
  122.         return s;
  123.     }
  124. } inf, ouf, ans;
  125.  
  126. void initChecker() {
  127.     spoj_init();
  128.     ans = InStream(JURY);
  129.     ouf = InStream(PART);
  130.     inf = InStream(INPUT);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement