Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define all(x) begin(x),end(x)
- #define PROBLEM_TYPE 0
- using namespace std;
- using ll = long long;
- enum {
- INTERACTIVE = 0,
- BINARY_FORMAT = 1,
- TEXT_FORMAT = 2
- };
- #if PROBLEM_TYPE == 1
- class BinaryIO {
- private:
- template<typename T = int>
- int readIntInBigEndian(const char *buff, int sizeOf = 4) {
- unsigned long long ans = 0;
- for (int i = 0; i < sizeOf; i++) {
- ans = (ans << 8) ^ uint8_t(buff[i]);
- }
- return static_cast<T>(ans);
- }
- template<typename T = int>
- int readIntInLittleEndian(const char *buff, int sizeOf = 4) {
- unsigned long long ans = 0;
- for (int i = sizeOf - 1; i >= 0; i--) {
- ans = (ans << 8) ^ uint8_t(buff[i]);
- }
- return static_cast<T>(ans);
- }
- template<typename T = int>
- T readInteger() {
- constexpr int sizeOf = sizeof(T);
- char buff[sizeOf];
- in.read(buff, sizeOf);
- if (type == UNDEF) {
- if (abs(readIntInBigEndian<T>(buff, sizeOf)) < abs(readIntInLittleEndian<T>(buff, sizeOf))) {
- type = BIG_ENDIAN;
- } else {
- type = LITTLE_ENDIAN;
- }
- }
- if (type == BIG_ENDIAN) {
- return readIntInBigEndian<T>(buff, sizeOf);
- } else {
- return readIntInLittleEndian<T>(buff, sizeOf);
- }
- }
- template<typename T = int>
- void printInteger(T val) {
- constexpr int sizeOf = sizeof(T);
- char buff[sizeOf];
- if (type == UNDEF) {
- type = BIG_ENDIAN;
- }
- for (int i = 0; i < sizeOf; i++) {
- switch (type) {
- case BIG_ENDIAN: {
- buff[sizeOf - 1 - i] = char(val & 255);
- break;
- }
- case LITTLE_ENDIAN: {
- buff[i] = char(val & 255);
- break;
- }
- default: {
- throw exception();
- }
- }
- val >>= 8;
- }
- out.write(buff, sizeOf);
- }
- enum {
- BIG_ENDIAN,
- LITTLE_ENDIAN,
- UNDEF
- };
- ofstream &out;
- ifstream ∈
- int type = UNDEF;
- public:
- BinaryIO(ifstream &in, ofstream &out)
- : in(in), out(out) {}
- ~BinaryIO() {
- in.close();
- out.close();
- }
- template<typename T>
- BinaryIO &operator<<(vector<T> &values) {
- for (int i = 0; i < values.size(); i++) {
- printInteger<T>(values[i]);
- if (i + 1 < values.size()) {
- printInteger<char>(' ');
- }
- }
- return *this;
- }
- template<typename T>
- BinaryIO &operator<<(T&(*x)()) {
- return *this;
- }
- template<typename T>
- BinaryIO &operator<<(T x) {
- printInteger<T>(x);
- return *this;
- }
- template<typename T>
- BinaryIO &operator>>(T &x) {
- x = readInteger<T>();
- return *this;
- }
- BinaryIO &endl() {
- *this << '\n';
- out.flush();
- return *this;
- }
- };
- BinaryIO &operator<<(BinaryIO &binaryIO, [[maybe_unused]] std::ostream &(*unusedFunction)(std::ostream &)) {
- return binaryIO.endl();
- }
- #endif
- template<typename TStreamType, typename TValueType>
- TStreamType &operator<<(TStreamType &stream, std::vector<TValueType> &values) {
- for (int i = 0; i < values.size(); i++) {
- stream << values[i];
- if (i + 1 < values.size()) {
- stream << ' ';
- }
- }
- return stream;
- }
- template<typename InStream, typename OutStream>
- class Solver {
- private:
- InStream ∈
- OutStream &out;
- public:
- Solver(InStream &inStream, OutStream &outStream)
- : in(inStream), out(outStream) {
- solve();
- }
- void solve();
- };
- int main() {
- ios_base::sync_with_stdio(false);
- switch (PROBLEM_TYPE) {
- case INTERACTIVE: {
- cin.tie(nullptr);
- cout.tie(nullptr);
- Solver<basic_istream<char>, basic_ostream<char>>(cin, cout);
- break;
- }
- #if PROBLEM_TYPE == 1
- case BINARY_FORMAT: {
- ifstream in("input.bin", ios::binary);
- ofstream out("output.bin", ios::binary);
- in.tie(nullptr);
- out.tie(nullptr);
- BinaryIO binaryIO(in, out);
- Solver<BinaryIO, BinaryIO>(binaryIO, binaryIO);
- in.close();
- out.close();
- break;
- }
- #endif
- case TEXT_FORMAT: {
- ifstream in("input.txt");
- ofstream out("output.txt");
- in.tie(nullptr);
- out.tie(nullptr);
- Solver<ifstream, ofstream>(in, out);
- in.close();
- out.close();
- break;
- }
- default: {
- throw bad_exception();
- }
- }
- }
- template<typename InStream, typename OutStream>
- void Solver<InStream, OutStream>::solve() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement