FastIO

· · 算法·理论

火车头(万能头+快读+一些功能)

#define ffopen(s) \
std::cin.tie(0)->sync_with_stdio(0); \
if (*#s) freopen(#s ".in", "r", stdin); \
if (*#s) freopen(#s ".out", "w", stdout); \
/**/
#include <bits/stdc++.h>
#define chkmax(x, y) ((x) = std::max((x), (y)))
#define chkmin(x, y) ((x) = std::min((x), (y)))
#define gc() (eof||(p==SIZE&&(buf[fread(buf,1,SIZE,stdin)]=EOF,(p=0)))||~buf[p]?buf[p++]:(eof=true,EOF))
#define ug(x) (eof=false,buf[--p]=(x))
#define pc(x) (buf[p++]=(x),p==SIZE?fl():p)
#define fl() (fwrite(buf,1,p,stdout),p=0)
namespace FastIn { constexpr size_t SIZE = 1 << 16; size_t p = SIZE; char buf[SIZE | 1], c; bool f, eof; inline void rd(char& x) { while(isspace(x=gc())); } inline void rd(char* s) { while(isspace(*s=gc()));if(eof){return;}for(;!isspace(*s)&&!eof;*++s=gc());ug(*s),*s='\0'; } template<typename T> inline std::enable_if_t<std::is_integral<T>::value || std::is_same_v<T, __int128> || std::is_same_v<T, unsigned __int128> > rd(T& x) { x=f=0;while(isspace(c=gc()));if(eof){return;}if(c=='-'){f=true,c=gc();}for(;isdigit(c);c=gc()){x=(x<<1)+(x<<3)+(c^'0');}ug(c);if(f){x=-x;} } }
namespace FastOut { constexpr size_t SIZE = 1 << 18; char buf[SIZE], c[40]; size_t f, p; struct __{~__(){fl();}}__; inline void wt(char x) { pc(x); } inline void wt(const char* s) { while(*s)pc(*s++); } template<typename T> inline std::enable_if_t<std::is_integral<T>::value || std::is_same_v<T, __int128> || std::is_same_v<T, unsigned __int128> > wt(T x) { if(!x){pc('0');return;}f=0;if(x<0)x=-x,pc('-');while(x){c[f++]=x%10^'0',x/=10;}while(f--){pc(c[f]);} } }
template<typename... Args> inline void rd(Args&&... args) { using namespace FastIn; (rd(args), ...); }
template<typename... Args> inline void wtsp(Args&&... args) { using namespace FastOut; ((wt(args), wt(' ')), ...); if constexpr (!sizeof...(args)) wt(' '); }
template<typename... Args> inline void wtln(Args&&... args) { using namespace FastOut; ((wt(args), wt('\n')), ...); if constexpr (!sizeof...(args)) wt('\n'); }
template<typename Iter> std::enable_if_t<std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<Iter>::iterator_category>::value> rd(Iter begin, Iter end) { while (begin != end) rd(*begin++); }
template<typename Iter> std::enable_if_t<std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<Iter>::iterator_category>::value> wtsp(Iter begin, Iter end) { while (begin != end) wtsp(*begin++); }
template<typename Iter> std::enable_if_t<std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<Iter>::iterator_category>::value> wtln(Iter begin, Iter end) { while (begin != end) wtln(*begin++); }
template<typename T_, size_t N_, T_ DEFAULT_ = T_()> class Array { T_ val[N_]; size_t ver[N_], clk; public: Array() { clk=1; } T_& operator[] (size_t i) { if(ver[i]!=clk){ver[i]=clk,val[i]=DEFAULT_;} return val[i]; } void clear() { ++clk; } };
template<typename T_, size_t N_> class Graph { inline static size_t psz; inline static std::pair<T_, size_t> pool[N_]; struct iterator { size_t now; T_& operator* () const { return pool[now].first; } bool operator== (iterator it) const { return now == it.now; } iterator& operator++ () { now=pool[now].second; return *this; } }; size_t head; public: Graph() { head = 0; } iterator begin() const { return {head}; } iterator end() const { return {0}; } void push_back(const T_& val) { ++psz,this->pool[psz]={val, head},head=psz; } void clear() { psz=head=0; } };
using std::min, std::max, std::cerr;
using intl = long long;

FastIO(可能不是最新)

该快读模板会有约 0.32\text{MiB} 的内存占用。

#define gc() (eof||(p==SIZE&&(buf[fread(buf,1,SIZE,stdin)]=EOF,(p=0)))||~buf[p]?buf[p++]:(eof=true,EOF))
#define ug(x) (eof=false,buf[--p]=(x))
#define pc(x) (buf[p++]=(x),p==SIZE?fl():p)
#define fl() (fwrite(buf,1,p,stdout),p=0)
namespace FastIn { constexpr size_t SIZE = 1 << 16; size_t p = SIZE; char buf[SIZE | 1], c; bool f, eof; inline void rd(char& x) { while(isspace(x=gc())); } inline void rd(char* s) { while(isspace(*s=gc()));if(eof){return;}for(;!isspace(*s)&&!eof;*++s=gc());ug(*s),*s='\0'; } template<typename T> inline std::enable_if_t<std::is_integral<T>::value || std::is_same_v<T, __int128> || std::is_same_v<T, unsigned __int128> > rd(T& x) { x=f=0;while(isspace(c=gc()));if(eof){return;}if(c=='-'){f=true,c=gc();}for(;isdigit(c);c=gc()){x=(x<<1)+(x<<3)+(c^'0');}ug(c);if(f){x=-x;} } }
namespace FastOut { constexpr size_t SIZE = 1 << 18; char buf[SIZE], c[40]; size_t f, p; struct __{~__(){fl();}}__; inline void wt(char x) { pc(x); } inline void wt(const char* s) { while(*s)pc(*s++); } template<typename T> inline std::enable_if_t<std::is_integral<T>::value || std::is_same_v<T, __int128> || std::is_same_v<T, unsigned __int128> > wt(T x) { if(!x){pc('0');return;}f=0;if(x<0)x=-x,pc('-');while(x){c[f++]=x%10^'0',x/=10;}while(f--){pc(c[f]);} } }
template<typename... Args> inline void rd(Args&... args) { using namespace FastIn; (rd(args), ...); }
template<typename... Args> inline void wtsp(const Args&... args) { using namespace FastOut; ((wt(args), wt(' ')), ...); if constexpr (!sizeof...(args)) wt(' '); }
template<typename... Args> inline void wtln(const Args&... args) { using namespace FastOut; ((wt(args), wt('\n')), ...); if constexpr (!sizeof...(args)) wt('\n'); }
template<typename Iter> std::enable_if_t<std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<Iter>::iterator_category>::value> rd(Iter begin, Iter end) { while (begin != end) rd(*begin++); }
template<typename Iter> std::enable_if_t<std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<Iter>::iterator_category>::value> wtsp(Iter begin, Iter end) { while (begin != end) wtsp(*begin++); }
template<typename Iter> std::enable_if_t<std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<Iter>::iterator_category>::value> wtln(Iter begin, Iter end) { while (begin != end) wtln(*begin++); }

更快的 FastIO(有点问题)

#include <unistd.h>
#include <fcntl.h>
#include <cstdio>
#include <cctype>
#include <cstddef>
#include <cstdlib>
#undef stdin
#undef stdout
#define stdin FastIO::in_fd
#define stdout FastIO::out_fd
#define IN_BUFFER_SIZE (1 << 16)
#define OUT_BUFFER_SIZE (1 << 18)
namespace FastIO {
    int in_fd = STDIN_FILENO, out_fd = STDOUT_FILENO;
    void freopen(const char* filename, const char* mode, int& file) {
        if (mode[0] == 'r') file = open(filename, O_RDONLY);
        if (mode[0] == 'w') file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
        if (file == -1) perror("Failed to open file"), exit(1);
    }
    size_t in_idx, in_end, out_idx;
    char in_buffer[IN_BUFFER_SIZE], out_buffer[OUT_BUFFER_SIZE];
    inline char getchar() {
        if (in_idx >= in_end) {
            in_idx = 0, in_end = read(in_fd, in_buffer, IN_BUFFER_SIZE);
            if (in_end <= 0) return EOF;
        } return in_buffer[in_idx++];
    }
    inline void putchar(const char &c) {
        out_buffer[out_idx++] = c;
        if (out_idx >= OUT_BUFFER_SIZE) write(out_fd, out_buffer, out_idx), out_idx = 0;
    }
    inline void flush() { if (out_idx > 0) write(out_fd, out_buffer, out_idx), out_idx = 0; }
    struct CIO { ~CIO() { flush(); } } cio;
    template<typename T_>
    CIO& operator>> (CIO& cio, T_& x) {
        char c; bool f;
        for (c = getchar(), f = false; isspace(c); c = getchar());
        if (c == '-') f = true, c = getchar();
        for (x = 0; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ '0');
        if (f) { x = -x; } if (!isspace(c)) { --in_idx; } return cio;
    }
    CIO& operator>> (CIO& cio, char& c) { for (c = getchar(); isspace(c); c = getchar()); return cio; }
    CIO& operator>> (CIO& cio, char* str) {
        char c; size_t len;
        for (c = getchar(); isspace(c); c = getchar());
        for (len = 0; !isspace(c); c = getchar()) str[len++] = c;
        str[len] = '\0'; return cio;
    }
    template<typename T_>
    CIO& operator<< (CIO& cio, T_ x) {
        if (!x) { putchar('0'); return cio; }
        if (x < 0) putchar('-'), x = -x;
        static char stk[21]; size_t top;
        for (top = 0; x; x /= 10) stk[top++] = x % 10;
        for (size_t i = top - 1; ~i; --i) putchar(stk[i] ^ '0');
        return cio;
    }
    CIO& operator<< (CIO& cio, char c) { putchar(c); return cio; }
    CIO& operator<< (CIO& cio, char* str) { for (size_t i = 0; str[i]; ++i) putchar(str[i]); return cio; }
    CIO& operator<< (CIO& cio, const char* str) { for (size_t i = 0; str[i]; ++i) putchar(str[i]); return cio; }
} using FastIO::cio; using FastIO::freopen;