<aside> ⚠️ This code is untested
</aside>
The purpose of this code is to make porting testlib checkers into spoj checkers as painless as possible.
Work in progress, is not tested, use at your own risk.
#include <spoj.h>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cassert>
#include <string>
#include <cstdint>
#include <limits>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define mp make_pair
bool readToken(FILE* stream, string &s, int maxLen) {
int c = ' ';
s = "";
while(isspace(c)) c = getc(stream);
if (c == EOF) return false;
while(!isspace(c) && c != EOF) {
s.push_back((char)c);
if ((int)s.length() > maxLen) return false;
c = getc(stream);
}
return true;
}
bool myToULL(string s, uint64_t &x) {
if (s.empty()) return false;
if (s == "0") {
x = 0;
return true;
}
if (s[0] == '0') return false;
for (char c : s) if (!isdigit(c)) return false;
if ((int)s.length() > 19) return false;
x = 0;
for (char c : s) x = x * 10 + (uint64_t)(c - '0');
return true;
}
bool readLong(FILE* stream, int64_t &x) {
string s;
if (!readToken(stream, s, 20)) return false;
ll sgn = 1;
if (s[0] == '-') {
sgn *= -1;
s = s.substr(1, (int)s.length() - 1);
}
uint64_t y = 0;
if (!myToULL(s, y)) return false;
if (y > ((uint64_t)1 << 63)) return false;
if (y == 0 && sgn == -1) return false;
if (y == ((uint64_t)1 << 63)) {
if (sgn == -1) {
x = 1;
x <<= 62;
x *= -1;
x += x;
return true;
} else {
return false;
}
}
x = sgn * (int64_t)y;
return true;
}
bool readLong(FILE* stream, int64_t &x, int64_t L, int64_t R) {
if (!readLong(stream, x)) return false;
if (x < L || x > R) return false;
return true;
}
bool readInt(FILE* stream, int &x, int L, int R) {
int64_t xx = 0;
if (!readLong(stream, xx, L, R)) return false;
x = xx;
return true;
}
const int JURY = 0;
const int PART = 1;
const int INPUT = 2;
void myAssert(int who, bool f) {
if (who == JURY || who == INPUT)
assert(f);
else
spoj_assert(f);
}
struct InStream {
int who;
FILE* stream;
InStream() : who(-1), stream() {}
InStream(int _who) : who(_who) {
if (who == JURY) {
stream = spoj_p_out;
} else if (who == PART) {
stream = spoj_t_out;
} else if (who == INPUT) {
stream = spoj_p_in;
} else assert(false);
}
void Assert(bool f) {
myAssert(who, f);
}
int readInt(int L, int R) {
int x = 0;
Assert(::readInt(stream, x, L, R));
return x;
}
int readInt() {
assert(who == INPUT);
return readInt(numeric_limits<int>::min(), numeric_limits<int>::max());
}
int64_t readLong(int64_t L, int64_t R) {
int64_t x = 0;
Assert(::readLong(stream, x, L, R));
return x;
}
string readToken(int maxLen) {
string s = "";
Assert(::readToken(stream, s, maxLen));
return s;
}
} inf, ouf, ans;
void initChecker() {
spoj_init();
ans = InStream(JURY);
ouf = InStream(PART);
inf = InStream(INPUT);
}