Advertisement
999ms

Untitled

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