版本比较
模板-缺省源
正文
增加 506 行删除 335 行
11234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606136263646566891011121367686970717273747516767778187980208182838485868788282930899091929394953536373839404142434445464748495051525354555657589697989910010110210310410510610710810911011111211311411511611711811912012112261123124636465666768697071727374757677787980811251261271281298384858613013113213313413513613713813914014114214314414514614714814988899015015115215392154155949596979899156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209103104105106107108109110111112113114210211212213214215216217218219220221222223224225226227118228229230231232233234235236237238239240241242243244245246247248249250120121122123124125126251252253254255256257258128129130259260261262263133264265266267268269270271138139140141142143144145146147148272273274275276277278279280281282283284285286287288289290291155156157158159160161162163164165166167168169292293294295296297298299300301302303304305306307308309310311312313314315171172173174175316317318319320321322323324325326327328182329330184185331332333334335336337338339340341342188189190191192193194195196343344345198199200201202203346347348349350351205206352353354355356357358359208209210211212360361214215216217218362363364365366367368369370220221222371372373374375376377378224225379380227228229230381382383384385386387388389390391392393394395396397398232233399400401402403404405406407408409410411412413414415416417418419420421422237238239240241242243244245423424425426427428247248249250251252429430431432433434435254255436437438439440441442257258259260261262443444445446447448449450451452453264265266267268454455456457458459460270271272461462463464465466274275276467468469470471472473278474475476477478479480280281282481482483484485486487488489286287288490491492290291493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549293294295296297298299550551552553554555556557301302303558559560561562563564305306307308309310311312313314315316317318319320321322565566567568324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412569570414415571572573417418574420421575576577578579580581582583584585586587588589590424591592593594595596597::::success[算法模板]:::::success[1. 数据结构模板]---:::warning[注意]{open}不要使用 STL!:::
::::info[1.1 基础线性容器]:::info[1.1.1 [B3614 【模板】栈](https://www.luogu.com.cn/problemnew/show/B3614)][AC记录](https://www.luogu.com.cn/record/254020653)
[C++类代码](https://github.com/fruit32820/Content-Delivery-Network/blob/main/luogu/header/classes/structures/mystack.hpp)
练习:> - [P1449 后缀表达式](https://www.luogu.com.cn/problemnew/show/P1449):::
:::info[1.1.2 [B3616 【模板】队列](https://www.luogu.com.cn/problemnew/show/B3616)][AC记录](https://www.luogu.com.cn/record/254019948)
[C++类代码](https://github.com/fruit32820/Content-Delivery-Network/blob/main/luogu/header/classes/structures/myqueue.hpp):::::::
::::info[1.2 单调优化线性容器]:::info[1.2.1 [P5788【模板】单调栈](https://www.luogu.com.cn/problemnew/show/P5788)]```暂无模板代码```::::::info[1.2.2 [P1886【模板】单调队列](https://www.luogu.com.cn/problemnew/show/P1886)]```暂无模板代码```:::::::
::::info[1.3 堆(优先队列)]:::info[1.3.1 [P3378 【模板】堆](https://www.luogu.com.cn/problemnew/show/P3378)][AC记录](https://www.luogu.com.cn/record/260394149)
[C++类代码](https://github.com/fruit32820/Content-Delivery-Network/blob/main/luogu/header/classes/structures/myheap.hpp)
练习:> - [P1177 【模板】排序](https://www.luogu.com.cn/problem/P1177):::::::
::::info[1.4 Trie 字典树]:::info[1.4.1 基础 Trie]```暂无模板代码```::::::::::::
---
:::::success[2. 图论模板]---::::info[2.1 最短路]:::info[[B3647 【模板】Floyd](https://www.luogu.com.cn/problemnew/show/B3647)]:::info[2.1.1 [B3647 【模板】$\text{Floyd}$](https://www.luogu.com.cn/problemnew/show/B3647)][AC记录](https://www.luogu.com.cn/record/253934206) ```cpp line-numbers// 邻接矩阵for(int k=1;k<=n;k++){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ G[i][j]=min(G[i][j],G[i][k]+G[k][j]); G[j][i]=G[i][j];// 有向图删这行 } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ G[i][j]=min(G[i][j],G[i][k]+G[k][j]); G[j][i]=G[i][j];// 有向图删除此行 } }}```练习:- [B3611 【模板】传递闭包](https://www.luogu.com.cn/problemnew/show/B3611)> - [B3611 【模板】传递闭包](https://www.luogu.com.cn/problemnew/show/B3611):::
:::info[[P4779 【模板】单源最短路径(标准版)](https://www.luogu.com.cn/problemnew/show/P4779)]:::info[2.1.2 [P4779 【模板】单源最短路径(标准版)](https://www.luogu.com.cn/problemnew/show/P4779)][AC记录](https://www.luogu.com.cn/record/260525532) 注意:这里可以使用dijkstra,但是$m$离$n$更近,所以选择堆优化的dijkstra,当然,SPFA也行。注意:这里可以使用朴素 $\text{Dijkstra}$,但是 $m$ 离 $n$ 更近,所以选择堆优化的 $\text{Dijkstra}$,当然,$\text{SPFA}$ 也行。```cpp line-numbers#include<bits/stdc++.h>using namespace std;const int INF=(1LL<<31)-1;const int MAXN=1e5+5,MAXM=2e5+5;
struct edge{ int v,w; edge(){} edge(int v_,int w_){ v=v_;w=w_; } int v,w; edge(){} edge(int v_,int w_){ v=v_;w=w_; }};
vector<edge> G[MAXN];
int* heap_dijkstra(const int& s,const int& count){ // 堆优化的dijkstra - O(n logm) struct Edge_Compare{ bool operator()(edge a,edge b){ return a.w>b.w; } }; priority_queue<edge,vector<edge>,Edge_Compare> pq; int n=count+1; int *dis=new int[n+5]; bool *vis=new bool[n+5]; for(int i=0;i<=n;i++) dis[i]=INF; memset(vis,0,sizeof(bool)*n); dis[s]=0; pq.emplace(s,dis[s]); while(!pq.empty()){ int cu=pq.top().v; int d=pq.top().w; pq.pop(); if(vis[cu]) continue; vis[cu]=1; for(const auto& e:G[cu]){ if(dis[e.v]>d+e.w){ dis[e.v]=d+e.w; pq.emplace(e.v,dis[e.v]); } } } return dis;int* heap_dijkstra(const int& s,const int& count){ // 堆优化的 Dijkstra struct Edge_Compare{ bool operator()(edge a,edge b){ return a.w>b.w; } }; priority_queue<edge,vector<edge>,Edge_Compare> pq; int n=count+1; int *dis=new int[n+5]; bool *vis=new bool[n+5]; for(int i=0;i<=n;i++) dis[i]=INF,vis[i]=false; dis[s]=0; pq.emplace(s,dis[s]); while(!pq.empty()){ int cu=pq.top().v; int d=pq.top().w; pq.pop(); if(vis[cu]) continue; vis[cu]=1; for(const auto& e:G[cu]){ if(dis[e.v]>d+e.w){ dis[e.v]=d+e.w; pq.emplace(e.v,dis[e.v]); } } } return dis;}```本代码复杂度:$O(n \log m)$
本题弱化版:[P3371 【模板】单源最短路径(弱化版)](https://www.luogu.com.cn/problemnew/show/P3371)(这道题$m$离$n^2$更近,所以选择朴素dijkstra)。本题弱化版:[P3371 【模板】单源最短路径(弱化版)](https://www.luogu.com.cn/problemnew/show/P3371)(这道题 $m$ 更接近 $n^2$,所以选择朴素 $\text{Dijkstra}$)。::::::info[[P10815 【模板】快速读入](https://www.luogu.com.cn/problemnew/show/P10815)]```cpp line-numbers#include<cstdio>inline long long read() { long long res = 0; char c = getchar(); bool f = false; while (c == ' ' || c == '\n' || c == '\r' || c == '\t') c = getchar(); if (c == '-') { f = true; c = getchar(); } while (c >= '0' && c <= '9') { res = res * 10 + (c - '0'); c = getchar(); } return f ? -res : res;}::::
::::info[2.2 $\textrm{Tarjan}$]:::info[2.2.1 强连通分量]```[教训](https://www.luogu.com.cn/record/282066694) 但该代码使用getchar单字符读入,每次读入都需要访问缓冲区,所以只能应付$10^6$的数据。对于本题$10^8$数据,需要采用以下`fread`一次性读入缓冲区方案。 [AC记录](https://www.luogu.com.cn/record/282147558)```cpp line-numbers暂无模板代码```:::
:::info[2.2.2 缩点]```暂无模板代码```::::::::::::
---
:::::success[3. IO工具模板]---::::info[3.1 快速读入]前置(程序缓冲区的getchar):```cpp// 缓冲区const int BUF_SIZE = 1 << 25;char buf[BUF_SIZE];int p = 0, len = 0;const int BUF_SIZE = 1 << 25; // 如果MLE可以把25改成22,20char inbuf[BUF_SIZE]; // 避免输出缓冲区重名int pin = 0, lenin = 0;
// 一次性填充缓冲区// 一次性填充缓冲区版 getcharinline char gc() { if (p == len) { // buf中还未读入数据 len = fread(buf, 1, BUF_SIZE, stdin); // 从stdin一次性读入 p = 0; if (!len) return EOF; } return buf[p++]; // 后续直接返回buf中的内容,不使用stdin if (pin == lenin) { // inbuf 中已经没有数据可读 lenin = fread(inbuf, 1, BUF_SIZE, stdin); // 从 stdin 一次性读入 pin = 0; if (!lenin) return EOF; } return inbuf[pin++]; // 后续直接返回 inbuf 中的内容,不使用 stdin}
// 判断是否空白字符inline bool isws(char c){ return c == ' ' || c == '\r' || c == '\n' || c == '\t' || c == '\v';}
// 跳过空白字符inline void skws(){ char c; while ((c=gc()) != EOF && isws(c)); // 非空白字符放回缓冲区 if (c != EOF) pin--;}```:::warning如果本地测试时,程序一直在等待输入(光标不停闪烁且可以输入),那么请使用 `Ctrl+D`(Linux)/`Ctrl+Z`(Windows) 终止输入(即键入 `EOF` 标志)。 **一定要换一行!** 不然会出现许多玄学问题(比如导致答案与预期不同,但程序其实没有问题,然后调半天,**尤其是这种需要单字符读入的!**)::::::info[3.1.1 [P10815 【模板】快速读入](https://www.luogu.com.cn/problemnew/show/P10815)(整数)][AC记录](https://www.luogu.com.cn/record/282147558)```cpp line-numbers// 在这里加上前置
inline bool is_digit(char c){ return '0' <= c && c <= '9';}inline long long read_int() { long long res = 0; skws(); char c = gc(); bool neg = false; if (c == '-'){ neg = true; c = gc(); } while (is_digit(c)) { res = (res << 3) + (res << 1) + (c ^ 48); // 位运算优化 c = gc(); } if (c != EOF) pin--; return neg ? -res : res;}```但我们通常会像下面这样写:```cpp line-numbers#include<cstdio>inline long long read() { long long res = 0; char c = gc(); bool f = false; while (c < '0' || c > '9') { if (c == '-') f = true; c = gc(); } while (c >= '0' && c <= '9') { res = res * 10 + (c ^ 48); c = gc(); } return f ? -res : res; long long res = 0; char c = getchar(); bool f = false; while (c == ' ' || c == '\n' || c == '\r' || c == '\t') c = getchar(); if (c == '-') { f = true; c = getchar(); } while (c >= '0' && c <= '9') { res = res * 10 + (c - '0'); c = getchar(); } return f ? -res : res;}```但该代码使用 `getchar` 单字符读入,每次读入都需要访问 `stdin` 缓冲区,所以只能应付 $10^6$ 的数据([关于此的教训](https://www.luogu.com.cn/record/282066694))。对于本题 $10^8$ 数据,需要采用开头的 `fread` 一次性读入缓冲区方案。 ::::::info[`__int128`输出 / 快速输出]
:::info[3.1.2 快速读入字符串]```cpp// 在这里加上前置
inline string read_str() { skws(); vector<char> buf; buf.reserve(1024); // 预分配初始容量减少扩容 char c; while ((c = gc()) != EOF && !isws(c)) { buf.push_back(c); } if (c != EOF) pin--; return string(buf.begin(), buf.end());}```:::::::
::::info[3.2 快速输出]:::info[3.2.1 `__int128` 输出]```cpp line-numbersinline void write(__int128_t n){ // 有符号__int128 if(n < 0){ putchar('-'); n = -n; } if(n > 9) write(n / 10); putchar(n % 10 + '0');inline void write(__int128_t n){ // 有符号 __int128 if(n < 0){ putchar('-'); n = -n; } if(n > 9) write(n / 10); putchar(n % 10 + '0');}inline void write(__uint128_t n){ // 无符号__int128 if(n > 9) write(n / 10); putchar(n % 10 + '0');inline void write(__uint128_t n){ // 无符号 __int128 if(n > 9) write(n / 10); putchar(n % 10 + '0');}```需要注意的是,`__int128`类型是`GCC`扩展,在洛谷和NOI系列比赛中可用,但**不保证**部分OJ或环境(如`MSVC`)可用。对于C++原生类型,建议使用以下函数:需要注意的是,`__int128` 类型是 `GCC` 扩展,在洛谷和 NOI 系列比赛中可用,但**不保证**部分 OJ 或环境(如 `MSVC`)可用。:::
:::info[3.2.2 快速输出整型]```cpp line-numbers#include <type_traits>template<typename T>inline void write(T x) { // 判断是否是无符号类型 constexpr bool is_uns = std::is_unsigned_v<T>; if constexpr (!is_uns) { if (x < 0) { putchar('-'); // 转无符号避免负数取模溢出 x = -static_cast<std::make_unsigned_t<T>>(x); } } if (x > 9) write(x / 10); putchar(x % 10 + '0'); // 判断是否是无符号类型 constexpr bool is_uns = std::is_unsigned<T>::value; if (!is_uns) { if (x < 0) { putchar('-'); // 转无符号避免负数取模溢出 x = -static_cast<typename std::make_unsigned<T>::type>(x); } } if (x > 9) write(x / 10); putchar(x % 10 + '0');}```迭代版防栈溢出:```cpp line-numbers#include <type_traits>#include <algorithm> // swap
template<typename T>inline void write(T x) { constexpr bool uns = std::is_unsigned_v<T>; if constexpr (!uns) { if (x < 0) { out_buf[out_p++] = '-'; x = -static_cast<std::make_unsigned_t<T>>(x); } } int top = out_p; do { out_buf[out_p++] = (x % 10) + '0'; x /= 10; } while (x); // 反转数字 for (int l = top, r = out_p - 1; l < r; l++, r--) swap(out_buf[l], out_buf[r]); constexpr bool uns = std::is_unsigned<T>::value; // 临时存数字字符,足够容纳64位整数最多20位 char tmp[32]; int top = 0;
// 负数符号处理 if (!uns) { if (x < 0) { putchar('-'); x = -static_cast<typename std::make_unsigned<T>::type>(x); } }
// 逆序拆分数字存入tmp do { tmp[top++] = static_cast<char>((x % 10) + '0'); x /= 10; } while (x);
// 逆序输出,还原正确数字顺序 for (int i = top - 1; i >= 0; --i) { putchar(tmp[i]); }}// 批量刷新缓冲区inline void flush() { fwrite(out_buf, 1, out_p, stdout); out_p = 0;}```:::
:::info[3.2.3 快速输出字符串]```暂无模板代码```::::::::::::
---
::::success[数据结构模板]:::::success[4. 高精度计算模板]---:::warning[注意]{open}不要用STL!:::info[4.1 Python 高精度]`Python` 在逻辑前加上这两句就可以了:```pythonimport syssys.set_int_max_str_digits(0)# 其实就是解除字符串长度限制# 因为 input() 读进来是字符串,而 Python 原生支持大整数# 所以只需要保证读进来的字符串不被截断就可以了```~~555为什么这么少,不像其他语言还要字符串/数组~~:::
:::info[[B3614 【模板】栈](https://www.luogu.com.cn/problemnew/show/B3614)][AC记录](https://www.luogu.com.cn/record/254020653) C++类代码:```cpp line-numberstemplate<typename _Tp=int>class mystack{private: _Tp *buf; unsigned long long ptr=0, ms;::::info[4.2 C++高精度]也可以直接用我的 [`bint` 类](https://github.com/fruit32820/Content-Delivery-Network/blob/main/luogu/header/classes/structures/bint.hpp)
public: mystack(long long size) : ms(size) { buf = new _Tp[ms]; } ~mystack() { delete[] buf; }:::info[4.2.1 字符串存储高精度数字]```暂无模板代码```:::
bool empty() const { return !ptr; } bool full() const { return ptr == ms; }:::info[4.2.2 整型数组存储高精度数字]```暂无模板代码```::::::::::::
int push(const _Tp& x) { if(full()) return -1; buf[ptr++] = x; return 0; }---
int pop() { if(empty()) return -1; ptr--; return 0; }:::::success[5. 字符串算法模板]---::::info[5.1 模式串匹配]:::info[5.1.1 KMP 单模式串匹配]```暂无模板代码```:::
_Tp top() const { return empty() ? _Tp(-1) : buf[ptr-1]; }:::info[5.1.2 AC自动机 多模式串匹配]```暂无模板代码```::::::::::::
long long size() const { return ptr; } void clear() { ptr = 0; }---
mystack(const mystack&) = delete; mystack& operator=(const mystack&) = delete;};:::::success[6. 数论模板]---::::info[6.1 快速幂]:::info[6.1.1 普通取模快速幂]```cpp line-numberslong long qpow(long long a,long long b,long long mod=0) { long long ans=1; if(mod) a%=mod; while(b){ if(b&1) ans=(__int128)ans*a; if(mod) ans%=mod; a=(__int128)a*a; if(mod) a%=mod; b>>=1; } return ans;}```实践:> - [P1449 后缀表达式](https://www.luogu.com.cn/problemnew/show/P1449)特别的,如果传入的 $mod=0$ 或者没有传入 $mod$,那么将不会取模。::::::info[6.1.2 浮点快速幂]```long double qpow(long double a,long long b) { long double ans=1; while(b){ if(b&1) ans=ans*a; a=a*a; b>>=1; } return ans;}```:::::::
::::info[6.2 GCD/欧几里得算法]:::info[6.2.1 朴素GCD]```暂无模板代码```:::
:::info[[B3616 【模板】队列](https://www.luogu.com.cn/problemnew/show/B3616)][AC记录](https://www.luogu.com.cn/record/254019948) C++类代码:```cpp line-numberstemplate<typename _Tp=int>class myqueue{private: _Tp *q; unsigned long long f=0, r=0, ms;:::info[6.2.2 exGCD/扩展欧几里得算法]```暂无模板代码```:::
public: myqueue(long long size) : ms(size+1) { q = new _Tp[ms]; } ~myqueue() { delete[] q; }:::info[6.2.3 乘法逆元]```暂无模板代码```:::::::
bool empty() const { return f == r; } bool full() const { return (r+1)%ms == f; }::::info[6.3 素数筛]:::info[6.3.1 普通筛法]```暂无模板代码```:::
int push(const _Tp& x) { if(full()) return -1; q[r] = x; r = (r+1)%ms; return 0; }:::info[6.3.2 埃氏筛/埃拉托色尼筛法/Eratosthenes Sieve]```暂无模板代码```但我们通常可能会这样写:```暂无模板代码```上述代码复杂度为 $O(n \log \log n)$。 但代码中,还有一处可以优化:
int pop() { if(empty()) return -1; f = (f+1)%ms; return 0; }用 $i$ 去筛掉 $2i, 3i, 4i\dots$ 时, 我们发现:- $2i \le n$ 的话,其一定被 $2$ 给筛掉了。- 而 $3i$ 也一定被 $3$ 筛掉了。 所以我们不需要从 $2i$ 开始筛,直接从 $i^2$ 开始筛即可(必须满足 $i^2 \le n$),于是可以优化为开头的代码:::
_Tp front() const { return empty() ? _Tp(-1) : q[f]; }:::info[6.3.3 线性筛/欧拉筛]```暂无模板代码```:::
long long size() const { return (r - f + ms) % ms; }:::info[6.3.4 分块筛]```暂无模板代码```:::::::
void clear() { f = r = 0; }::::info[6.4 CRT/中国剩余定理]:::info[6.4.1 朴素CRT]```暂无模板代码```:::
myqueue(const myqueue&) = delete; myqueue& operator=(const myqueue&) = delete;};:::info[6.4.2 exCRT/扩展中国剩余定理]```暂无模板代码```::::::::::::---
:::info[[P3378 【模板】堆](https://www.luogu.com.cn/problemnew/show/P3378)][AC记录](https://www.luogu.com.cn/record/260394149) C++类代码:::::success[7. DP 模板]:::info[7.1 数位 DP]```cpp line-numbers#include <stdexcept>#include <algorithm>#include<bits/stdc++.h>using namespace std;using ll=long long;const int MAXDIG=20,MAXST=25,MAXPRE=20;int dig[MAXDIG];// dp[pos] [state][pre]ll dp[MAXDIG][MAXST][MAXPRE];ll dfs(int pos,int st,int pre,bool lim,bool lead0){ // 边界 if(pos==0) return 1; // 使用记忆化结果 if(!lim && !lead0 && dp[pos][st][pre]!=-1) return dp[pos][st][pre]; ll res=0; int maxlim=lim?dig[pos]:9; // 当前位限制 for(int i=0;i<=maxlim;i++){ bool nlim=lim&&(i==maxlim); bool nlead0=lead0&&!i; int npos=pos-1; int nst=st/* 根据题意修改 */; int npre=nlead0?-1:i; if(i==4 || (pre==6 && i==2)) continue; res+=dfs(npos,nst,npre,nlim,nlead0); } // 加入记忆化 if(!lim && !lead0) dp[pos][st][pre]=res; return res;}ll calc(ll x){ // 拆分 x int len=0; while(x){ dig[++len]=x%10; x/=10; } return dfs(len,0,-1,true,true);}int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); memset(dp,-1,sizeof(dp)); ll l,r; cin>>l>>r; cout<<calc(r)-calc(l-1)<<'\n';}```::::---
template<typename T=int, typename Compare=std::greater<T>>class myheap{private: unsigned long long ms; T *p; unsigned long long len; Compare comp;::::success[8. 其他模板]:::info[8.1 算法竞赛极限卡常(保留浮点精度)]```cpp line-numbers#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,inline")#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,f16c,tune=native")```:::
bool valid(unsigned long long pos) const { return pos >= 1 && pos <= len; }:::info[8.2 极速数值计算专用]```cpp line-numbers#pragma GCC optimize("Ofast,no-stack-protector,fast-math,unroll-loops,inline")#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,f16c,tune=native")```:::
// 二分查找插入位置 unsigned long long findInsertPos(const T& val) const { if(len == 0) return 1; unsigned long long left = 1, right = len; while(left <= right) { unsigned long long mid = left + (right - left) / 2; if(!comp(p[mid], val) && (mid == 1 || comp(p[mid-1], val))) { return mid; } else if(comp(p[mid], val)) { left = mid + 1; } else { if(mid == 1) return 1; right = mid - 1; } } return left; }:::warning{open}洛谷通过 [修改 GCC](https://github.com/luogu-dev/judge-env/blob/master/gcc/13_disable-pragma-and-attribute-for-optimize.patch) 禁用了 `#pragma` 和 `__attribute__` 打开优化,所以如有这两个的代码会直接 `CE`。:::
// 二分查找元素位置 unsigned long long findElementPos(const T& val) const { unsigned long long left = 1, right = len; while(left <= right) { unsigned long long mid = left + (right - left) / 2; if(p[mid] == val) { // 找到最左边的相等元素 while(mid > 1 && p[mid-1] == val) mid--; return mid; } else if(comp(val, p[mid])) { left = mid + 1; } else { if(mid == 1) return 0; // 不存在 right = mid - 1; } } return 0; // 不存在 }
public: myheap(unsigned long long size_max) : ms(size_max), len(0) { if(!size_max) throw std::invalid_argument("Max size can not be 0."); p = new T[ms + 1]; }
~myheap() { delete[] p; }
myheap(const myheap&) = delete; myheap& operator=(const myheap&) = delete;
T get(unsigned long long pos) const { if(!valid(pos)) throw std::out_of_range("Access location does not exist."); return p[pos]; }
void modify(unsigned long long pos, const T& x) { if(!valid(pos)) throw std::out_of_range("Modify location does not exist."); erase(pos); push(x); }
void erase(unsigned long long pos) { if(!valid(pos)) throw std::out_of_range("Delete location does not exist."); // 将后面元素前移 for(unsigned long long i = pos; i < len; ++i) { p[i] = p[i+1]; } len--; }
void push(const T& val) { if(len >= ms) throw std::overflow_error("Heap is full, cannot insert."); // 找到插入位置并移动元素 unsigned long long insertPos = findInsertPos(val); for(unsigned long long i = len; i >= insertPos; --i) { p[i+1] = p[i]; } p[insertPos] = val; len++; }
T top() const { if(!len) throw std::underflow_error("Heap is empty, no top."); return p[1]; } void pop() { if(!len) throw std::underflow_error("Heap is empty, no top."); // 第一个元素后移 for(unsigned long long i = 1; i < len; ++i) { p[i] = p[i+1]; } len--; } bool empty() const { return len == 0; } bool full() const { return len >= ms; } unsigned long long size() const { return len; } void clear() { len = 0; } // 验证数组是否有序 bool isOrdered() const { for(unsigned long long i = 1; i < len; ++i) { if(comp(p[i+1], p[i])) return false; } return true; }};:::info[8.3 体积缩小编译指令]```练习:- [P1177 【模板】排序](https://www.luogu.com.cn/problem/P1177)-Wl,--gc-sections -Wl,--strip-all -Wl,--discard-all -Wl,-e,WinMainCRTStartup -fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables -fexec-charset=gbk```::::::info[[U643423 【模板】动态大小数组](https://www.luogu.com.cn/problemnew/show/U643423)]由于 [U645395 【模板】动态大小数组(弱化版)](https://www.luogu.com.cn/problemnew/show/U645395)是本题弱化版,所以本题类代码和该题通用。
C++类代码:```cpp:::info[8.4 文件初始缺省源]```cpp line-numbers// C++ std#include<bits/stdc++.h>using namespace std;// GNU pb_ds#include<bits/extc++.h>using namespace __gnu_cxx;using namespace __gnu_pbds;int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); }```:::> - [B4324 【模板】双向链表](https://www.luogu.com.cn/problemnew/show/B4324)::::
---
:::warning[AI 声明]{open}本文章初次写作完成后使用 DeepSeek(实则豆包) 进行文字润色与格式规范修正,在 AI 完成后**人工**进行了格式修复。:::