跳到正文

剪贴板

剪贴板 819fu5fd

原文
剪贴板信息

剪贴板信息

收录
更新

不想自己写

谢谢ek

C++
#include <bits/stdc++.h>
using namespace std;
// #define OPENIOBUF

namespace FastIO
{

class FastIOBase
{
 protected:
#ifdef OPENIOBUF
	static const int BUFSIZE=1<<16;
	char buf[BUFSIZE+1];
	int buf_p=0;
#endif
	FILE *target;
 public:
#ifdef OPENIOBUF
	virtual void flush()=0;
#endif
	FastIOBase(FILE *f): target(f){}
	~FastIOBase()=default;
};

class FastOutput final: public FastIOBase
{
#ifdef OPENIOBUF
 public:
	inline void flush()
	{ fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
 private:
	inline void __putc(char x)
	{
#ifdef OPENIOBUF
		if(buf[buf_p++]=x,buf_p==BUFSIZE)flush();
#else
		putc(x,target);
#endif
	}
	template<typename T>
	inline void __write(T x)
	{
		static char stk[64],*top;top=stk;
		if(x<0) return __putc('-'),__write(-x);
		do *(top++)=x%10,x/=10; while(x);
		for(;top!=stk;__putc(*(--top)+'0'));
	}
 public:
	FastOutput(FILE *f=stdout): FastIOBase(f){}
#ifdef OPENIOBUF
	~FastOutput(){ flush(); }
#endif
	inline FastOutput &operator <<(char x)
	{ return __putc(x),*this; }
	inline FastOutput &operator <<(const char *s)
	{ for(;*s;__putc(*(s++)));return *this; }
	inline FastOutput &operator <<(const string &s)
	{ return (*this)<<s.c_str(); }
	template<typename T>
	inline enable_if_t<is_integral<T>::value,FastOutput&> operator <<(const T &x)
	{ return __write(x),*this; }
	template<typename ...T>
	inline void writesp(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc(' '),0)...}; }
	template<typename ...T>
	inline void writeln(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc('\n'),0)...}; }
	template<typename Iter>
	inline void writesp(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<' '; }
	template<typename Iter>
	inline void writeln(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }

}qout;

class FastInput final: public FastIOBase
{
#ifdef OPENIOBUF
 public:
	inline void flush()
	{ buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
#endif
 private:
	bool __eof,__ok;
	inline char __getc()
	{
		if(__eof) return EOF;
#ifdef OPENIOBUF
		if(buf_p==BUFSIZE) flush();
		return ~buf[buf_p]?buf[buf_p++]:(__eof=true,EOF);
#else
		char ch=getc(target);
		return ~ch?ch:(__eof=true,EOF);
#endif
	}
 public:
	FastInput(FILE *f=stdin): FastIOBase(f),__eof(false),__ok(true)
#ifdef OPENIOBUF
	{ buf_p=BUFSIZE; }
#else
	{}
#endif
	inline bool eof()const { return __eof; }
	inline char peek() { return __getc(); }
	explicit inline operator bool()const { return __ok; }
	inline FastInput &operator >>(char &x)
	{ while(isspace(x=__getc()));return *this; }
	template<typename T>
	inline enable_if_t<is_integral<T>::value,FastInput&> operator >>(T &x)
	{
		char ch,sym=0;
		while(isspace(ch=__getc()));
		if(ch=='-') sym=1,ch=__getc();
		if(__eof) return __ok=false,*this;
		for(x=0;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=__getc());
		return sym?x=-x:x,*this;
	}
	inline FastInput &operator >>(char *s)
	{
		while(isspace(*s=__getc()));
		if(__eof) return __ok=false,*this;
		for(;!isspace(*s) && !__eof;*(++s)=__getc());
		return *s='\0',*this;
	}
	inline FastInput &operator >>(string &s)
	{
		char str_buf[(1<<8)+1],*p=str_buf;
		char *const buf_end=str_buf+(1<<8);
		while(isspace(*p=__getc()));
		if(__eof) return __ok=false,*this;
		for(s.clear(),p++;;p=str_buf)
		{
			for(;p!=buf_end && !isspace(*p=__getc()) && !__eof;p++);
			*p='\0',s.append(str_buf);
			if(p!=buf_end) break;
		}
		return *this;
	}
	template<typename ...T>
	inline void read(T &...x)
	{ initializer_list<int>{(this->operator>>(x),0)...}; }
	template<typename Iter>
	inline void read(Iter begin,Iter end)
	{ while(begin!=end) (*this)>>*(begin++); }
}qin;

} // namespace FastIO
using FastIO::qin,FastIO::qout;

谢谢卷王

C++
#define OPENIOBUF

namespace FastIO {
    class FastIOBase {
    protected:
#ifdef OPENIOBUF
        static const int BUFSIZE = 1 << 22;
        char buf[BUFSIZE + 1];
        int buf_p = 0;
#endif
        FILE *target;

    public:
#ifdef OPENIOBUF
        virtual void flush() = 0;
#endif
        FastIOBase(FILE *f) : target(f) {}
        ~FastIOBase() = default;
    };

    class FastOutput : public FastIOBase {
#ifdef OPENIOBUF
    public:
        inline void flush() { fwrite(buf, 1, buf_p, target), buf_p = 0; }
#endif
    protected:
        inline void __putc(char x) {
#ifdef OPENIOBUF
            if (buf[buf_p++] = x, buf_p == BUFSIZE)
                flush();
#else
            putc(x, target);
#endif
        }
        template <typename T>
        inline void __write(T x) {
            static char stk[64], *top;
            top = stk;
            if (x < 0)
                return __putc('-'), __write(-x);
            do *(top++) = x % 10, x /= 10;
            while (x);
            for (; top != stk; __putc(*(--top) + '0'));
        }

    public:
        FastOutput(FILE *f = stdout) : FastIOBase(f) {}
#ifdef OPENIOBUF
        inline void setTarget(FILE *f) { this->flush(), target = f; }
        ~FastOutput() { flush(); }
#else
        inline void setTarget(FILE *f) { target = f; }
#endif
        template <typename... T>
        inline void writesp(const T &...x) { initializer_list<int>{(this->operator<<(x), __putc(' '), 0)...}; }
        template <typename... T>
        inline void writeln(const T &...x) { initializer_list<int>{(this->operator<<(x), __putc('\n'), 0)...}; }
        inline FastOutput &operator<<(char x) { return __putc(x), *this; }
        inline FastOutput &operator<<(const char *s) {
            for (; *s; __putc(*(s++)));
            return *this;
        }
        inline FastOutput &operator<<(const string &s) { return (*this) << s.c_str(); }
        template <typename T, typename = typename enable_if<is_integral<T>::value>::type>
        inline FastOutput &operator<<(const T &x) { return __write(x), *this; }
    };
    FastOutput qout = FastOutput(stdout);
    FastOutput qerr = FastOutput(stderr);

    class FastInput : public FastIOBase {
#ifdef OPENIOBUF
    public:
        inline void flush() { buf[fread(buf, 1, BUFSIZE, target)] = '\0', buf_p = 0; }
#endif
    protected:
        inline char __getc() {
#ifdef OPENIOBUF
            if (buf_p == BUFSIZE)
                flush();
            return buf[buf_p++];
#else
            return getc(target);
#endif
        }
    public:
#ifdef OPENIOBUF
        FastInput(FILE *f = stdin) : FastIOBase(f) { buf_p = BUFSIZE; }
        inline void setTarget(FILE *f) { this->flush(), target = f; }
#else
        FastInput(FILE *f = stdin) : FastIOBase(f) {}
        inline void setTarget(FILE *f) { target = f; }
#endif
        inline char getchar() { return __getc(); }
        template <typename... T>
        inline void read(T &...x) { initializer_list<int>{(this->operator>>(x), 0)...}; }
        inline FastInput &operator>>(char &x) {
            while (isspace(x = __getc()));
            return *this;
        }
        template <typename T, typename = typename enable_if<is_integral<T>::value>::type>
        inline FastInput &operator>>(T &x) {
            static char ch, sym;
            x = sym = 0;
            while (isspace(ch = __getc()));
            if (ch == '-') sym = 1, ch = __getc();
            for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = __getc());
            return sym ? x = -x : x, *this;
        }
        inline FastInput &operator>>(char *s) {
            while (isspace(*s = __getc()));
            for (; !isspace(*s) && *s && ~*s; *(++s) = __getc());
            return *s = '\0', *this;
        }
        inline FastInput &operator>>(string &s) {
            char str_buf[(1 << 8) + 1], *p = str_buf;
            char *const buf_end = str_buf + (1 << 8);
            while (isspace(*p = __getc()));
            for (s.clear(), p++;; p = str_buf) {
                for (; p != buf_end && !isspace(*p = __getc()) && *p && ~*p; p++);
                *p = '\0', s.append(str_buf);
                if (p != buf_end) break;
            }
            return *this;
        }
    } qin;
}
using namespace FastIO;

Fast-IO

使用总纲:fread/fwrite 切忌与其它 STL 读入方式混用!

Ver1\mathfrak{Ver1}

C++
namespace IO {
    char ibuf[(1 << 20) + 1], *iS, *iT, obuf[(1 << 20) + 1], *oS = obuf;
#define writesp(x) write(x), pc(' ')
#define writeln(x) write(x), pc('\n')
#if ONLINE_JUDGE
#define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), (iS == iT ? EOF : *iS++) : *iS++)
#else
#define gh() getchar()
#endif
	template<typename T = int>
    inline T read() {
        char ch = gh();
        T x = 0;
        bool t = 0;
        while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
        while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gh();
        return t ? ~(x - 1) : x;
    }
    inline void flush () {
    	fwrite(obuf, 1, oS - obuf, stdout), oS = obuf;
	}
    inline void pc (char ch) {
    	if (oS == obuf + (1 << 20) + 1) flush();
    	*oS++ = ch;
	}
	template<typename _Tp>
    inline void write (_Tp x) {
    	static char stk[64], *tp = stk;
    	if (x < 0) x = ~(x - 1), pc('-');
		do *tp++ = x % 10, x /= 10;
		while (x);
		while (tp != stk) pc((*--tp) | 48);
    }
}

using IO::read;
using IO::write;
using IO::pc;
using IO::flush;

return 0 时要手动 flush。

Ver2\mathfrak{Ver2}

C++
namespace FastIO {
	struct IO {
	    char ibuf[(1 << 20) + 1], *iS, *iT, obuf[(1 << 20) + 1], *oS;
	    IO() : iS(ibuf), iT(ibuf), oS(obuf) {}
		~IO() { fwrite(obuf, 1, oS - obuf, stdout); }
		#if ONLINE_JUDGE
		#define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), (iS == iT ? EOF : *iS++) : *iS++)
		#else
		#define gh() getchar()
		#endif
		template<typename T = int>
	    inline T read() {
	        char ch = gh();
	        T x = 0;
	        bool t = 0;
	        while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
	        while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gh();
	        return t ? ~(x - 1) : x;
	    }
	    inline void pc (char ch) {
	    	if (oS == obuf + (1 << 20) + 1) fwrite(obuf, 1, oS - obuf, stdout), oS = obuf;
	    	*oS++ = ch;
		}
		template<typename _Tp>
	    inline void write (_Tp x) {
	    	static char stk[64], *tp = stk;
	    	if (x < 0) x = ~(x - 1), pc('-');
			do *tp++ = x % 10, x /= 10;
			while (x);
			while (tp != stk) pc((*--tp) | 48);
	    }
	} io;
	inline int rd () { return io.read(); }
	inline long long rdl () { return io.read<long long>(); }
	template<typename _Tp>
	inline void write (_Tp x) { io.write(x); }
}

using FastIO::rd;
using FastIO::rdl;
using FastIO::wr;
using FastIO::pc;

Ver3\mathfrak{Ver3}

C++
namespace FastIO {
	struct IO {
	    char ibuf[(1 << 20) + 1], *iS, *iT, obuf[(1 << 20) + 1], *oS;
	    IO() : iS(ibuf), iT(ibuf), oS(obuf) {}
		~IO() { fwrite(obuf, 1, oS - obuf, stdout); }
		#if ONLINE_JUDGE
		#define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), (iS == iT ? EOF : *iS++) : *iS++)
		#else
		#define gh() getchar()
		#endif
		inline bool eof (const char &ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == EOF; }
	    template<typename T = int>
		inline T read() {
	        char ch = gh();
	        T x = 0;
	        bool t = 0;
	        while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
	        while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gh();
	        return t ? ~(x - 1) : x;
	    }
	    inline void read (char *s) {
	    	char ch = gh();
			int l = 0;
	    	while (eof(ch)) ch = gh();
	    	while (!eof(ch)) s[l++] = ch, ch = gh();
	    }
	    inline void pc (char ch) {
	    	if (oS == obuf + (1 << 20) + 1) fwrite(obuf, 1, oS - obuf, stdout), oS = obuf;
	    	*oS++ = ch;
		}
		template<typename _Tp>
	    inline void write (_Tp x) {
	    	static char stk[64], *tp = stk;
	    	if (x < 0) x = ~(x - 1), pc('-');
			do *tp++ = x % 10, x /= 10;
			while (x);
			while (tp != stk) pc((*--tp) | 48);
	    }
	    inline void write (char *s) {
	    	int n = strlen(s);
	    	for (int i = 0; i < n; i++) pc(s[i]);
	    }
	} io;
	inline int rd () { return io.read(); }
	inline long long rdl () { return io.read<long long>();	}
	template<typename Tp>
	inline void rd (Tp &x) { io.read(x); }
	template<typename _Tp>
	inline void wr (_Tp x) { io.write(x); }
}

using FastIO::rd;
using FastIO::rdl;
using FastIO::wr;
using FastIO::pc;

可读写字符串。