跳到正文

个人记录

火车头模板

Qinyoulin666约 27 分钟
原文

这是 2026年9月25日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年8月12日
收录
2026年9月25日
更新
2026年11月14日
篇幅
约 27 分钟

版本

  1. v7当前
  2. v6
  3. v5
  4. v4
更早的 3 个版本
  1. v3
  2. v2
  3. v1
C++
namespace FastIO{
	constexpr int IOSIZE=1<<20;
	char ibuf[IOSIZE],obuf[IOSIZE];
	char*p1=ibuf,*p2=ibuf,*p3=obuf;
	inline char _gc(){if(p1==p2){p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin);if(p1==p2)return EOF;}return*p1++;}
	inline void _pc(char c){if(p3==obuf+IOSIZE){fwrite(obuf,1,p3-obuf,stdout);p3=obuf;}*p3++=c;}
	inline bool _isdigit(char c){return c>='0'&&c<='9';}
	inline bool _isspace(char c){return c<=' ';}
	template<typename T,bool IsSigned=std::is_signed<T>::value>struct IntReader{static void read(T&x){char c;while(!_isdigit(c=_gc()));x=c-'0';while(_isdigit(c=_gc()))x=x*10+(c-'0');}};
	template<typename T>struct IntReader<T,true>{static void read(T&x){char c;while(!_isdigit(c=_gc())&&c!='-');bool neg=(c=='-');if(neg)c=_gc();x=c-'0';while(_isdigit(c=_gc()))x=x*10+(c-'0');if(neg)x=-x;}};
	template<typename T,bool IsSigned=std::is_signed<T>::value>struct IntWriter{static void write(T x){if(x==0){_pc('0');return;}char buf[20];int n=0;do{buf[n++]=x%10+'0';x/=10;}while(x);while(n--)_pc(buf[n]);}};
	template<typename T>struct IntWriter<T,true>{static void write(T x){if(x==0){_pc('0');return;}if(x<0){_pc('-');x=-x;}char buf[20];int n=0;do{buf[n++]=x%10+'0';x/=10;}while(x);while(n--)_pc(buf[n]);}};
	inline void read(int&x){IntReader<int>::read(x);}
	inline void read(long long&x){IntReader<long long>::read(x);}
	inline void read(unsigned int&x){IntReader<unsigned int>::read(x);}
	inline void read(unsigned long long&x){IntReader<unsigned long long>::read(x);}
	inline void read(short&x){IntReader<short>::read(x);}
	inline void read(long&x){IntReader<long>::read(x);}
	inline void read(char&x){while(_isspace(x=_gc()));}
	inline void read(bool&b){char c;while(_isspace(c=_gc()));b=(c!='0'&&c!='f');}
	inline void read(std::string&s){char c;while(_isspace(c=_gc()));s.clear();do{s+=c;}while(!_isspace(c=_gc()));}
	inline void read(char *x){char c;int k=0;while(_isspace(c=_gc()));do{x[k]=c;k++;}while(!_isspace(c=_gc()));x[k]='\0';}
	template<typename T>typename std::enable_if<std::is_floating_point<T>::value,void>::type read(T&x){char c;while(!_isdigit(c=_gc())&&c!='-'&&c!='.');bool neg=(c=='-');if(c=='-'||c=='.')c=_gc();x=0;T base=1;while(_isdigit(c)){x=x*10+(c-'0');c=_gc();}if(c=='.'){c=_gc();while(_isdigit(c)){base*=0.1;x+=(c-'0')*base;c=_gc();}}if(neg)x=-x;}
	inline void write(int x){IntWriter<int>::write(x);}
	inline void write(long long x){IntWriter<long long>::write(x);}
	inline void write(unsigned int x){IntWriter<unsigned int>::write(x);}
	inline void write(unsigned long long x){IntWriter<unsigned long long>::write(x);}
	inline void write(short x){IntWriter<short>::write(x);}
	inline void write(long x){IntWriter<long>::write(x);}
	inline void write(char x){_pc(x);}
	inline void write(const char*x){while(*x)_pc(*x++);}
	inline void write(const std::string&s){for(char c:s)_pc(c);}
	inline void write(bool b){_pc(b?'1':'0');}
	template<typename T,typename...Args>inline void read(T&x,Args&...args){read(x);read(args...);}
	template<typename T,typename...Args>inline void write(const T&x,const Args&...args){write(x);write(args...);}
	struct FastIO_{~FastIO_(){if(p3>obuf)fwrite(obuf,1,p3-obuf,stdout);}template<typename T>FastIO_& operator>>(T& x){read(x);return *this;}FastIO_& operator>>(char *x){read(x);return *this;}template<typename T>FastIO_& operator<<(const T& x){write(x);return *this;}}fio;
}using namespace FastIO;
#define cin FastIO::fio
#define cout FastIO::fio
#define endl '\n'

加固后模板:

C++
#include<bits/stdc++.h>
using namespace std;
namespace FastIO{
	constexpr int IOSIZE=1<<30;
	char ibuf[IOSIZE],obuf[IOSIZE];
	char*p1=ibuf,*p2=ibuf,*p3=obuf;
	inline char _gc(){if(p1==p2){p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin);if(p1==p2)return EOF;}return*p1++;}
	inline void _pc(char c){if(p3==obuf+IOSIZE){fwrite(obuf,1,p3-obuf,stdout);p3=obuf;}*p3++=c;}
	inline bool _isdigit(char c){return c>='0'&&c<='9';}
	inline bool _isspace(char c){return c<=' ';}
	template<typename T,bool IsSigned=std::is_signed<T>::value>struct IntReader{static void read(T&x){char c;while(!_isdigit(c=_gc()));x=c-'0';while(_isdigit(c=_gc()))x=x*10+(c-'0');}};
	template<typename T>struct IntReader<T,true>{static void read(T&x){char c;while(!_isdigit(c=_gc())&&c!='-');bool neg=(c=='-');if(neg)c=_gc();x=c-'0';while(_isdigit(c=_gc()))x=x*10+(c-'0');if(neg)x=-x;}};
	template<typename T,bool IsSigned=std::is_signed<T>::value>struct IntWriter{static void write(T x){if(x==0){_pc('0');return;}char buf[20];int n=0;do{buf[n++]=x%10+'0';x/=10;}while(x);while(n--)_pc(buf[n]);}};
	template<typename T>struct IntWriter<T,true>{static void write(T x){using UT=typename std::make_unsigned<T>::type;if(x==0){_pc('0');return;}UT val;if(x<0){_pc('-');val=~static_cast<UT>(x)+1u;}else val=static_cast<UT>(x);char buf[20];int n=0;do{buf[n++]=val%10+'0';val/=10;}while(val);while(n--)_pc(buf[n]);}};
	inline void read(int&x){IntReader<int>::read(x);}
	inline void read(long long&x){IntReader<long long>::read(x);}
	inline void read(unsigned int&x){IntReader<unsigned int>::read(x);}
	inline void read(unsigned long long&x){IntReader<unsigned long long>::read(x);}
	inline void read(short&x){IntReader<short>::read(x);}
	inline void read(long&x){IntReader<long>::read(x);}
	inline void read(char&x){while(_isspace(x=_gc()));}
	inline void read(bool&b){char c;while(_isspace(c=_gc()));b=(c!='0'&&c!='f');}
	inline void read(std::string&s){char c;while(_isspace(c=_gc()));s.clear();do{s+=c;}while(!_isspace(c=_gc()));}
	inline void read(char *x){char c;int k=0;while(_isspace(c=_gc()));do{x[k]=c;k++;}while(!_isspace(c=_gc()));x[k]='\0';}
	template<typename T>typename std::enable_if<std::is_floating_point<T>::value,void>::type read(T&x){char c;while(!_isdigit(c=_gc())&&c!='-'&&c!='.');bool neg=(c=='-');if(c=='-'||c=='.')c=_gc();x=0;T base=1;while(_isdigit(c)){x=x*10+(c-'0');c=_gc();}if(c=='.'){c=_gc();while(_isdigit(c)){base*=0.1;x+=(c-'0')*base;c=_gc();}}if(neg)x=-x;}
	inline void write(int x){IntWriter<int>::write(x);}
	inline void write(long long x){IntWriter<long long>::write(x);}
	inline void write(unsigned int x){IntWriter<unsigned int>::write(x);}
	inline void write(unsigned long long x){IntWriter<unsigned long long>::write(x);}
	inline void write(short x){IntWriter<short>::write(x);}
	inline void write(long x){IntWriter<long>::write(x);}
	inline void write(char x){_pc(x);}
	inline void write(const char*x){while(*x)_pc(*x++);}
	inline void write(const std::string&s){for(char c:s)_pc(c);}
	inline void write(bool b){_pc(b?'1':'0');}
	template<typename T,typename...Args>inline void read(T&x,Args&...args){read(x);read(args...);}
	template<typename T,typename...Args>inline void write(const T&x,const Args&...args){write(x);write(args...);}
	struct FastIO_{~FastIO_(){if(p3>obuf)fwrite(obuf,1,p3-obuf,stdout);}template<typename T>FastIO_& operator>>(T& x){read(x);return *this;}FastIO_& operator>>(char *x){read(x);return *this;}template<typename T>FastIO_& operator<<(const T& x){write(x);return *this;}}fio;
}using namespace FastIO;
#define cin FastIO::fio
#define cout FastIO::fio

超级加固后模板(???)

C++
#include <cstdio>
#include <type_traits>
#include <string>
#include <climits>
#include <cstdint>
#include <cmath>

#if defined(__linux__)
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif

namespace FastIO{
	constexpr int OBUFSIZE=1<<30;
	constexpr int FREAD_BUFSIZE=1<<24;
	char obuf[OBUFSIZE];
	char*p3=obuf;

#if defined(__linux__)
	char*mmap_ptr=nullptr;
	char*mmap_end=nullptr;
	char fbuf[FREAD_BUFSIZE];
	char*p1=fbuf,*p2=fbuf;
	bool use_mmap=false;

	inline void init_mmap(){int fd=fileno(stdin);struct stat st;if((fstat(fd,&st)==0)&(st.st_size>0)){void*p=mmap(nullptr,st.st_size,PROT_READ,MAP_PRIVATE,fd,0);if(p!=(void*)MAP_FAILED){mmap_ptr=(char*)p;mmap_end=mmap_ptr+st.st_size;use_mmap=true;return;}}use_mmap=false;p1=fbuf,p2=fbuf;}
	inline char _gc_mmap(){return (mmap_ptr>=mmap_end)?EOF:*mmap_ptr++;}
	inline char _gc_fread(){if(p1==p2){p2=(p1=fbuf)+fread(fbuf,1,FREAD_BUFSIZE,stdin);if(p1==p2)return EOF;}return *p1++;}
	inline char _gc(){return use_mmap?_gc_mmap():_gc_fread();}
#elif defined(_WIN32)
	char fbuf[FREAD_BUFSIZE];
	char*p1=fbuf,*p2=fbuf;
	inline char _gc(){if(p1==p2){p2=(p1=fbuf)+fread(fbuf,1,FREAD_BUFSIZE,stdin);if(p1==p2)return EOF;}return *p1++;}
	inline void init_mmap(){}
#else
	char fbuf[FREAD_BUFSIZE];
	char*p1=fbuf,*p2=fbuf;
	inline char _gc(){if(p1==p2){p2=(p1=fbuf)+fread(fbuf,1,FREAD_BUFSIZE,stdin);if(p1==p2)return EOF;}return *p1++;}
	inline void init_mmap(){}
#endif

	inline void _pc(char c){if(p3==obuf+OBUFSIZE){fwrite(obuf,1,p3-obuf,stdout);p3=obuf;}*p3++=c;}
	inline bool _isdigit(char c){return (c>='0')&(c<='9');}
	inline bool _isspace(char c){return c<=' ';}

	template<typename T,bool IsSigned=std::is_signed<T>::value>struct IntReader{static void read(T&x){char c;while(!_isdigit(c=_gc()));x=c-'0';while(_isdigit(c=_gc()))x=x*10+(c-'0');}};
	template<typename T>struct IntReader<T,true>{static void read(T&x){char c;while((!_isdigit(c=_gc()))&(c^'-'));bool neg=(c=='-');if(neg)c=_gc();x=c-'0';while(_isdigit(c=_gc()))x=x*10+(c-'0');if(neg)x=-x;}};

	template<typename T,bool IsSigned=std::is_signed<T>::value>struct IntWriter{static void write(T x){if(x==0){_pc('0');return;}char buf[20];int n=0;do{buf[n++]=x%10+'0';x/=10;}while(x);while(n--)_pc(buf[n]);}};
	template<typename T>struct IntWriter<T,true>{static void write(T x){using UT=typename std::make_unsigned<T>::type;if(x==0){_pc('0');return;}UT val;if(x<0){_pc('-');val=~static_cast<UT>(x)+1u;}else val=static_cast<UT>(x);char buf[20];int n=0;do{buf[n++]=val%10+'0';val/=10;}while(val);while(n--)_pc(buf[n]);}};

	inline void read(int&x){IntReader<int>::read(x);}
	inline void read(long long&x){IntReader<long long>::read(x);}
	inline void read(unsigned int&x){IntReader<unsigned int>::read(x);}
	inline void read(unsigned long long&x){IntReader<unsigned long long>::read(x);}
	inline void read(short&x){IntReader<short>::read(x);}
	inline void read(long&x){IntReader<long>::read(x);}
	inline void read(char&x){while(_isspace(x=_gc()));}
	inline void read(bool&b){char c;while(_isspace(c=_gc()));b=(c^'0')&(c^'f');}
	inline void read(std::string&s){char c;while(_isspace(c=_gc()));s.clear();do{s+=c;}while(!_isspace(c=_gc()));}
	inline void read(char *x){char c;int k=0;while(_isspace(c=_gc()));do{x[k]=c;k++;}while(!_isspace(c=_gc()));x[k]='\0';}

	template<typename T>static T pow10(int e){T r=1;if(e>0)for(int i=0;i<e;i++)r*=10;else for(int i=0;i<-e;i++)r*=0.1;return r;}

	template<typename T>typename std::enable_if<std::is_floating_point<T>::value,void>::type read(T&x){
		char c;while((!_isdigit(c=_gc()))&(c^'-')&(c^'.'));
		bool neg=(c=='-');
		if(c=='-')c=_gc();
		uint64_t intpart=0;
		while(_isdigit(c)){intpart=intpart*10+(c-'0');c=_gc();}
		uint64_t fracpart=0;
		int frac_len=0;
		if(c=='.'){
			c=_gc();
			while(_isdigit(c)){fracpart=fracpart*10+(c-'0');frac_len++;c=_gc();}
		}
		T res=(T)intpart;
		if(frac_len>0)res+= (T)fracpart * pow10<T>(-frac_len);
		if((c=='e')|(c=='E')){
			c=_gc();
			bool eneg=(c=='-');
			if((c=='+')|(c=='-'))c=_gc();
			int exp=0;
			while(_isdigit(c)){exp=exp*10+(c-'0');c=_gc();}
			if(eneg)exp=-exp;
			res *= pow10<T>(exp);
		}
		if(neg)res=-res;
		x=res;
	}

	template<typename T>typename std::enable_if<std::is_floating_point<T>::value,void>::type write(T x,int prec=6,bool sci=false){
		if(x<0){_pc('-');x=-x;}
		if(sci){
			int exp=0;
			if(x>0){
				while(x>=10){x/=10;exp++;}
				while(x<1){x*=10;exp--;}
			}
			T rnd=0.5*pow10<T>(-prec);
			x+=rnd;
			long long ip=(long long)x;
			T fp=x-ip;
			IntWriter<long long>::write(ip);
			if(prec>0){
				_pc('.');
				for(int i=0;i<prec;i++){
					fp*=10;
					int d=(int)fp;
					_pc(d+'0');
					fp-=d;
				}
			}
			_pc('e');
			if(exp<0){_pc('-');exp=-exp;}
			else{_pc('+');}
			if(exp<10)_pc('0');
			IntWriter<int>::write(exp);
		}else{
			long long ip=(long long)x;
			T fp=x-ip;
			T rnd=0.5*pow10<T>(-prec);
			fp+=rnd;
			long long carry=(long long)fp;
			ip+=carry;
			fp-=carry;
			IntWriter<long long>::write(ip);
			if(prec>0){
				_pc('.');
				for(int i=0;i<prec;i++){
					fp*=10;
					int d=(int)fp;
					_pc(d+'0');
					fp-=d;
				}
			}
		}
	}

	inline void write(int x){IntWriter<int>::write(x);}
	inline void write(long long x){IntWriter<long long>::write(x);}
	inline void write(unsigned int x){IntWriter<unsigned int>::write(x);}
	inline void write(unsigned long long x){IntWriter<unsigned long long>::write(x);}
	inline void write(short x){IntWriter<short>::write(x);}
	inline void write(long x){IntWriter<long>::write(x);}
	inline void write(char x){_pc(x);}
	inline void write(const char*x){while(*x)_pc(*x++);}
	inline void write(const std::string&s){for(char c:s)_pc(c);}
	inline void write(bool b){_pc(b?'1':'0');}

	template<typename T,typename...Args>inline void read(T&x,Args&...args){read(x);read(args...);}
	template<typename T,typename...Args>inline void write(const T&x,const Args&...args){write(x);write(args...);}

	struct FastIO_{
		FastIO_(){init_mmap();}
		~FastIO_(){if(p3>obuf)fwrite(obuf,1,p3-obuf,stdout);}
		template<typename T>FastIO_& operator>>(T& x){read(x);return *this;}
		FastIO_& operator>>(char *x){read(x);return *this;}
		template<typename T>FastIO_& operator<<(const T& x){write(x);return *this;}
		FastIO_& operator<<(double x){write(x,6,false);return *this;}
		FastIO_& operator<<(float x){write(x,6,false);return *this;}
		// 扩展接口,用于指定精度+科学计数开关
		template<typename T>FastIO_& putf(T x,int prec,bool sci=false){write(x,prec,sci);return *this;}
	}fio;
}
using namespace FastIO;
#define cin FastIO::fio
#define cout FastIO::fio