跳到正文

个人记录

C++ 火车头模板

_LUOGU_USER_约 9 分钟
原文
文章信息与版本

文章信息

发布
2022年2月17日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 9 分钟

版本

  1. v1当前
C++
#ifndef ONLINE_JUDGE
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#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")
#endif

#include <cstdio>
#include <string>
using namespace std;

typedef long long ll;

const int FASTOJ_BASE = 1000000;

#ifndef FOJ_NO_GETCHAR_FAST
char *p1, *p2, buf[FASTOJ_BASE];
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++)
#endif

struct InStream {
	public:
		InStream& operator >> (int& x);
		InStream& operator >> (ll& x);
		InStream& operator >> (char& x);
		InStream& operator >> (char *x);
		InStream& operator >> (string& x);
	private:
		int Iread();
		ll Lread();
};

inline int InStream::Iread() {
	int x = 0, f = 1;
	char ch;
	ch = getchar();
	while (ch < 48 || ch > 57) {
		if (ch == '-') f = -f;
		ch = getchar();
	}
	while (ch >= 48 && ch <= 57) {
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	return x * f;
}

inline ll InStream::Lread() {
	ll x = 0, f = 1;
	char ch;
	ch = getchar();
	while (ch < 48 || ch > 57) {
		if (ch == '-') f = -f;
		ch = getchar();
	}
	while (ch >= 48 && ch <= 57) {
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	return x * f;
}

inline InStream& InStream::operator >> (int& x) {
	x = Iread();
	return *this;
}

inline InStream& InStream::operator >> (ll& x) {
	x = Lread();
	return *this;
}

inline InStream& InStream::operator >> (char& x) {
	x = ' ';
	while (x == ' ' || x == '\n') x = getchar();
	return *this;
}

inline InStream& InStream::operator >> (char *x) {
	scanf("%s", x);
	return *this;
}

inline InStream& InStream::operator >> (string& x) {
	x.resize(FASTOJ_BASE);
	scanf("%s", &x[0]);
	return *this;
}

struct OutStream {
	public:
		OutStream& operator << (int x);
		OutStream& operator << (ll x);
		OutStream& operator << (char x);
		OutStream& operator << (char *x);
		OutStream& operator << (const char *x);
		OutStream& operator << (const string& x);
	private:
		void Iwrite(int x);
		void Lwrite(ll x);
};

inline void OutStream::Iwrite(int x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) Iwrite(x / 10);
	putchar(x % 10 + '0');
}

inline void OutStream::Lwrite(ll x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) Lwrite(x / 10);
	putchar(x % 10 + '0');
}

inline OutStream& OutStream::operator << (int x) {
	Iwrite(x);
	return *this;
}

inline OutStream& OutStream::operator << (ll x) {
	Lwrite(x);
	return *this;
}

inline OutStream& OutStream::operator << (char x) {
	putchar(x);
	return *this;
}

inline OutStream& OutStream::operator << (char *x) {
	printf("%s", x);
	return *this;
}

inline OutStream& OutStream::operator << (const char *x) {
	printf("%s", x);
	return *this;
}

inline OutStream& OutStream::operator << (const string& x) {
	printf("%s", x.c_str());
	return *this;
}

InStream cin;
OutStream cout;
char endl = '\n';
原文