MtSaka's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub MtSaka/library

:heavy_check_mark: template/out.hpp

Depends on

Required by

Verified with

Code

#pragma once
#include <unistd.h>
#include "macro.hpp"
#include "alias.hpp"
#include "type-traits.hpp"

namespace fastio {
struct Pre {
    char buffer[10000][4];
    constexpr Pre() : buffer() {
        for (int i = 0; i < 10000; ++i) {
            int n = i;
            for (int j = 3; j >= 0; --j) {
                buffer[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;
template <std::size_t BUFF_SIZE = 1 << 17, bool debug = false>
struct Printer {
   private:
    template <typename, bool = debug, class = void>
    struct has_print : std::false_type {};
    template <typename T>
    struct has_print<T, false, decltype(std::declval<T>().print(std::declval<Printer&>()), (void)0)> : std::true_type {};
    template <typename T>
    struct has_print<T, true, decltype(std::declval<T>().debug(std::declval<Printer&>()), (void)0)> : std::true_type {};
    int fd;
    char buffer[BUFF_SIZE];
    int idx;
    std::size_t decimal_precision;

   public:
    Printer() : Printer((debug ? 2 : 1)) {}
    explicit Printer(int fd) : fd(fd), idx(0), decimal_precision(16) {}
    explicit Printer(FILE* file) : fd(fileno(file)), idx(0), decimal_precision(16) {}
    ~Printer() {
        flush();
    }
    void set_decimal_precision(std::size_t n) { decimal_precision = n; }
    inline void print_char(char c) {
        buffer[idx++] = c;
        if (idx == BUFF_SIZE) flush();
    }
    inline void flush() {
        idx = write(fd, buffer, idx);
        idx = 0;
    }
    void print(char a) {
        if constexpr (debug) print_char('\'');
        print_char(a);
        if constexpr (debug) print_char('\'');
    }
    void print(bool a) {
        if constexpr (debug) print_char('\'');
        print_char('0' + a);
        if constexpr (debug) print_char('\'');
    }
    void print(const char* a) {
        if constexpr (debug) print_char('\"');
        for (; *a != '\0'; ++a) print_char(*a);
        if constexpr (debug) print_char('\"');
    }
    template <std::size_t N>
    void print(const char (&a)[N]) {
        if constexpr (debug) print_char('\"');
        for (auto i : a) print_char(i);
        if constexpr (debug) print_char('\"');
    }
    void print(const std::string& a) {
        if constexpr (debug) print_char('\"');
        for (auto i : a) print_char(i);
        if constexpr (debug) print_char('\"');
    }
    template <std::size_t len>
    void print(const std::bitset<len>& a) {
        for (int i = len - 1; i >= 0; --i) print_char('0' + a[i]);
    }
    template <typename T, typename std::enable_if<is_int<T>::value && !has_print<T>::value>::type* = nullptr>
    void print(T a) {
        if (!a) {
            print_char('0');
            return;
        }
        if constexpr (is_signed_int<T>::value) {
            if (a < 0) {
                print_char('-');
                a = -a;
            }
        }
        if (static_cast<size_t>(idx + 40) >= BUFF_SIZE) flush();
        static char stk[40];
        int top = 40;
        while (a >= 10000) {
            int i = a % 10000;
            a /= 10000;
            top -= 4;
            std::memcpy(stk + top, pre.buffer[i], 4);
        }
        if (a >= 1000) {
            std::memcpy(buffer + idx, pre.buffer[a], 4);
            idx += 4;
        } else if (a >= 100) {
            std::memcpy(buffer + idx, pre.buffer[a] + 1, 3);
            idx += 3;
        } else if (a >= 10) {
            std::memcpy(buffer + idx, pre.buffer[a] + 2, 2);
            idx += 2;
        } else {
            buffer[idx++] = '0' | a;
        }
        std::memcpy(buffer + idx, stk + top, 40 - top);
        idx += 40 - top;
    }
    template <typename T, typename std::enable_if<std::is_floating_point<T>::value && !has_print<T>::value>::type* = nullptr>
    void print(T a) {
        if (a == infinity<T>::max || a == infinity<T>::value) {
            print("inf");
            return;
        }
        if (a == infinity<T>::min || a == infinity<T>::mvalue) {
            print("-inf");
            return;
        }
        if (std::isnan(a)) {
            print("nan");
            return;
        }
        if (a < 0) {
            print_char('-');
            a = -a;
        }
        T b = a;
        if (b < 1) {
            print_char('0');
        } else {
            std::string s;
            while (b >= 1) {
                s += (char)('0' | (int)std::fmod(b, 10.0));
                b /= 10;
            }
            for (auto i = s.rbegin(); i != s.rend(); ++i) {
                print_char(*i);
            }
        }
        print_char('.');
        for (std::size_t _ = 0; _ < decimal_precision; ++_) {
            a *= 10;
            print_char('0' | (int)std::fmod(a, 10.0));
        }
    }

   private:
    template <std::size_t i, typename... Args>
    void print(const std::tuple<Args...>& a) {
        if constexpr (i < sizeof...(Args)) {
            if constexpr (debug) print_char(',');
            print_char(' ');
            print(std::get<i>(a));
            print<i + 1>(a);
        }
    }

   public:
    template <typename... Args>
    void print(const std::tuple<Args...>& a) {
        if constexpr (debug) print_char('(');
        if constexpr (sizeof...(Args) != 0) {
            print(std::get<0>(a));
        }
        print<1, Args...>(a);
        if constexpr (debug) print_char(')');
    }
    template <typename T, typename U>
    void print(const std::pair<T, U>& a) {
        if constexpr (debug) print_char('(');
        print(a.first);
        if constexpr (debug) print_char(',');
        print_char(' ');
        print(a.second);
        if constexpr (debug) print_char(')');
    }
    template <typename T, typename std::enable_if<is_range<T>::value>::type* = nullptr>
    void print(const T& a) {
        if constexpr (debug) print_char('{');
        auto it = std::begin(a);
        if (it != std::end(a)) {
            print(*it);
            while (++it != std::end(a)) {
                if constexpr (debug) print_char(',');
                print_char(' ');
                print(*it);
            }
        }
        if constexpr (debug) print_char('}');
    }
    template <typename T, typename std::enable_if<has_print<T>::value && !debug>::type* = nullptr>
    void print(const T& a) {
        a.print(*this);
    }
    template <typename T, typename std::enable_if<has_print<T>::value && debug>::type* = nullptr>
    void print(const T& a) {
        a.debug(*this);
    }
    void operator()() {}
    template <typename Head, typename... Tail>
    void operator()(const Head& head, const Tail&... tail) {
        print(head);
        operator()(std::forward<const Tail&>(tail)...);
    }
    template <typename T>
    Printer& operator<<(const T& a) {
        print(a);
        return *this;
    }
    Printer& operator<<(Printer& (*f)(Printer&)) {
        return f(*this);
    }
};

template <std::size_t BUFF_SIZE, bool debug>
Printer<BUFF_SIZE, debug>& endl(Printer<BUFF_SIZE, debug>& out) {
    out.print_char('\n');
    out.flush();
    return out;
}
template <std::size_t BUFF_SIZE, bool debug>
Printer<BUFF_SIZE, debug>& flush(Printer<BUFF_SIZE, debug>& out) {
    out.flush();
    return out;
}
Printer<> pr;
Printer<1 << 17, true> prd;
}  // namespace fastio
using fastio::endl;
using fastio::flush;
using fastio::pr;
using fastio::prd;
#line 2 "template/out.hpp"
#include <unistd.h>
#line 2 "template/macro.hpp"
#include <bits/stdc++.h>

#ifndef __COUNTER__
#define __COUNTER__ __LINE__
#endif

#define SELECT4(a, b, c, d, e, ...) e
#define SELECT3(a, b, c, d, ...) d
#define REP_1(a, c) for (ll REP_##c = 0; REP_##c < (ll)(a); ++REP_##c)
#define REP1(a) REP_1(a, __COUNTER__)
#define REP2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define REP3(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define REP4(i, a, b, c) for (ll i = (ll)(a); i < (ll)(b); i += (ll)(c))
#define rep(...) SELECT4(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define RREP_1(a, c) for (ll RREP_##c = (ll)(a) - 1; RREP_##c >= 0; --RREP_##c)
#define RREP1(a) RREP_1(a, __COUNTER__)
#define RREP2(i, a) for (ll i = (ll)(a) - 1; i >= 0; --i)
#define RREP3(i, a, b) for (ll i = (ll)(b) - 1; i >= (ll)(a); --i)
#define rrep(...) SELECT3(__VA_ARGS__, RREP3, RREP2, RREP1)(__VA_ARGS__)
#define all(v) std::begin(v), std::end(v)
#define rall(v) std::rbegin(v), std::rend(v)
#define INT(...)     \
    int __VA_ARGS__; \
    scan(__VA_ARGS__)
#define LL(...)     \
    ll __VA_ARGS__; \
    scan(__VA_ARGS__)
#define STR(...)        \
    string __VA_ARGS__; \
    scan(__VA_ARGS__)
#define CHR(...)      \
    char __VA_ARGS__; \
    scan(__VA_ARGS__)
#define DBL(...)        \
    double __VA_ARGS__; \
    scan(__VA_ARGS__)
#define LD(...)     \
    ld __VA_ARGS__; \
    scan(__VA_ARGS__)
#define pb push_back
#define eb emplace_back
#line 3 "template/alias.hpp"

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128_t;
using u128 = __uint128_t;
using pi = std::pair<int, int>;
using pl = std::pair<ll, ll>;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vs = std::vector<std::string>;
using vc = std::vector<char>;
using vvl = std::vector<vl>;
using vd = std::vector<double>;
using vp = std::vector<pl>;
using vb = std::vector<bool>;
template <typename T>
struct infinity {
    static constexpr T max = std::numeric_limits<T>::max();
    static constexpr T min = std::numeric_limits<T>::min();
    static constexpr T value = std::numeric_limits<T>::max() / 2;
    static constexpr T mvalue = std::numeric_limits<T>::min() / 2;
};
template <typename T>
constexpr T INF = infinity<T>::value;
constexpr ll inf = INF<ll>;
constexpr ld EPS = 1e-8;
constexpr ld PI = 3.1415926535897932384626;
constexpr int dx[8] = {-1, 0, 1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
#line 3 "template/type-traits.hpp"

#line 5 "template/type-traits.hpp"

template <typename T, typename... Args>
struct function_traits_impl {
    using return_type = T;
    static constexpr std::size_t arg_size = sizeof...(Args);
    template <std::size_t idx>
    using argument_type = typename std::tuple_element<idx, std::tuple<Args...>>::type;
    using argument_types = std::tuple<Args...>;
};

template <typename>
struct function_traits_helper;
template <typename T, typename Tp, typename... Args>
struct function_traits_helper<T (Tp::*)(Args...)> : function_traits_impl<T, Args...> {};
template <typename T, typename Tp, typename... Args>
struct function_traits_helper<T (Tp::*)(Args...) const> : function_traits_impl<T, Args...> {};
template <typename T, typename Tp, typename... Args>
struct function_traits_helper<T (Tp::*)(Args...)&> : function_traits_impl<T, Args...> {};
template <typename T, typename Tp, typename... Args>
struct function_traits_helper<T (Tp::*)(Args...) const&> : function_traits_impl<T, Args...> {};

template <typename F>
using function_traits = function_traits_helper<decltype(&std::remove_reference<F>::type::operator())>;
template <typename F>
using function_return_type = typename function_traits<F>::return_type;
template <typename F, std::size_t idx>
using function_argument_type = typename function_traits<F>::template argument_type<idx>;
template <typename F>
using function_argument_types = typename function_traits<F>::argument_types;
template <class T>
using is_signed_int = std::integral_constant<bool, (std::is_integral<T>::value && std::is_signed<T>::value) || std::is_same<T, __int128_t>::value>;
template <class T>
using is_unsigned_int = std::integral_constant<bool, (std::is_integral<T>::value && std::is_unsigned<T>::value) || std::is_same<T, __uint128_t>::value>;
template <class T>
using is_int = std::integral_constant<bool, is_signed_int<T>::value || is_unsigned_int<T>::value>;
template <typename T, typename = void>
struct is_range : std::false_type {};
template <typename T>
struct is_range<
    T,
    decltype(all(std::declval<typename std::add_lvalue_reference<T>::type>()), (void)0)> : std::true_type {};
template <std::size_t size>
struct int_least {
    static_assert(size <= 128, "size must be less than or equal to 128");
    using type = typename std::conditional<
        size <= 8, std::int_least8_t,
        typename std::conditional<
            size <= 16, std::int_least16_t,
            typename std::conditional<
                size <= 32, std::int_least32_t,
                typename std::conditional<size <= 64, std::int_least64_t, __int128_t>::type>::type>::type>::type;
};
template <std::size_t size>
using int_least_t = typename int_least<size>::type;
template <std::size_t size>
struct uint_least {
    static_assert(size <= 128, "size must be less than or equal to 128");
    using type = typename std::conditional<
        size <= 8, std::uint_least8_t,
        typename std::conditional<
            size <= 16, std::uint_least16_t,
            typename std::conditional<
                size <= 32, std::uint_least32_t,
                typename std::conditional<size <= 64, std::uint_least64_t, __uint128_t>::type>::type>::type>::type;
};
template <std::size_t size>
using uint_least_t = typename uint_least<size>::type;
template <typename T>
using double_size_int = int_least<std::numeric_limits<T>::digits * 2 + 1>;
template <typename T>
using double_size_int_t = typename double_size_int<T>::type;
template <typename T>
using double_size_uint = uint_least<std::numeric_limits<T>::digits * 2>;
template <typename T>
using double_size_uint_t = typename double_size_uint<T>::type;
template <typename T>
using double_size = typename std::conditional<std::is_signed<T>::value, double_size_int<T>, double_size_uint<T>>::type;
template <typename T>
using double_size_t = typename double_size<T>::type;
#line 6 "template/out.hpp"

namespace fastio {
struct Pre {
    char buffer[10000][4];
    constexpr Pre() : buffer() {
        for (int i = 0; i < 10000; ++i) {
            int n = i;
            for (int j = 3; j >= 0; --j) {
                buffer[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;
template <std::size_t BUFF_SIZE = 1 << 17, bool debug = false>
struct Printer {
   private:
    template <typename, bool = debug, class = void>
    struct has_print : std::false_type {};
    template <typename T>
    struct has_print<T, false, decltype(std::declval<T>().print(std::declval<Printer&>()), (void)0)> : std::true_type {};
    template <typename T>
    struct has_print<T, true, decltype(std::declval<T>().debug(std::declval<Printer&>()), (void)0)> : std::true_type {};
    int fd;
    char buffer[BUFF_SIZE];
    int idx;
    std::size_t decimal_precision;

   public:
    Printer() : Printer((debug ? 2 : 1)) {}
    explicit Printer(int fd) : fd(fd), idx(0), decimal_precision(16) {}
    explicit Printer(FILE* file) : fd(fileno(file)), idx(0), decimal_precision(16) {}
    ~Printer() {
        flush();
    }
    void set_decimal_precision(std::size_t n) { decimal_precision = n; }
    inline void print_char(char c) {
        buffer[idx++] = c;
        if (idx == BUFF_SIZE) flush();
    }
    inline void flush() {
        idx = write(fd, buffer, idx);
        idx = 0;
    }
    void print(char a) {
        if constexpr (debug) print_char('\'');
        print_char(a);
        if constexpr (debug) print_char('\'');
    }
    void print(bool a) {
        if constexpr (debug) print_char('\'');
        print_char('0' + a);
        if constexpr (debug) print_char('\'');
    }
    void print(const char* a) {
        if constexpr (debug) print_char('\"');
        for (; *a != '\0'; ++a) print_char(*a);
        if constexpr (debug) print_char('\"');
    }
    template <std::size_t N>
    void print(const char (&a)[N]) {
        if constexpr (debug) print_char('\"');
        for (auto i : a) print_char(i);
        if constexpr (debug) print_char('\"');
    }
    void print(const std::string& a) {
        if constexpr (debug) print_char('\"');
        for (auto i : a) print_char(i);
        if constexpr (debug) print_char('\"');
    }
    template <std::size_t len>
    void print(const std::bitset<len>& a) {
        for (int i = len - 1; i >= 0; --i) print_char('0' + a[i]);
    }
    template <typename T, typename std::enable_if<is_int<T>::value && !has_print<T>::value>::type* = nullptr>
    void print(T a) {
        if (!a) {
            print_char('0');
            return;
        }
        if constexpr (is_signed_int<T>::value) {
            if (a < 0) {
                print_char('-');
                a = -a;
            }
        }
        if (static_cast<size_t>(idx + 40) >= BUFF_SIZE) flush();
        static char stk[40];
        int top = 40;
        while (a >= 10000) {
            int i = a % 10000;
            a /= 10000;
            top -= 4;
            std::memcpy(stk + top, pre.buffer[i], 4);
        }
        if (a >= 1000) {
            std::memcpy(buffer + idx, pre.buffer[a], 4);
            idx += 4;
        } else if (a >= 100) {
            std::memcpy(buffer + idx, pre.buffer[a] + 1, 3);
            idx += 3;
        } else if (a >= 10) {
            std::memcpy(buffer + idx, pre.buffer[a] + 2, 2);
            idx += 2;
        } else {
            buffer[idx++] = '0' | a;
        }
        std::memcpy(buffer + idx, stk + top, 40 - top);
        idx += 40 - top;
    }
    template <typename T, typename std::enable_if<std::is_floating_point<T>::value && !has_print<T>::value>::type* = nullptr>
    void print(T a) {
        if (a == infinity<T>::max || a == infinity<T>::value) {
            print("inf");
            return;
        }
        if (a == infinity<T>::min || a == infinity<T>::mvalue) {
            print("-inf");
            return;
        }
        if (std::isnan(a)) {
            print("nan");
            return;
        }
        if (a < 0) {
            print_char('-');
            a = -a;
        }
        T b = a;
        if (b < 1) {
            print_char('0');
        } else {
            std::string s;
            while (b >= 1) {
                s += (char)('0' | (int)std::fmod(b, 10.0));
                b /= 10;
            }
            for (auto i = s.rbegin(); i != s.rend(); ++i) {
                print_char(*i);
            }
        }
        print_char('.');
        for (std::size_t _ = 0; _ < decimal_precision; ++_) {
            a *= 10;
            print_char('0' | (int)std::fmod(a, 10.0));
        }
    }

   private:
    template <std::size_t i, typename... Args>
    void print(const std::tuple<Args...>& a) {
        if constexpr (i < sizeof...(Args)) {
            if constexpr (debug) print_char(',');
            print_char(' ');
            print(std::get<i>(a));
            print<i + 1>(a);
        }
    }

   public:
    template <typename... Args>
    void print(const std::tuple<Args...>& a) {
        if constexpr (debug) print_char('(');
        if constexpr (sizeof...(Args) != 0) {
            print(std::get<0>(a));
        }
        print<1, Args...>(a);
        if constexpr (debug) print_char(')');
    }
    template <typename T, typename U>
    void print(const std::pair<T, U>& a) {
        if constexpr (debug) print_char('(');
        print(a.first);
        if constexpr (debug) print_char(',');
        print_char(' ');
        print(a.second);
        if constexpr (debug) print_char(')');
    }
    template <typename T, typename std::enable_if<is_range<T>::value>::type* = nullptr>
    void print(const T& a) {
        if constexpr (debug) print_char('{');
        auto it = std::begin(a);
        if (it != std::end(a)) {
            print(*it);
            while (++it != std::end(a)) {
                if constexpr (debug) print_char(',');
                print_char(' ');
                print(*it);
            }
        }
        if constexpr (debug) print_char('}');
    }
    template <typename T, typename std::enable_if<has_print<T>::value && !debug>::type* = nullptr>
    void print(const T& a) {
        a.print(*this);
    }
    template <typename T, typename std::enable_if<has_print<T>::value && debug>::type* = nullptr>
    void print(const T& a) {
        a.debug(*this);
    }
    void operator()() {}
    template <typename Head, typename... Tail>
    void operator()(const Head& head, const Tail&... tail) {
        print(head);
        operator()(std::forward<const Tail&>(tail)...);
    }
    template <typename T>
    Printer& operator<<(const T& a) {
        print(a);
        return *this;
    }
    Printer& operator<<(Printer& (*f)(Printer&)) {
        return f(*this);
    }
};

template <std::size_t BUFF_SIZE, bool debug>
Printer<BUFF_SIZE, debug>& endl(Printer<BUFF_SIZE, debug>& out) {
    out.print_char('\n');
    out.flush();
    return out;
}
template <std::size_t BUFF_SIZE, bool debug>
Printer<BUFF_SIZE, debug>& flush(Printer<BUFF_SIZE, debug>& out) {
    out.flush();
    return out;
}
Printer<> pr;
Printer<1 << 17, true> prd;
}  // namespace fastio
using fastio::endl;
using fastio::flush;
using fastio::pr;
using fastio::prd;
Back to top page