版本比较
trick or 坑点记录(技巧性思维)
正文
增加 220 行删除 0 行
372 行未改动全部展开373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596372 行未改动全部展开
例题:
- [P17729 [SCP-J 2026] 进位 / carry](https://www.luogu.com.cn/problem/P17729)。 72. 按位或不具有最优子结构。73. 随机打乱排列:利用 `shuffle`+`mt19937`
```cpp mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); vector<int> permutation(N); for (int i = 0; i < N; i++) permutation[i] = i; shuffle(permutation.begin(), permutation.end(), rng); ```
74.
| 容器 / 类型 | 标准头文件 | 备注 || :--- | :--- | :--- || $\texttt{std::vector}$ | $\texttt{<vector>}$ | 动态数组 || $\texttt{std::deque}$ | $\texttt{<deque>}$ | 双端队列 || $\texttt{std::list}$ | $\texttt{<list>}$ | 双向链表(竞赛很少用) || $\texttt{std::forward\_list}$ | $\texttt{<forward\_list>}$ | 单向链表 || $\texttt{std::queue}$ | $\texttt{<queue>}$ | 队列;注意:$\texttt{queue}$ 内部默认依赖 $\texttt{deque}$,但头文件必须写 $\texttt{<queue>}$ || $\texttt{std::priority\_queue}$ | $\texttt{<queue>}$ | 优先队列(堆) || $\texttt{std::stack}$ | $\texttt{<stack>}$ | 栈 || $\texttt{std::set / std::multiset}$ | $\texttt{<set>}$ | 有序集合 || $\texttt{std::map / std::multimap}$ | $\texttt{<map>}$ | 有序键值对 || $\texttt{std::unordered\_set / unordered\_multiset}$ | $\texttt{<unordered\_set>}$ | 哈希集合 || $\texttt{std::unordered\_map / unordered\_multimap}$ | $\texttt{<unordered\_map>}$ | 哈希 map || $\texttt{std::bitset}$ | $\texttt{<bitset>}$ | 定长位集 || $\texttt{std::array}$ | $\texttt{<array>}$ | C++11,固定大小数组 || $\texttt{std::string}$ | $\texttt{<string>}$ | 字符串!高频坑:很多人以为 $\texttt{iostream}$ 带 $\texttt{string}$ || $\texttt{std::pair<T1,T2>}$ | $\texttt{<utility>}$ | 你刚才遇到的! || $\texttt{std::tuple}$ | $\texttt{<tuple>}$ | 多元组 || $\texttt{std::optional}$ | $\texttt{<optional>}$ | C++17 || $\texttt{std::variant}$ | $\texttt{<variant>}$ | C++17 |
| 东西 | 头文件 | 备注 || :--- | :--- | :--- || $\texttt{sort / lower\_bound / upper\_bound / max\_element / next\_permutation}$ 等算法 | $\texttt{<algorithm>}$ | **超级大坑!很多人漏写,GCC 偶尔能过** || $\texttt{std::iterator}$ 迭代器基类、迭代器辅助函数 | $\texttt{<iterator>}$ | || $\texttt{std::functional : greater<T>()、less<T>()}$ | $\texttt{<functional>}$ | $\texttt{priority\_queue}$ 自定义比较器要用 || $\texttt{numeric : accumulate}$ 求和、$\texttt{gcd}$ (C++17) | $\texttt{<numeric>}$ | || $\texttt{cmath : sqrt、sin、floor、ceil、pow}$ | $\texttt{<cmath>}$ | 注意:和 $\texttt{cstdio}$ 区分 || $\texttt{cstdlib : rand、malloc、free、exit}$ | $\texttt{<cstdlib>}$ | |
| 内容 | 头文件 || :--- | :--- || $\texttt{cin, cout, cerr}$ | $\texttt{<iostream>}$ || $\texttt{printf, scanf, puts, getchar}$ | $\texttt{<cstdio>}$ || $\texttt{memset, strlen, strcpy, strcmp}$ | $\texttt{<cstring>}$ || $\texttt{time, clock}$ | $\texttt{<ctime>}$ || $\texttt{climits : INT\_MAX、INT\_MIN}$ | $\texttt{<climits>}$ || $\texttt{cstdint : int64\_t、uint32\_t}$ | $\texttt{<cstdint>}$ || $\texttt{cassert : assert()}$ | $\texttt{<cassert>}$ |
宏定义 $\texttt{ll}$、$\texttt{rep}$ 是你自己写的,不需要头文件。
75. 在离线二维数点中,我使用了如下错误写法:
::::error[错误示范] ```cpp lines=39 line-numbers #include<iostream> #include<cstdio> #include<vector> #define ll long long #define rep(i,l,r) for(int i=(l);i<=(r);++i) using namespace std; const int N=2e6+10; int n,m; int V; int a[N],ans[N]; struct node{ int x,op,id; }; vector<node> Q[N]; int lowbit(int x){ return x&(-x); } struct bit{ int c[N]; void update(int x,int y){ for(int i=x;i<=V;i+=lowbit(i))c[i]+=y; } int query(int x){ int res=0; for(int i=x;i;i-=lowbit(i))res+=c[i]; return res; } }Tr; int main(){ scanf("%d %d",&n,&m); rep(i,1,n){ scanf("%d",&a[i]); V=max(V,a[i]); } rep(_,1,m){ int l,r,x; scanf("%d %d %d",&l,&r,&x); Q[r].push_back(node{x,1,_}); Q[l-1].push_back(node{x,-1,_}); } for(int i=1;i<=n;++i){ Tr.update(a[i],1); for(auto G:Q[i]){ int x=G.x,op=G.op,id=G.id; ans[id]+=Tr.query(x)*op; } } rep(i,1,m)printf("%d\n",ans[i]); return 0; } ``` ::::
在这个示例中,我对于输入的 $a_i$,记录 $V$ 表示 $a_i$ 值域上界,但是询问的 $x$ 可以大于 $V$,这导致了树状数组在查询时,对应 $c$ 数组没有值。
76. 树状数组可以看做一颗没有右子树的线段树。
77. 局部变量如果未初始化,会导致随机赋垃圾值,这是因为局部变量是调用栈内存,栈会被复用,若未初始化,就可能用的是以前剩下的垃圾值。
78. 解决**局部变量结构体初始化**方法:
- `node A{}` - 构造函数。
```cpp struct Martrix { int n, m; int c[110][110]; // 1. 默认构造函数(无参) // 当你写 Martrix A; 时调用 Martrix() : n(0), m(0), c{} {} // 2. 带参构造函数 // 当你写 Martrix A(3, 3); 时调用 Martrix(int _n, int _m) : n(_n), m(_m), c{} {} // 3. (可选)拷贝构造函数 // 一般不用手写,编译器默认生成的就能完成深拷贝 // 但如果你手动写了析构函数或指针成员,就需要自己写 }; ```79.
# $\texttt{scanf}$ 变量占位符对照表
在 C/C++ 中,使用 $\texttt{scanf}$ 和 $\texttt{printf}$ 进行输入输出时,格式控制符(占位符)的匹配非常关键。特别要注意 $\texttt{double}$ 类型在输入和输出时的区别(超级大坑!)。
## 1. 基础数据类型对照表
| 数据类型 | $\texttt{scanf}$ 占位符 | $\texttt{printf}$ 占位符 | 备注与易错点 || :--- | :--- | :--- | :--- || $\texttt{int}$ | $\texttt{\%d}$ | $\texttt{\%d}$ | 最常用 || $\texttt{long long}$ | $\texttt{\%lld}$ | $\texttt{\%lld}$ | 在部分旧版 Windows 环境(如 MinGW)中可能需要用 $\texttt{\%I64d}$ || $\texttt{short}$ | $\texttt{\%hd}$ | $\texttt{\%hd}$ | 短整型 || $\texttt{unsigned int}$ | $\texttt{\%u}$ | $\texttt{\%u}$ | 无符号整型 || $\texttt{unsigned long long}$ | $\texttt{\%llu}$ | $\texttt{\%llu}$ | 无符号长整型 || $\texttt{float}$ | $\texttt{\%f}$ | $\texttt{\%f}$ | 单精度浮点数 || $\texttt{double}$ | $\texttt{\%lf}$ | $\texttt{\%f}$ | **⚠️ 核心大坑:** $\texttt{scanf}$ 必须用 $\texttt{\%lf}$(代表 long float),但 $\texttt{printf}$ 用 $\texttt{\%f}$ 即可(C99 后 $\texttt{\%lf}$ 也兼容,但传统标准是 $\texttt{\%f}$) || $\texttt{long double}$ | $\texttt{\%Lf}$ | $\texttt{\%Lf}$ | 注意是大写 $\texttt{L}$ || $\texttt{char}$ | $\texttt{\%c}$ | $\texttt{\%c}$ | **⚠️ 易错点:** 会读取空格和换行符。通常写成 $\texttt{scanf("\%c", \&ch)}$ 跳过前导空白字符 || $\texttt{char[]}$ (字符串) | $\texttt{\%s}$ | $\texttt{\%s}$ | 遇到空格或换行符停止读取 || 指针 (地址) | $\texttt{\%p}$ | $\texttt{\%p}$ | 打印内存地址 |
## 2. 进制与其他格式
| 格式需求 | $\texttt{scanf}$ 占位符 | $\texttt{printf}$ 占位符 | 说明 || :--- | :--- | :--- | :--- || 八进制整数 | $\texttt{\%o}$ | $\texttt{\%o}$ | 输入/输出无前缀的八进制 || 十六进制整数 | $\texttt{\%x}$ 或 $\texttt{\%X}$ | $\texttt{\%x}$ 或 $\texttt{\%X}$ | 输入时可带 $\texttt{0x}$ 前缀,输出大小写看占位符 || 限定宽度读取 | $\texttt{\%3d}$ | $\texttt{\%3d}$ | 例如 $\texttt{\%3d}$ 表示最多读取 3 位整数 || 指定精度输出 | 不适用 | $\texttt{\%.2f}$ | 仅用于 $\texttt{printf}$,保留 2 位小数 || 读入整行字符串 | $\texttt{\%[^\textbackslash n]}$ | 不适用 | 读取直到遇到换行符为止(包含空格) |
## 3. 常见易错组合示例
### (1) $\texttt{double}$ 的输入输出$$\begin{array}{l}\texttt{double x = 3.14159;} \\\texttt{scanf("\%lf", \&x); \quad // 输入必须用 \%lf} \\\texttt{printf("\%f\textbackslash n", x); \quad // 输出通常用 \%f} \\\end{array}$$
### (2) $\texttt{long long}$ 的输入输出$$\begin{array}{l}\texttt{long long n = 10000000000LL;} \\\texttt{scanf("\%lld", \&n);} \\\texttt{printf("\%lld\textbackslash n", n);} \\\end{array}$$
### (3) 混合读取数字与字符(如读取数字后立刻读字符)$$\begin{array}{l}\texttt{int n;} \\\texttt{char c;} \\\texttt{scanf("\%d", \&n);} \\\texttt{scanf(" \%c", \&c); \quad // 注意 \%c 前面的空格,用于吸收上一次输入遗留的换行符} \\\end{array}$$
80. 关于一类有理数不等式化为整数不等式技巧。
运用:[P5656 【模板】二元一次不定方程 (exgcd)](https://www.luogu.com.cn/problem/P5656)。
- 若 $p,q \in \mathbb{Q}$,且有 $p \le x \le q$,则有 $\lceil p \rceil \le x \le \lfloor q \rfloor$。 - 若 $p,q \in \mathbb{Q}$,且有 $p < x < q$,则有 $\lfloor p \rfloor +1 \le x \le \lceil q \rceil - 1$。
81. 在线段树叶子节点要小心,若访问不存在的叶子的左右儿子会越界!