卡常技巧
输入输出优化:
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
快读:
int read() {
int x = 0, f = 1;
char ch = getchar_unlocked();
for(; ch < '0' || ch > '9'; ch = getchar_unlocked()) {
if(ch == '-') {
f = -1;
}
}
for(; ch >= '0' && ch <= '9'; ch = getchar_unlocked()) {
x = (x << 3) + (x << 1) + ch - '0';
}
return x * f;
}
快读2:
namespace _________________________________________________________________________________________{template<size_t...Ints>struct index_sequence{using type=index_sequence;using value_type=size_t;static std::size_t size(){return sizeof...(Ints);}};template<class Sequence1,class Sequence2>struct merge_and_renumber;template<size_t...I1,size_t...I2>struct merge_and_renumber<index_sequence<I1...>,index_sequence<I2...>>:index_sequence<I1...,(sizeof...(I1)+I2)...>{};template<size_t N>struct make_index_sequence:merge_and_renumber<typename make_index_sequence<N/2>::type,typename make_index_sequence<N-N/2>::type>{};template<>struct make_index_sequence<0>:index_sequence<>{};template<>struct make_index_sequence<1>:index_sequence<0>{};template<typename Func,typename Tuple,std::size_t...index>auto apply_helper(Func&&func,Tuple&&tuple,index_sequence<index...>)->decltype(func(std::get<index>(std::forward<Tuple>(tuple))...)){return func(std::get<index>(std::forward<Tuple>(tuple))...);}template<typename Func,typename Tuple>auto apply(Func&&func,Tuple&&tuple)->decltype(apply_helper(std::forward<Func>(func),std::forward<Tuple>(tuple),make_index_sequence<std::tuple_size<typename std::decay<Tuple>::type>::value>{})){return apply_helper(std::forward<Func>(func),std::forward<Tuple>(tuple),make_index_sequence<std::tuple_size<typename std::decay<Tuple>::type>::value>{});}}namespace fast_io{namespace type_traits{template<class Tp>constexpr bool is_char_v=std::is_same<Tp,char>::value||std::is_same<Tp,signed char>::value||std::is_same<Tp,unsigned char>::value;template<class Tp>constexpr bool is_int_v=(std::is_integral<Tp>::value&&std::is_signed<Tp>::value&&!is_char_v<Tp>)||std::is_same<Tp,__int128_t>::value;template<class Tp>constexpr bool is_uint_v=(std::is_integral<Tp>::value&&std::is_unsigned<Tp>::value&&!is_char_v<Tp>)||std::is_same<Tp,__uint128_t>::value;template<class Tp>using make_uint_t=typename std::conditional_t<(std::is_same<Tp,__int128_t>::value||std::is_same<Tp,__uint128_t>::value),std::common_type<__uint128_t>,typename std::conditional_t<std::is_signed<Tp>::value,std::make_unsigned<Tp>,std::common_type<Tp>>>::type;}template<size_t BUFFER_SIZE>class FastIn{using self=FastIn<BUFFER_SIZE>;protected:char buffer_[BUFFER_SIZE],*now_=buffer_,*end_=buffer_;FILE*file_;public:explicit FastIn(FILE*file=stdin):file_(file){}char fetch(){return now_==end_&&(end_=(now_=buffer_)+fread(buffer_,1,BUFFER_SIZE,file_),now_==end_)?EOF:*(now_)++;}char visit(){return now_==end_&&(end_=(now_=buffer_)+fread(buffer_,1,BUFFER_SIZE,file_),now_==end_)?EOF:*(now_);}void set_file(FILE*file){file_=file;now_=end_=buffer_;}bool iseof(){return visit()==EOF;}template<class Tp,std::enable_if_t<type_traits::is_int_v<Tp>> * =nullptr>self&read(Tp&n){bool is_neg=false;char ch=fetch();while(!isdigit(ch)){is_neg|=ch=='-';ch=fetch();}n=0;while(isdigit(ch)){(n*=10)+=ch&0x0f;ch=fetch();}if(is_neg)n=-n;return*this;}template<class Tp,std::enable_if_t<type_traits::is_uint_v<Tp>> * =nullptr>self&read(Tp&n){char ch=fetch();while(!isdigit(ch))ch=fetch();n=0;while(isdigit(ch)){(n*=10)+=ch&0x0f;ch=fetch();}return*this;}template<class Tp,std::enable_if_t<type_traits::is_char_v<Tp>> * =nullptr>self&read(Tp&n){while(!isgraph(n=fetch()));return*this;}self&read(char*n){char*n_=n;while(!isgraph(*n_=fetch()));while(isgraph(*(++n_)=fetch()));*n_='\0';return*this;}self&read(std::string&n){n.clear();char n_;while(!isgraph(n_=fetch()));n.push_back(n_);while(isgraph(n_=fetch()))n.push_back(n_);return*this;}template<class Tp,class Up>self&read(std::pair<Tp,Up>&p){return read(p.first).read(p.second);}template<typename...Ts>self&read(std::tuple<Ts...>&p){_________________________________________________________________________________________::apply([&](Ts&...targs){((read(targs)),...);},p);return*this;}self&getline(char*n){char*n_=n;while(!isprint(*n_=fetch()));while(isprint(*(++n_)=fetch()));*n_='\0';return*this;}self&getline(std::string&n){char n_;while(!isprint(n_=fetch()));n.push_back(n_);while(isprint(n_=fetch()))n.push_back(n_);return*this;}template<class Tp,std::enable_if_t<type_traits::is_char_v<Tp>> * =nullptr>self&strict_read(Tp&n){n=fetch();return*this;}template<class Tp>self&operator>>(Tp&val){return read(val);}};template<size_t BUFFER_SIZE,size_t INT_BUFFER_SIZE>class FastOut{using self=FastOut<BUFFER_SIZE,INT_BUFFER_SIZE>;private:char int_buffer_[INT_BUFFER_SIZE],*now_ib_=int_buffer_;protected:FILE*file_;char*now_,buffer_[BUFFER_SIZE];const char*const end_=buffer_+BUFFER_SIZE;public:explicit FastOut(FILE*file=stdout):file_(file),now_(buffer_){}self&operator=(const self&rhs){file_=rhs.file_;now_=buffer_+(rhs.now_-rhs.buffer_);memcpy(buffer_,rhs.buffer_,sizeof(*buffer_)*(rhs.now_-rhs.buffer_));return*this;}FastOut(const self&rhs){*this=rhs;}~FastOut(){flush();}void flush(){fwrite(buffer_,1,now_-buffer_,file_);now_=buffer_;}void rebind(FILE*file){file_=file;}template<class Tp,std::enable_if_t<type_traits::is_char_v<Tp>> * =nullptr>self&write(const Tp&n){if(now_==end_)flush();*(now_++)=n;return*this;}self&write(const char*n){size_t len=strlen(n),l_;const char*n_=n;while(now_+len>=end_){l_=end_-now_;memcpy(now_,n_,l_);now_+=l_;n_+=l_;len-=l_;flush();}memcpy(now_,n_,len);now_+=len;return*this;}template<class Tp,std::enable_if_t<type_traits::is_int_v<Tp>> * =nullptr>self&write(Tp n){if(n<0){write('-');n=-n;}return write(static_cast<typename type_traits::make_uint_t<Tp>>(n));}template<class Tp,std::enable_if_t<type_traits::is_uint_v<Tp>> * =nullptr>self&write(Tp n){now_ib_=int_buffer_+INT_BUFFER_SIZE-1;do{*(--(now_ib_))=char(n%10)|'0';}while(n/=10);return write(now_ib_);}self&write(const std::string&str){return write(str.c_str());}template<class Tp,class Up>self&write(const std::pair<Tp,Up>&p){return write(p.first).space().write(p.second);}template<typename...Ts>self&write(const std::tuple<Ts...>&p){_________________________________________________________________________________________::apply([&](Ts const&...targs){std::size_t n{0};((write(targs).space_if(++n!=sizeof...(Ts))),...);},p);return*this;}self&linebreak(){return write('\n');}self&linebreak_if(bool flag){return flag?linebreak():*this;}self&space(){return write(' ');}self&space_if(bool flag){return flag?space():*this;}template<class Tp>self&operator<<(const Tp&val){return write(val);}};const std::size_t BUFFER_SIZE=(1<<21)+5;FastIn<BUFFER_SIZE>cin;FastOut<BUFFER_SIZE,21>cout;}
#define cin fast_io::cin
#define cout fast_io::cout
指定级别优化:
#pragma GCC target("sse3","sse2","sse","avx","sse4","sse4.1","sse4.2","ssse3","f16c")
#pragma GCC diagnostic error "-fwhole-program","-fcse-skip-blocks","-funsafe-loop-optimizations","-std=c++14"
手动开 O2
:
#pragma GCC optimize(2)
手动开 O3
:
#pragma GCC optimize(3, "Ofast", "inline")
火车头:
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
求佛祖保佑:
/**
* _ooOoo_
* o8888888o
* 88" . "88
* (| -_- |)
* O\ = /O
* ____/'---'\____
* .' \\| |// '.
* / \\||| : |||// \
* / -||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' |_/ |
* \ .-\__ '-' ___/-. /
* ___'. .' /--.--\ '. .'___
* ."" '< '.___\_<|>_/___.' >' "".
* | | : '- \'.:'\ _ /':.'/ - ' : | |
* \ \ '-. \_ __\ /__ _/ .-' / /
* ======'-.____'-.___\_____/___.-'____.-'======
* '=---='
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* 佛祖保佑 永不超时
* 佛曰:
* 写字楼里写字间,写字间里程序员;
* 程序人员写程序,又拿程序换酒钱。
* 酒醒只在网上坐,酒醉还来网下眠;
* 酒醉酒醒日复日,网上网下年复年。
* 但愿老死电脑间,不愿鞠躬老板前;
* 奔驰宝马贵者趣,公交自行程序员。
* 别人笑我忒疯癫,我笑自己命太贱;
*/
本文作者:yaosicheng124
本文链接:https://www.cnblogs.com/yaosicheng124/p/18707957
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步