专栏文章
c++优化“火车头究极版”
个人记录参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @minkuhdg
- 此快照首次捕获于
- 2025/12/02 04:03 3 个月前
- 此快照最后确认于
- 2025/12/02 04:03 3 个月前
CPP
// 究极C++优化火车头
#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")
#include <bits/stdc++.h>
using namespace std;
// 手动循环展开
#define UNROLL_4(x) do { \
x; x; x; x; \
} while(0)
#define UNROLL_8(x) do { \
x; x; x; x; x; x; x; x; \
} while(0)
// 内联函数强制内联
#define ALWAYS_INLINE __attribute__((always_inline)) inline
// 缓存行对齐
#define CACHE_ALIGN __attribute__((aligned(64)))
// 快速数学函数
constexpr double PI = 3.14159265358979323846;
constexpr double E = 2.71828182845904523536;
// 快速IO类
class FastIO {
private:
static const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE], obuf[BUFSIZE];
char *ip1 = ibuf, *ip2 = ibuf;
char *op1 = obuf;
int precision;
ALWAYS_INLINE char getchar() {
if (ip1 == ip2) {
ip1 = ibuf;
ip2 = ibuf + fread(ibuf, 1, BUFSIZE, stdin);
if (ip1 == ip2) return EOF;
}
return *ip1++;
}
ALWAYS_INLINE void putchar(char c) {
if (op1 == obuf + BUFSIZE) {
fwrite(obuf, 1, BUFSIZE, stdout);
op1 = obuf;
}
*op1++ = c;
}
public:
FastIO() : precision(6) {
memset(ibuf, 0, sizeof(ibuf));
memset(obuf, 0, sizeof(obuf));
}
~FastIO() {
fwrite(obuf, 1, op1 - obuf, stdout);
}
template<typename T>
ALWAYS_INLINE void read(T &x) {
x = 0; T f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
x *= f;
}
template<typename T>
ALWAYS_INLINE void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
ALWAYS_INLINE void read(char *s) {
char c = getchar();
while (c <= ' ') c = getchar();
while (c > ' ') *s++ = c, c = getchar();
*s = '\0';
}
ALWAYS_INLINE void write(const char *s) {
while (*s) putchar(*s++);
}
ALWAYS_INLINE void set_precision(int p) {
precision = p;
}
} IO;
// 快速数学函数
ALWAYS_INLINE int popcount(unsigned int x) {
return __builtin_popcount(x);
}
ALWAYS_INLINE int popcount(unsigned long long x) {
return __builtin_popcountll(x);
}
ALWAYS_INLINE int clz(unsigned int x) {
return __builtin_clz(x);
}
ALWAYS_INLINE int ctz(unsigned int x) {
return __builtin_ctz(x);
}
// 内存池(用于动态规划等)
template<typename T, size_t SIZE = 1000000>
class MemoryPool {
private:
T pool[SIZE];
size_t index;
public:
MemoryPool() : index(0) {}
ALWAYS_INLINE T* allocate() {
return &pool[index++];
}
ALWAYS_INLINE void clear() {
index = 0;
}
ALWAYS_INLINE size_t size() const {
return index;
}
};
// 快速排序(特定情况使用)
template<typename T>
ALWAYS_INLINE void quick_sort(T *arr, int l, int r) {
if (l >= r) return;
T pivot = arr[(l + r) >> 1];
int i = l - 1, j = r + 1;
while (i < j) {
do i++; while (arr[i] < pivot);
do j--; while (arr[j] > pivot);
if (i < j) swap(arr[i], arr[j]);
}
quick_sort(arr, l, j);
quick_sort(arr, j + 1, r);
}
// 主函数模板
int main() {
// 终极IO优化
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// 设置浮点数精度(如果需要)
cout.precision(10);
cout << fixed;
// 关闭C流同步
std::ios_base::sync_with_stdio(false);
// 你的代码开始
return 0;
}
// 预编译常用数据结构
namespace FastDS {
// 快速并查集
class UnionFind {
private:
vector<int> parent, rank;
public:
UnionFind(int n) : parent(n), rank(n, 0) {
for (int i = 0; i < n; i++) parent[i] = i;
}
ALWAYS_INLINE int find(int x) {
return parent[x] == x ? x : parent[x] = find(parent[x]);
}
ALWAYS_INLINE void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return;
if (rank[x] < rank[y]) {
parent[x] = y;
} else {
parent[y] = x;
if (rank[x] == rank[y]) rank[x]++;
}
}
ALWAYS_INLINE bool same(int x, int y) {
return find(x) == find(y);
}
};
// 快速前缀和
template<typename T>
class PrefixSum {
private:
vector<T> pre;
public:
PrefixSum(const vector<T> &arr) {
pre.resize(arr.size() + 1);
for (size_t i = 0; i < arr.size(); i++) {
pre[i + 1] = pre[i] + arr[i];
}
}
ALWAYS_INLINE T query(int l, int r) {
return pre[r + 1] - pre[l];
}
};
}
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...