快读/快写,超“实用”!

发布时间 2024-01-07 16:44:09作者: cloud_eve

导言

在一些毒瘤题中,不完全是正解 + 正常的输入输出是被卡常然后会 TLE 的。所以这些时候,就需要快读/快写了。

当然,快读/快写也可以帮你在比赛中多水一些分。

1.int && long long

inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
inline void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}

优化

不要忘记带上上面的读写

inline char getchar() {
	if (p1 == p2) {
		p1 = buf;
		p2 = buf + fread(buf, 1, 1 << 20, stdin);
		if (p1 == p2) return EOF;
		else return *p1++;
	}
	return *p1++;
}

fread

namespace fastread {
	const int S = 1 << 16;
	char B[S + 3], *H, *T;
	inline int gc() {
		if (H == T) T = (H = B) + fread(B, 1, S, stdin);
		return (H == T) ? EOF : *H++;
	}
	inline int read() {
		int x, ch;
		while ((ch = gc()) < 48);
		x = ch^'0';
		while ((ch = gc()) > 47) x = x * 10 + (ch^'0');
		return x;
	}
} using fastread::read;
namespace fastwrite {
	const int S = 1 << 16;
	int cnt;
	char B[S + 3];
	inline void write(int x) {
		if (x > 9) write(x / 10);
		B[cnt++] = (x % 10) | 48;
		if (cnt == S) {
			fwrite(B, 1, S, stdout);
			cnt = 0;
		}
	}
	inline void space() {
		B[cnt++] = 32;
		if (cnt == S) {
			fwrite(B, 1, S, stdout);
			cnt = 0;
		}
	}
	inline void endl() {
		B[cnt++] = 10;
		if (cnt == S) {
			fwrite(B, 1, S, stdout);
			cnt = 0;
		}
	}
	inline void show() {
		fwrite(B, 1, cnt, stdout);
		cnt = 0;
	}
} using fastwrite::write, fastwrite::space, fastwrite::show;

更“实用”的


namespace fastIO {
    #define BUF_SIZE 100000
    //fread -> read
    bool IOerror = 0;
    inline char nc() {
        static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
        if(p1 == pend) {
            p1 = buf;
            pend = buf + fread(buf, 1, BUF_SIZE, stdin);
            if(pend == p1) {
                IOerror = 1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
    }
    inline void read(int &x) {
        char ch;
        while(blank(ch = nc()));
        if(IOerror)
            return;
        for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
    }
    inline void readll(long long int &x) {
        char ch;
        while(blank(ch = nc()));
        if(IOerror)
            return;
        for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
    }
    #undef BUF_SIZE
};

更更“实用”的

struct fast_IO{
    /***read***/
    char buf[1000000], *s = buf, *t = buf;
    inline char gc()
    {
        #ifdef LOCAL
        return getchar();
        #endif
        return s == t && (t = (s = buf) + fread(buf, 1, 1000000, stdin), s == t) ? EOF : *s ++ ;
    }

    // read a character
    int chtype = 0;
    inline void chnormal() {chtype = 0;}
    inline void chall() {chtype = 1;}
    inline void chdigit() {chtype = 2;}
    inline void chletter() {chtype = 3;}
    inline char chread()
    {
        char ch = gc();
        while(ch != EOF && ch <= 32) ch = gc();
        return ch;
    }
    inline char digitread()
    {
        char ch = gc();
        while(ch != EOF && (ch < '0' || ch > '9')) ch = gc();
        return ch;
    }
    inline char letterread()
    {
        char ch = gc();
        while(ch != EOF && !('A' <= ch && ch <= 'Z' || 'a' <= ch && ch <= 'z')) ch = gc();
        return ch;
    }

    // read a string
    int strtype = 0;
    inline void strnormal() {strtype = 0;}
    inline void strline() {strtype = 1;}
    inline void strread(char *s)
    {
        char ch = gc();
        while(ch <= 32) ch = gc();
        while(ch > 32) *s ++ = ch, ch = gc();
        *s = 0;
    }
    inline void lineread(char *s)
    {
        char ch = gc();
        while(ch < 32) ch = gc();
        while(ch >= 32) *s ++ = ch, ch = gc();
        *s = 0;
    }

    // read an integer
    int inttype = 0;
    inline void intnormal() {inttype = 0;}
    inline void intunsigned() {inttype = 1;}
    inline unsigned uread()
    {
        char ch = gc();
        unsigned x = 0;
        while(ch < '0' || ch > '9') ch = gc();
        while('0' <= ch && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
        return x;
    }
    inline int read()
    {
        char ch = gc();
        int x = 0, f = 0;
        while(ch < '0' || ch > '9')
        {
            if(ch == '-') f = 1;
            ch = gc();
        }
        if(f) while('0' <= ch && ch <= '9') x = x * 10 - (ch ^ 48), ch = gc();
        else while('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = gc();
        return x;
    }

    // read a long long integer
    int lltype = 0;
    inline void llnormal() {lltype = 0;}
    inline void llunsigned() {lltype = 1;}
    inline unsigned long long ullread()
    {
        char ch = gc();
        unsigned long long x = 0;
        while(ch < '0' || ch > '9') ch = gc();
        while('0' <= ch && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
        return x;
    }
    inline long long llread()
    {
        char ch = gc();
        long long x = 0, f = 0;
        while(ch < '0' || ch > '9')
        {
            if(ch == '-') f = 1;
            ch = gc();
        }
        if(f) while('0' <= ch && ch <= '9') x = x * 10 - (ch ^ 48), ch = gc();
        else while('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = gc();
        return x;
    }

    // read a real number
    inline double dread()
    {
        char ch = gc();
        double x = 0, f = 1;
        while(ch < '0' || ch > '9')
        {
            if(ch == '-') f = -1;
            ch = gc();
        }
        while('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = gc();
        if(ch == '.')
        {
            ch = gc();
            double p = 0.1;
            while('0' <= ch && ch <= '9') x += p * (ch ^ 48), ch = gc(), p *= 0.1;
        }
        return x * f;
    }

    inline fast_IO & operator >> (char &x)
    {
        switch(chtype)
        {
            case 0 : x = chread(); break ;
            case 1 : x = gc(); break ;
            case 2 : x = digitread(); break ;
            case 4 : x = letterread(); break ;
            default : break ;
        }
        return *this;
    }
    inline fast_IO & operator >> (char *x)
    {
        strtype ? lineread(x) : strread(x);
        return *this;
    }
    inline fast_IO & operator >> (string &x)
    {
        static char buf[1000005];
        strtype ? lineread(buf) : strread(buf);
        return x = buf, *this;
    }
    inline fast_IO & operator >> (int &x)
    {
        inttype ? x = uread() : x = read();
        return *this;
    }
    inline fast_IO & operator >> (unsigned &x)
    {
        return x = uread(), *this;
    }
    inline fast_IO & operator >> (long long &x)
    {
        lltype ? x = ullread() : x = llread();
        return *this;
    }
    inline fast_IO & operator >> (unsigned long long &x)
    {
        return x = ullread(), *this;
    }
    inline fast_IO & operator >> (__int128 &x)
    {
        lltype ? x = ullread() : x = llread();
        return *this;
    }
    inline fast_IO & operator >> (unsigned __int128 &x)
    {
        return x = ullread(), *this;
    }
    inline fast_IO & operator >> (float &x)
    {
        return x = dread(), *this;
    }
    inline fast_IO & operator >> (double &x)
    {
        return x = dread(), *this;
    }
    inline fast_IO & operator >> (long double &x)
    {
        return x = dread(), *this;
    }
    inline fast_IO & operator >> (__float128 &x)
    {
        return x = dread(), *this;
    }

    //***Liyixi_i is very lovely***//
    //***Liyixi_i is very lovely***//
    //***Liyixi_i is very lovely***//
    //***Liyixi_i is very lovely***//
    //***Liyixi_i is very lovely***//
    //***Liyixi_i is very lovely***//
    //***Liyixi_i is very lovely***//
    //***Liyixi_i is very lovely***//
    //***Liyixi_i is very lovely***//
    //***Liyixi_i is very lovely***//
    //***Liyixi_i is very lovely***//

    /***write***/
    char obuf[1000000], *p = obuf;
    const char endl = '\n';
    inline void pc(char x)
    {
        p - obuf < 1000000 ? (*p ++ = x) : (fwrite(obuf, p - obuf, 1, stdout), p = obuf, *p ++ = x);
    }
    inline ~fast_IO() {fwrite(obuf, p - obuf, 1, stdout);}
    inline fast_IO & operator << (int x)
    {
        if(!x) return pc(48), *this;
        int len = 0;
        static char c[40];
        if(x < 0) pc('-'), x = -x;
        while(x) c[ ++ len] = (x % 10) ^ 48, x /= 10;
        while(len) pc(c[len -- ]);
        return *this;
    }
    inline fast_IO & operator << (unsigned x)
    {
        if(!x) return pc(48), *this;
        int len = 0;
        static char c[40];
        if(x < 0) pc('-'), x = -x;
        while(x) c[ ++ len] = (x % 10) ^ 48, x /= 10;
        while(len) pc(c[len -- ]);
        return *this;
    }
    inline fast_IO & operator << (long long x)
    {
        if(!x) return pc(48), *this;
        int len = 0;
        static char c[40];
        if(x < 0) pc('-'), x = -x;
        while(x) c[ ++ len] = (x % 10) ^ 48, x /= 10;
        while(len) pc(c[len -- ]);
        return *this;
    }
    inline fast_IO & operator << (unsigned long long x)
    {
        if(!x) return pc(48), *this;
        int len = 0;
        static char c[40];
        if(x < 0) pc('-'), x = -x;
        while(x) c[ ++ len] = (x % 10) ^ 48, x /= 10;
        while(len) pc(c[len -- ]);
        return *this;
    }
    inline fast_IO & operator << (__int128 x)
    {
        if(!x) return pc(48), *this;
        int len = 0;
        static char c[40];
        if(x < 0) pc('-'), x = -x;
        while(x) c[ ++ len] = (x % 10) ^ 48, x /= 10;
        while(len) pc(c[len -- ]);
        return *this;
    }
    inline fast_IO & operator << (unsigned __int128 x)
    {
        if(!x) return pc(48), *this;
        int len = 0;
        static char c[40];
        if(x < 0) pc('-'), x = -x;
        while(x) c[ ++ len] = (x % 10) ^ 48, x /= 10;
        while(len) pc(c[len -- ]);
        return *this;
    }
    inline fast_IO & operator << (char x)
    {
        return pc(x), *this;
    }
    inline fast_IO & operator << (char *x)
    {
        while(*x) pc(*x ++ );
        return *this;
    }
    inline fast_IO & operator << (string x)
    {
        for(char i : x) pc(i);
        return *this;
    }
}IO;

1.5 __int128

struct

使用 cincout 输入、输出即可

namespace fastio {
	struct reader {
		template<typename T>reader&operator>>(T&x) {
			char c = getchar();
			short f = 1;
			while (c < '0' || c > '9') {
				if (c == '-')f *= -1;
				c = getchar();
			}
			x = 0;
			while (c >= '0' && c <= '9') {
				x = (x << 1) + (x << 3) + (c ^ 48);
				c = getchar();
			}
			x *= f;
			return *this;
		}
	} cin;
	struct writer {
		template<typename T>writer&operator<<(T x) {
			if (x == 0)return putchar('0'), *this;
			if (x < 0)putchar('-'), x = -x;
			static int sta[45];
			int top = 0;
			while (x)sta[++top] = x % 10, x /= 10;
			while (top)putchar(sta[top] + '0'), --top;
			return*this;
		}
	} cout;
};
#define cin fastio::cin
#define cout fastio::cout

read && write

#define int __int128
inline void read(int &n) {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	n = x * f;
}
inline void write(int n) {
	if (n < 0) {
		putchar('-');
		n *= -1;
	}
	if (n > 9) write(n / 10);
	putchar(n % 10 + '0');
}
#undef int

2.double (float)

容易出现精度问题,建议最好还是用 scanfprintf

inline double read() {
	double x = 0;
	int flag = 0;
	char ch = 0;
	while (!isdigit(ch)) {
		flag |= (ch == '-');
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + (ch - '0');
		ch = getchar();
	}
	if (ch != '.') return flag ? -x : x;
	int f = 1;
	ch = getchar();
	while (isdigit(ch)) {
		x = x + (ch - '0') * pow(10, -f);
		f++;
		ch = getchar();
	}
	return flag ? -x : x;
}
inline void write(int x) {
	char num[30];
	int cnt = 0;
	if (x == 0) {
		putchar('0');
		return;
	}
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	while (x > 0) {
		num[cnt++] = x % 10 + '0';
		x /= 10;
	}
	while (cnt > 0) {
		putchar(num[--cnt]);
	}
}
inline void write(double x) {
	write(int(x));
	if (x < 0) x = -x;
	x -= int(x);
	if (x != 0) putchar('.');
	while (x) {
		x *= 10;
		putchar(int(x) + '0');
		x -= int(x);
	}
}

3.char、char*、char[]、string

NO.1

使用和 cincout 相同的格式输入、输出,将 c 改为 q 即可

// #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;
		FastIOBase(FILE *f): target(f){}
		~FastIOBase()=default;
	public:
#ifdef OPENIOBUF
		virtual void flush()=0;
#endif
	};
	class FastOutput final: public FastIOBase
	{
	private:
		void __putc(char x)
		{
#ifdef OPENIOBUF
			if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
			putc(x,target);
#endif
		}
		template<typename T>
		void __write(T x)
		{
			char stk[numeric_limits<T>::digits10+1],*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(); }
		void flush() { fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
		FastOutput &operator <<(char x)
		{ return __putc(x),*this; }
		FastOutput &operator <<(const char *s)
		{ for(;*s;__putc(*(s++)));return *this; }
		FastOutput &operator <<(const std::string &s)
		{ return (*this)<<s.c_str(); }
		template<typename T>
		std::enable_if_t<std::is_integral<T>::value,FastOutput&> operator <<(const T &x)
		{ return __write(x),*this; }
		template<typename ...T>
		void writesp(const T &...x)
		{ std::initializer_list<int>{(this->operator<<(x),__putc(' '),0)...}; }
		template<typename ...T>
		void writeln(const T &...x)
		{ std::initializer_list<int>{(this->operator<<(x),__putc('\n'),0)...}; }
		template<typename Iter>
		void writesp(Iter begin,Iter end)
		{ while(begin!=end) (*this)<<*(begin++)<<' '; }
		template<typename Iter>
		void writeln(Iter begin,Iter end)
		{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
	}qout;
	class FastInput final: public FastIOBase
	{
	private:
		bool __eof;
	public:
		FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
		{ buf_p=BUFSIZE; }
		void flush() { buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
		bool eof()const { return buf[buf_p]==EOF; }
#else
		{}
		bool eof()const { return feof(target); }
#endif
		char get()
		{
			if(__eof) return EOF;
#ifdef OPENIOBUF
			if(buf_p==BUFSIZE) flush();
			char ch=buf[buf_p++];
#else
			char ch=getc(target);
#endif
			return ~ch?ch:(__eof=true,EOF);
		}
		void unget(char c)
		{
			__eof=false;
#ifdef OPENIOBUF
			buf[--buf_p]=c;
#else
			ungetc(c,target);
#endif
		}
		explicit operator bool()const { return !__eof; }
		FastInput &operator >>(char &x)
		{ while(isspace(x=get()));return *this; }
		template<typename T>
		std::enable_if_t<std::is_integral<T>::value,FastInput&> operator >>(T &x)
		{
			char ch,sym=0;x=0;
			while(isspace(ch=get()));
			if(__eof) return *this;
			if(ch=='-') sym=1,ch=get();
			for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=get());
			return unget(ch),sym?x=-x:x,*this;
		}
		FastInput &operator >>(char *s)
		{
			while(isspace(*s=get()));
			if(__eof) return *this;
			for(;!isspace(*s) && !__eof;*(++s)=get());
			return unget(*s),*s='\0',*this;
		}
		FastInput &operator >>(std::string &s)
		{
			char str_buf[(1<<8)+1]={0},*p=str_buf;
			char *const buf_end=str_buf+(1<<8);
			while(isspace(*p=get()));
			if(__eof) return *this;
			for(s.clear(),p++;;p=str_buf)
			{
				for(;p!=buf_end && !isspace(*p=get()) && !__eof;p++);
				if(p!=buf_end) break;
				s.append(str_buf);
			}
			unget(*p),*p='\0',s.append(str_buf);
			return *this;
		}
		template<typename ...T>
		void read(T &...x)
		{ std::initializer_list<int>{(this->operator>>(x),0)...}; }
		template<typename Iter>
		void read(Iter begin,Iter end)
		{ while(begin!=end) (*this)>>*(begin++); }
	}qin;
	
} // namespace FastIO
using FastIO::qin,FastIO::qout;

NO.2

cincout 的格式,将 c 改为 f

namespace fastio
{
	const int bufl=1<<20;
	const double base1[16]={1,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,1e-11,1e-12,1e-13,1e-14,1e-15};
	const double base2[16]={1,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13,1e14,1e15};
	struct IN{
		FILE *IT;char ibuf[bufl],*is=ibuf,*it=ibuf;
		IN(){IT=stdin;}IN(char *a){IT=fopen(a,"r");}
		inline char getChar(){if(is==it){it=(is=ibuf)+fread(ibuf,1,bufl,IT);if(is==it)return EOF;}return *is++;}
		template<typename Temp>inline void getInt(Temp &a){a=0;int b=0,c=getChar();while(c<48||c>57)b^=(c==45),c=getChar();while(c>=48&&c<=57)a=(a<<1)+(a<<3)+c-48,c=getChar();if(b)a=-a;}
		template<typename Temp>inline void getDouble(Temp &a){a=0;int b=0,c=getChar(),d=0;__int128 e=0,f=0;while(c<48||c>57)b^=(c==45),c=getChar();while(c>=48&&c<=57)e=(e<<1)+(e<<3)+c-48,c=getChar();if(c==46){c=getChar();while(c>=48&&c<=57)d++,f=(f<<1)+(f<<3)+c-48,c=getChar();}a=e+base1[d]*f;if(b)a=-a;}
		IN& operator>>(char &a){a=getChar();return *this;}
		IN& operator>>(char *a){do{*a=getChar();}while(*a<=32);while(*a>32)*++a=getChar();*a=0;return *this;}
		IN& operator>>(string &a){char b=getChar();while(b<=32)b=getChar();while(b>32)a+=b,b=getChar();return *this;}
		IN& operator>>(int &a){getInt(a);return *this;}
		IN& operator>>(long long &a){getInt(a);return *this;}
		IN& operator>>(__int128 &a){getInt(a);return *this;}
		IN& operator>>(float &a){getDouble(a);return *this;}
		IN& operator>>(double &a){getDouble(a);return *this;}
		IN& operator>>(long double &a){getDouble(a);return *this;}
	};
	struct OUT{
		FILE *IT;char obuf[bufl],*os=obuf,*ot=obuf+bufl;int Eps;long double Acc;
		OUT(){IT=stdout,Eps=6,Acc=1e-6;}OUT(char *a){IT=fopen(a,"w"),Eps=6,Acc=1e-6;}
		inline void ChangEps(int x=6){Eps=x;}
		inline void flush(){fwrite(obuf,1,os-obuf,IT);os=obuf;}
		inline void putChar(int a){*os++=a;if(os==ot)flush();}
		template<typename Temp>inline void putInt(Temp a){if(a<0){putChar(45);a=-a;}if(a<10){putChar(a+48);return;}putInt(a/10);putChar(a%10+48);}
		template<typename Temp>inline void putLeading(Temp a,int b){if(!b)return;putLeading(a/10,b-1);putChar(a%10+48);}
		template<typename Temp>inline void putDouble(Temp a){if(a<0){putChar(45);a=-a;}__int128 b=a;putInt(b);a-=b;a*=base2[Eps];b=a+Acc;putChar(46);putLeading(b,Eps);}
		OUT& operator<<(char a){putChar(a);return *this;}
		OUT& operator<<(char *a){while(*a>32)putChar(*a++);return *this;}
		OUT& operator<<(string a){for(auto c:a)putChar(c);return *this;}
		OUT& operator<<(int a){putInt(a);return *this;}
		OUT& operator<<(long long a){putInt(a);return *this;}
		OUT& operator<<(__int128 a){putInt(a);return *this;}
		OUT& operator<<(float a){putDouble(a);return *this;}
		OUT& operator<<(double a){putDouble(a);return *this;}
		OUT& operator<<(long double a){putDouble(a);return *this;}
		~OUT(){flush();}
	};
}
using fastio::IN;
using fastio::OUT;
IN fin;
OUT fout;