1. 必备头文件
#include <iostream> // 标准输入输出流(cin, cout)
#include <vector> // 动态数组 vector
#include <algorithm> // 排序(sort)、二分查找(lower_bound)等
#include <queue> // 队列(queue、priority_queue)
#include <stack> // 栈(stack)
#include <cstring> // memset、memcpy 等内存操作
#include <cmath> // 数学函数(abs, sqrt, pow)
#include <climits> // 极限值(INT_MAX, LLONG_MAX)
#include <unordered_map> // 哈希表(C++11 特性)
//比赛中可以直接用万能头文件
#include <bits/stdc++.h>
using namespace std; // 标准命名空间
2. 常用宏定义
#define rep(i, a, b) for (int i = (a); i <= (b); i++) // 简化循环
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define INF 0x3f3f3f3f // 定义无穷大(十进制为 1061109567)
#define LL long long // 简化长整型
#define PII pair<int, int> // 简化 pair 类型
#define all(x) x.begin(), x.end() // 容器遍历简化
3. 关闭同步流(提速关键)
int main() {
ios::sync_with_stdio(false); // 关闭 C 与 C++ 标准流的同步
cin.tie(nullptr); // 解除 cin 与 cout 的绑定
cout.tie(nullptr); // 可选(某些编译器需要)
// 此后只能使用 cin/cout,不能混用 scanf/printf!
int x;
cin >> x;
cout << x << endl;
return 0;
}
4. 快速读取(适用于超大数据量)
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
// 使用示例
int a = read();
5.数组的初始化
1. 静态数组
const int N = 1e5 + 10;
int a[N]; // 全局数组自动初始化为 0
// 初始化填充
memset(a, 0, sizeof(a)); // 按字节填充(0 或 -1)
fill(a, a + N, 123); // 任意值填充(需包含 <algorithm>)
2. 动态数组(vector)
vector<int> v1; // 空 vector
vector<int> v2(10); // 10 个元素,初始化为 0
vector<int> v3(10, 5); // 10 个 5
vector<int> v4{1, 2, 3}; // 初始化列表(C++11)
// 二维 vector
vector<vector<int>> vv(3, vector<int>(4, 0)); // 3 行 4 列,初始为 0
// 预分配空间(减少扩容开销)
v1.reserve(1000); // 预留容量(size 不变)
6.其他实用技巧
1. 加快编译速度
#pragma GCC optimize(2) // O2 优化(部分竞赛允许)
#pragma GCC optimize(3, "Ofast", "inline")
2. 调试宏
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "] = "; _debug(__VA_ARGS__);
#else
#define debug(...) 42
#endif
template <typename T>
void _debug(const T& x) {
cerr << x << '\n';
}
- 本地调试时输出日志,提交时自动忽略。
3. 无穷大的选择
-
0x3f3f3f3f(约 1e9)的优点:-
足够大且相加不会溢出
int范围。 -
用
memset(arr, 0x3f, sizeof(arr))可直接初始化所有元素为该值。```
-
7.主要模版
// 万能头文件(部分竞赛环境可能不支持)
#include <bits/stdc++.h>
// 全局使用 long long(防 int 溢出,但可能略微增加内存)
#define int long long
// 简化 pair 类型(常用在 Dijkstra、BFS 中)
#define PII pair<int, int>
// 换行符优化(比 endl 更快且安全)
#define endl '\n'
// 超大数据类型(需编译器支持,不可直接输入输出)
#define LL __int128
// 标准命名空间
using namespace std;
// 全局常量定义(根据题目调整)
const int N = 2e5 + 10; // 数组大小(1e5 是蓝桥杯常见上限)
const int INF = 0x3f3f3f3f; // 无穷大(兼容 int 和 long long)
// 常用全局数组
int a[N], pre[N]; // a: 数据数组,pre: 前缀和数组
vector<int> graph[N]; // 邻接表存图
// ==================== 常用函数模板 ====================
// 快速读取函数(适合超大数据量)
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
// 快速输出函数(兼容 __int128)
void print(LL x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
// ==================== 主函数 ====================
signed main() {
// 输入输出优化(关键!必须放在 main 开头)
ios::sync_with_stdio(false); // 关闭 C 与 C++ 标准流同步
cin.tie(0); // 解除 cin 与 cout 的绑定
cout.tie(0); // 可选(某些编译器需要)
// ==== 代码逻辑示例 ====
int n = read(); // 快速读取
for (int i = 1; i <= n; i++) {
cin >> a[i]; // 混合使用(需确保关闭同步后不用 scanf)
pre[i] = pre[i-1] + a[i];
}
// 调试输出示例
cout << "前缀和: ";
for (int i = 1; i <= n; i++) {
cout << pre[i] << " \n"[i == n]; // 技巧:末尾换行
}
return 0;
}