跳到正文

剪贴板

剪贴板 ntid3vxs

clx201022
原文
剪贴板信息

剪贴板信息

发布
收录
更新

FastIO

C++
#ifndef FastIO_version
#define FastIO_version 20260821L
#include<cstddef>
#include<cstdio>
#include<cctype>
#include<string>
#include<stdexcept>
namespace fastIO
{
	using namespace std;
	typedef unsigned long long ull;
	typedef unsigned int ui;
	constexpr size_t IOSIZE = 8192;
	ui precision = 6, POW[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
	#if defined(ONLINE_JUDGE) && !defined(FastIO)
	#define FastIO
	#endif
	#ifdef FastIO
	char obuf[IOSIZE], *p3 = obuf, ibuf[IOSIZE], *p1 = ibuf, *p2 = ibuf;
	#define getchar() (((fastIO::p1 == fastIO::p2) && (fastIO::p2 = (fastIO::p1 = fastIO::ibuf) + fread(fastIO::ibuf, 1, fastIO::IOSIZE, stdin)), fastIO::p1 == fastIO::p2) ? (EOF) : (*fastIO::p1++))
	// p2 终止指针 p1 目前要读哪里 一开始从 ibuf 读完就 p1 归零,p2 读新的
	#define putchar(x) ((fastIO::p3 == fastIO::obuf + fastIO::IOSIZE) && (fwrite(fastIO::obuf, fastIO::p3 - obuf, 1, stdout), fastIO::p3 = obuf), *fastIO::p3++ = (x))
	// p3 目前输出已占满的后一位 即输出 [obuf, p3)
	#endif
	template<typename T> 
	inline bool read(T &s)
	{
		s = 0;
		bool w = 0; 
		char ch = getchar();
		for(; !(ch > 47 && ch < 58) && ch != EOF; ch = getchar()) if(ch == '-') w ^= 1;
		if(ch == EOF) return false;
		for(; ch > 47 && ch < 58; ch = getchar()) s = s * 10 + (ch ^ 48);
		if(w) s = -s;
		return true;
	}
	template<typename T> 
	inline void writeu(T x)
	{
		if(x > 9) writeu(x / 10);
		putchar((x % 10) ^ 48);
	}
	template<typename T> 
	inline void write(T x)
	{
		if(x < 0) 
		{
			putchar('-');
			x = -x;
		}
		writeu(x);
	}
	inline bool read(char & s)
	{
		for (s = getchar(); isspace(s) && s != EOF; s = getchar()); 
		return s != EOF;
	}
	inline void write(char x) {putchar(x);}
	inline bool read(char *s)
	{
		char ch = getchar();
		while(ch != EOF && ch < 33) ch = getchar();
		if(ch == EOF) return false;
		for(; ch != EOF && !(ch < 33); ch = getchar()) *s++ = ch;
		*s = '\0';
		return true;
	}
	inline void write(const char *x)
	{
		while(*x) putchar(*x++);
	}
	inline void write(char *x)
	{
		while(*x) putchar(*x++);
	}
	inline bool read(string &s)
	{
		s.clear();	
		char ch = getchar();
		while(ch != EOF && ch < 33) ch = getchar();
		if(ch == EOF) return false;
		for(; ch != EOF && !(ch < 33); ch = getchar()) s += ch;
		return true;
	}
	inline void write(const string &s)
	{
		for(char i : s) putchar(i);
	}
	inline bool read(bool & b)
	{
		char ch = getchar(); 
		while(ch < 33 && ch != EOF) ch = getchar(); 
		return ch == EOF ? false : (b = ch ^ 48, true);
	}
	inline void write(bool b){putchar(b ^ 48);}
	typedef long double ld;
	template<typename T>
	inline bool readf(T &s)
	{
		s = 0;
		char ch = getchar();
		bool f = 0;
		for(; !(ch > 47 && ch < 58) && ch != EOF; ch = getchar()) if(ch == '-') f ^= 1;
		if(ch == EOF) return false;
		for(; ch > 47 && ch < 58; ch = getchar()) s = s * 10 + (ch ^ 48);
		if(ch == '.')
		{
			T y = 0.1;
			for(ch = getchar(); ch > 47 && ch < 58; y /= 10, ch = getchar()) s += y * (ch ^ 48);
		}
		if(f) s = -s;
		return true;
	}
	inline bool read(ld &s) {return readf(s);}
	inline bool read(double &s) {return readf(s);}
	inline bool read(float &s) {return readf(s);}
	template<typename T>
	inline void writef(T x)
	{
		if(x < 0) 
		{
			putchar('-');
			x = -x;
		}
		ull ux = x;
		if(!precision)
		{
			if(x - ux < 0.5) write(ux);
			else write(ux + 1);
			return;
		} // 特判:不用输出 .
		T y = ((x - ux) * POW[precision]);
		ull uy = y;
		ull z = (y - uy < 0.5 ? uy : uy + 1);
		if(z == POW[precision]) ux++, z = 0;
		write(ux), putchar('.');
		for(int j = precision - 1; ~j; j--) putchar((z / POW[j] % 10) ^ 48);
	} // 自动四舍五入
	inline void write(ld x) {return writef(x);}
	inline void write(double x) {return writef(x);}
	inline void write(float x) {return writef(x);}
	template<typename T, typename... restT>
	inline int read(T &s, restT & ...other)
	{
		return read(s) + read(other...);
	}
	template<typename T, typename... restT>
	inline void write(const T &x, const restT &...other)
	{
		return write(x), write(other...);
	}
	template<typename T> 
	inline T read() 
	{
		T a; 
		return read(a), a;
	}
	struct Fast_IO 
	{
		bool flag = 1;
		#ifdef FastIO
		inline ~Fast_IO(){(fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf);}
		#endif
		inline Fast_IO& flush()
		{
			#ifdef FastIO
			(fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf);
			#endif
			return *this;
		}
		// 程序终止调用析构
		inline void SetPrecision(int x){if (x > 9) throw runtime_error("Precision too high!"); else if (x < 0) throw std::runtime_error("Precision too low!"); else precision = x;} // 浮点数精度设为 x,即精确到小数点后 x 位。
		inline operator bool(){return flag;}
	} io;
	Fast_IO & operator >>(Fast_IO & io, char * b){return io.flag &= read(b), io;}
	template<typename T> Fast_IO & operator >>(Fast_IO & io, T & b){return io.flag &= read(b), io;}
	template<typename T> Fast_IO & operator << (Fast_IO & io, T b){return write(b), io;}
	#ifdef FastIO
	#undef getchar
	#undef putchar
	#endif
	char getchar()
	{
		#ifdef FastIO
		return (((p1 == p2) && (p2 = (p1 = ibuf) + fread(ibuf, 1, IOSIZE, stdin)), p1 == p2) ? (EOF) : (*p1++));
		#else
		return getchar();
		#endif
	}
	void putchar(char x)
	{
		#ifdef FastIO
		((p3 == obuf + IOSIZE) && (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf), *p3++ = (x));
		#else
		putchar(x);
		#endif
	}
}
#endif