Advertisement
999ms

Untitled

Oct 9th, 2021
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.49 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:16777216")
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. #define all(x) begin(x), end(x)
  6. #define sum(x) accumulate(all(x), 0ll)
  7.  
  8. // g++ -Wl,--stack,1000000000 -std=c++17 main.cpp -o main && main
  9.  
  10. #define PROBLEM_TYPE 2
  11. using namespace std;
  12. using ll = long long;
  13.  
  14. enum {
  15.     INTERACTIVE = 0,
  16.     BINARY_FORMAT = 1,
  17.     TEXT_FORMAT = 2
  18. };
  19.  
  20. #if PROBLEM_TYPE == 1
  21. class BinaryIO {
  22. private:
  23.     template<typename T = int>
  24.     int readIntInBigEndian(const char *buff, int sizeOf = 4) {
  25.         unsigned long long ans = 0;
  26.         for (int i = 0; i < sizeOf; i++) {
  27.             ans = (ans << 8) ^ uint8_t(buff[i]);
  28.         }
  29.         return static_cast<T>(ans);
  30.     }
  31.  
  32.     template<typename T = int>
  33.     int readIntInLittleEndian(const char *buff, int sizeOf = 4) {
  34.         unsigned long long ans = 0;
  35.         for (int i = sizeOf - 1; i >= 0; i--) {
  36.             ans = (ans << 8) ^ uint8_t(buff[i]);
  37.         }
  38.         return static_cast<T>(ans);
  39.     }
  40.  
  41.     template<typename T = int>
  42.     T readInteger() {
  43.         constexpr int sizeOf = sizeof(T);
  44.         char buff[sizeOf];
  45.         in.read(buff, sizeOf);
  46.  
  47.         if (type == UNDEF) {
  48.             if (abs(readIntInBigEndian<T>(buff, sizeOf)) < abs(readIntInLittleEndian<T>(buff, sizeOf))) {
  49.                 type = BIG_ENDIAN;
  50.             } else {
  51.                 type = LITTLE_ENDIAN;
  52.             }
  53.         }
  54.         if (type == BIG_ENDIAN) {
  55.             return readIntInBigEndian<T>(buff, sizeOf);
  56.         } else {
  57.             return readIntInLittleEndian<T>(buff, sizeOf);
  58.         }
  59.  
  60.     }
  61.  
  62.     template<typename T = int>
  63.     void printInteger(T val) {
  64.         constexpr int sizeOf = sizeof(T);
  65.         char buff[sizeOf];
  66.         if (type == UNDEF) {
  67.             type = BIG_ENDIAN;
  68.         }
  69.         for (int i = 0; i < sizeOf; i++) {
  70.             switch (type) {
  71.                 case BIG_ENDIAN: {
  72.                     buff[sizeOf - 1 - i] = char(val & 255);
  73.                     break;
  74.                 }
  75.                 case LITTLE_ENDIAN: {
  76.                     buff[i] = char(val & 255);
  77.                     break;
  78.                 }
  79.                 default: {
  80.                     throw exception();
  81.                 }
  82.             }
  83.  
  84.             val >>= 8;
  85.         }
  86.         out.write(buff, sizeOf);
  87.     }
  88.  
  89.     enum {
  90.         BIG_ENDIAN,
  91.         LITTLE_ENDIAN,
  92.         UNDEF
  93.     };
  94.  
  95.  
  96.     ofstream &out;
  97.     ifstream &in;
  98.     int type = UNDEF;
  99. public:
  100.  
  101.     BinaryIO(ifstream &in, ofstream &out)
  102.             : in(in), out(out) {}
  103.  
  104.     ~BinaryIO() {
  105.         in.close();
  106.         out.close();
  107.     }
  108.  
  109.     template<typename T>
  110.     BinaryIO &operator<<(vector<T> &values) {
  111.         for (int i = 0; i < values.size(); i++) {
  112.             printInteger<T>(values[i]);
  113.             if (i + 1 < values.size()) {
  114.                 printInteger<char>(' ');
  115.             }
  116.         }
  117.         return *this;
  118.     }
  119.  
  120.     template<typename T>
  121.     BinaryIO &operator<<(T&(*x)()) {
  122.         return *this;
  123.     }
  124.  
  125.     template<typename T>
  126.     BinaryIO &operator<<(T x) {
  127.         printInteger<T>(x);
  128.         return *this;
  129.     }
  130.  
  131.     template<typename T>
  132.     BinaryIO &operator>>(T &x) {
  133.         x = readInteger<T>();
  134.         return *this;
  135.     }
  136.  
  137.     BinaryIO &endl() {
  138.         *this << '\n';
  139.         out.flush();
  140.         return *this;
  141.     }
  142.  
  143.  
  144. };
  145.  
  146. BinaryIO &operator<<(BinaryIO &binaryIO, [[maybe_unused]] std::ostream &(*unusedFunction)(std::ostream &)) {
  147.     return binaryIO.endl();
  148. }
  149. #endif
  150.  
  151. template<typename TStreamType, typename TValueType>
  152. TStreamType &operator<<(TStreamType &stream, std::vector<TValueType> &values) {
  153.     for (int i = 0; i < values.size(); i++) {
  154.         stream << values[i];
  155.         if (i + 1 < values.size()) {
  156.             stream << ' ';
  157.         }
  158.     }
  159.     return stream;
  160. }
  161.  
  162. template<typename TStreamType, typename TValueType>
  163. TStreamType &operator>>(TStreamType &stream, std::vector<TValueType> &values) {
  164.     for (int i = 0; i < values.size(); i++) {
  165.         stream >> values[i];
  166.     }
  167.     return stream;
  168. }
  169.  
  170. template<typename InStream, typename OutStream>
  171. class Solver {
  172. private:
  173.     InStream &in;
  174.     OutStream &out;
  175. public:
  176.     Solver(InStream &inStream, OutStream &outStream)
  177.             : in(inStream), out(outStream) {
  178.         solve();
  179.     }
  180.     void solve();
  181. };
  182.  
  183. int main() {
  184.     ios_base::sync_with_stdio(false);
  185.     switch (PROBLEM_TYPE) {
  186.         case INTERACTIVE: {
  187.             cin.tie(nullptr);
  188.             cout.tie(nullptr);
  189.             Solver<basic_istream<char>, basic_ostream<char >>(cin, cout);
  190.             break;
  191.         }
  192. #if PROBLEM_TYPE == 1
  193.             case BINARY_FORMAT: {
  194.                 ifstream in("input.bin", ios::binary);
  195.                 ofstream out("output.bin", ios::binary);
  196.                 in.tie(nullptr);
  197.                 out.tie(nullptr);
  198.                 BinaryIO binaryIO(in, out);
  199.                 Solver<BinaryIO, BinaryIO>(binaryIO, binaryIO);
  200.                 in.close();
  201.                 out.close();
  202.                 break;
  203.             }
  204. #endif
  205.         case TEXT_FORMAT: {
  206.             ifstream in("input.txt");
  207.             ofstream out("output.txt");
  208.             in.tie(nullptr);
  209.             out.tie(nullptr);
  210.             Solver<ifstream, ofstream>(in, out);
  211.             in.close();
  212.             out.close();
  213.             break;
  214.         }
  215.         default: {
  216.             throw bad_exception();
  217.         }
  218.     }
  219. }
  220.  
  221. template<typename InStream, typename OutStream>
  222. void Solver<InStream, OutStream>::solve() {
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement