缺省源与常用宏¶
在算法竞赛中,一个简洁而健壮的缺省源可以显著提高代码书写效率,并尽可能规避常见的 I/O 瓶颈与隐蔽 Bug。
现代 C++ 竞赛缺省源¶
集成快速输入输出、常用类型别名、常用极大值与模数常量,以及本地调试宏。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <cassert>
using namespace std;
// 常用类型别名
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
// 常用常量
constexpr int INF = 0x3f3f3f3f;
constexpr ll LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int MOD = 1e9 + 7; // 或 998244353
// 常用快捷宏
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pb push_back
#define fi first
#define se second
// 本地调试输出宏 (编译时传入 -DLOCAL 生效)
#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << (x) << " (Line " << __LINE__ << ")\n"
#else
#define dbg(x)
#endif
void solve() {
// 单组测试数据逻辑
}
int main() {
// 快速 I/O 优化
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
// cin >> T; // 多组测试数据时取消注释
while (T--) {
solve();
}
return 0;
}
核心避坑要点¶
std::endl与'\n':std::endl会强制刷新缓冲区,导致频繁的系统调用大幅拖慢 I/O,请一律使用'\n'。- 禁止混用输入输出流:解除同步后,严禁混用
cin/cout与scanf/printf/getchar,否则可能导致输入输出顺序错乱。 - 整型溢出防御:两个可能超过 \(2 \times 10^9\) 的数相乘时,务必强制类型转换
1LL * a * b或直接使用long long。