Advertisement
Um_nik

spoj_wrapper_3

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