版本比较
个人代码基础模板
正文
增加 937 行删除 13 行
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364262 行未改动全部展开327328329330331332333334335451 行未改动全部展开7877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838841,221 行未改动全部展开210621072108210921102111196119622112211321142115196621162117211821192120132 行未改动全部展开2253225422552256210721082109211022572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489211324902491211524922493211724942495211924962497212124982499212325002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326148 行未改动262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069# Aurelia_Veil 好菜呀
## [欧拉回路](https://www.luogu.com.cn/problem/P7771)
```cpp#include<bits/stdc++.h>using namespace std;const int N=2e5+111;int n,m;vector<int>g[N];stack<int>ans;int in[N],out[N];int thi[N];void FC(int u){ for(int i=thi[u];i<g[u].size();i=thi[u]){ int v=g[u][i]; thi[u]=i+1; FC(v); } ans.push(u);}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ int x,y; scanf("%d%d",&x,&y); in[y]++; out[x]++; g[x].push_back(y); } int cntin=0,cntout=0,top=1; for(int i=1;i<=n;i++){ if(abs(in[i]-out[i])>=2){ printf("No"); return 0; } if(in[i]==out[i]+1){ cntin++; } if(in[i]+1==out[i]){ top=i; cntout++; } } if(cntin>=2||cntout>=2){ printf("No"); return 0; } for(int i=1;i<=n;i++){ sort(g[i].begin(),g[i].end()); } FC(top); while(!ans.empty()){ printf("%d ",ans.top()); ans.pop(); } return 0;}```
## 快读快写
```cpp#include<bits/stdc++.h>262 行未改动全部展开 return 0;}```
### 树剖法
```cpp#include<bits/stdc++.h>451 行未改动全部展开 return 0;} ```
## [可持久化线段树2](https://www.luogu.com.cn/problem/AT_abc453_g):
```#include<bits/stdc++.h>using namespace std;const int N=5e6+111;int n,m;int a[N],aa[N];int top=0;struct tree{ struct cqbzhf520{ int l,r,u; long long x; }tr[N]; int tot=0,ver[N]; void merge(int u){ tr[u].x=tr[tr[u].l].x+tr[tr[u].r].x; } int add(int u){ tr[++tot]=tr[u]; return tot; } int build(int u,int l,int r){ u=++tot; if(l==r){ return u; } int mid=(l+r)/2; tr[u].l=build(tr[u].l,l,mid); tr[u].r=build(tr[u].r,mid+1,r); return u; } int adtree(int u,int l,int r,int x,int y){ u=add(u); if(l==r){ tr[u].x=y; return u; } int mid=(l+r)/2; if(x<=mid){ tr[u].l=adtree(tr[u].l,l,mid,x,y); }else{ tr[u].r=adtree(tr[u].r,mid+1,r,x,y); } merge(u); return u; } long long qry(int u,int l,int r,int cl,int cr){ if(u==0){ return 0; } if(cr<l||cl>r){ return 0; } if(l<=cl&&cr<=r){ return tr[u].x; } int mid=(cl+cr)/2; return qry(tr[u].l,l,r,cl,mid)+qry(tr[u].r,l,r,mid+1,cr); }}tr;int tp[N];int main(){ int k; scanf("%d%d%d",&n,&m,&k); tp[0]=tr.build(1,1,m); for(int i=1;i<=n;i++){ tp[i]=tp[0]; } while(k--){ int op; scanf("%d",&op); if(op==1){ int x,y; scanf("%d%d",&x,&y); tp[x]=tp[y]; }else if(op==2){ int x,y,z; scanf("%d%d%d",&x,&y,&z); tp[x]=tr.adtree(tp[x],1,m,y,z); }else{ int x,y,z; scanf("%d%d%d",&x,&y,&z); printf("%lld\n",tr.qry(tp[x],y,z,1,m)); } } return 0;}```
## [平衡树(权值线段树模拟)](https://www.luogu.com.cn/problem/P3369)
```cpp#include<bits/stdc++.h>1,221 行未改动全部展开 } return 0;}```## [treap/splay/FHQ-treap大合集]((https://www.luogu.com.cn/problem/P3369))
## [Splay平衡树](https://www.luogu.com.cn/problem/P3369)
```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=4e5+111;const int N=2e5+111;int n;struct Splay{ #define ll s[0] #define rr s[1]132 行未改动全部展开 add(0x3f3f3f3f); } #undef ll #undef rr}t;int main(){ t._init(); scanf("%d",&n);};struct treap{ #define ll s[0] #define rr s[1] int root=0,tot=0; struct tree{ int s[3]; int thi,siz,t,pri; }tr[N]; int init(int x){ tr[tot].ll=tr[++tot].rr=0; tr[tot].thi=x; tr[tot].siz=tr[tot].t=1; tr[tot].pri=rand(); return tot; } void merge(int u){ if(u==0){ return; } tr[u].siz=tr[tr[u].ll].siz+tr[tr[u].rr].siz+tr[u].t; } int zag(int u){ int v=tr[u].rr; tr[u].rr=tr[v].ll; tr[v].ll=u; merge(u); merge(v); return v; } int zig(int u){ int v=tr[u].ll; tr[u].ll=tr[v].rr; tr[v].rr=u; merge(u); merge(v); return v; } int add(int u,int x){ if(!u){ return init(x); } if(tr[u].thi==x){ tr[u].t++; }else if(x<tr[u].thi){ tr[u].ll=add(tr[u].ll,x); if(tr[tr[u].ll].pri>tr[u].pri){ u=zig(u); } }else if(x>tr[u].thi){ tr[u].rr=add(tr[u].rr,x); if(tr[tr[u].rr].pri>tr[u].pri){ u=zag(u); } } merge(u); return u; } int del(int u,int x){ if(!u){ return 0; } if(tr[u].thi==x){ if(tr[u].t>1){ tr[u].t--; }else{ if(!tr[u].ll&&!tr[u].rr){ return 0; } if(!tr[u].ll){ u=tr[u].rr; }else if(!tr[u].rr){ u=tr[u].ll; }else{ if(tr[tr[u].ll].pri>tr[tr[u].rr].pri){ u=zig(u); tr[u].rr=del(tr[u].rr,x); }else{ u=zag(u); tr[u].ll=del(tr[u].ll,x); } } } }else if(x<tr[u].thi){ tr[u].ll=del(tr[u].ll,x); }else{ tr[u].rr=del(tr[u].rr,x); } merge(u); return u; } int rank(int u,int x){ if(!u){ return 1; } if(tr[u].thi==x){ return tr[tr[u].ll].siz+1; }else if(x<tr[u].thi){ return rank(tr[u].ll,x); }else{ return rank(tr[u].rr,x)+tr[tr[u].ll].siz+tr[u].t; } } int rankth(int u,int k){ if(!u){ return -0x3f3f3f3f3f3f3f3f; } if(k<=tr[tr[u].ll].siz){ return rankth(tr[u].ll,k); }else if(k<=tr[tr[u].ll].siz+tr[u].t){ return tr[u].thi; }else{ return rankth(tr[u].rr,k-(tr[tr[u].ll].siz+tr[u].t)); } } int prext(int u,int x,bool f){ if(!u){ return f?0x3f3f3f3f3f3f3f3f:-0x3f3f3f3f3f3f3f3f; } if(f==0){ if(x<=tr[u].thi){ return prext(tr[u].ll,x,f); }else{ return max(prext(tr[u].rr,x,f),tr[u].thi); } }else{ if(x>=tr[u].thi){ return prext(tr[u].rr,x,f); }else{ return min(prext(tr[u].ll,x,f),tr[u].thi); } } } void _init(){ srand(time(0)); root=add(root,-0x3f3f3f3f3f3f3f3f); root=add(root,0x3f3f3f3f3f3f3f3f); } #undef ll #undef rr};struct fhq_treap{ #define ll s[0] #define rr s[1] int root=0,tot=0; struct tree{ int s[3]; int thi,siz,t,pri; }tr[N]; int init(int x){ tot++; tr[tot].ll=tr[tot].rr=0; tr[tot].thi=x; tr[tot].siz=tr[tot].t=1; tr[tot].pri=rand(); return tot; } void merge(int u){ if(u==0){ return; } tr[u].siz=tr[tr[u].ll].siz+tr[tr[u].rr].siz+tr[u].t; } pair<int,int> split(int u,int k){ if(!u){ return {0,0}; } if(tr[u].thi<k){ pair<int,int> v=split(tr[u].rr,k); tr[u].rr=v.first; merge(u); return {u,v.second}; }else{ pair<int,int> v=split(tr[u].ll,k); tr[u].ll=v.second; merge(u); return {v.first,u}; } } int make(int u,int v){ if(!u||!v){ return max(u,v); } if(tr[u].pri<tr[v].pri){ tr[u].rr=make(tr[u].rr,v); merge(u); return u; }else{ tr[v].ll=make(u,tr[v].ll); merge(v); return v; } } void add(int x){ int u=init(x); pair<int,int> v=split(root,x); root=make(make(v.first,u),v.second); } void del(int x){ pair<int,int> a; a=split(root,x); pair<int,int> b; b=split(a.second,x+1); b.first=make(tr[b.first].ll,tr[b.first].rr); root=make(a.first,make(b.first,b.second)); } int rank(int u){ pair<int,int> v=split(root,u); int k=tr[v.first].siz+1; root=make(v.first,v.second); return k; } int rankth(int u,int x){ if(x<=tr[tr[u].ll].siz){ return rankth(tr[u].ll,x); }else if(x==tr[tr[u].ll].siz+1){ return tr[u].thi; }else{ return rankth(tr[u].rr,x-(tr[tr[u].ll].siz+1)); } } int prext(int x,bool f){ if(!f){ return rankth(root,rank(x)-1); }else{ return rankth(root,rank(x+1)); } }}t3;signed main(){ scanf("%lld",&n); while(n--){ int op,x; scanf("%d%d",&op,&x); scanf("%lld%lld",&op,&x); if(op==1){ t.add(x); t3.add(x); }else if(op==2){ t.del(x); t3.del(x); }else if(op==3){ printf("%d\n",t.rank(x)); printf("%lld\n",t3.rank(x)); }else if(op==4){ printf("%d\n",t.rankth(x)); printf("%lld\n",t3.rankth(t3.root,x)); }else if(op==5||op==6){ printf("%d\n",t.tr[t.prext(x,op-5)].thi); printf("%lld\n",t3.prext(x,op-5)); } } return 0;}
```
## [斜率优化(附带公式)](https://www.luogu.com.cn/problem/P3195)
```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=5e4+111;int a[N];int t[N],t2[N];int dp[N];int n,m;int putx(int x){ return 2*t2[x];}int puty(int x){ return dp[x]+t2[x]*t2[x]+2*t2[x]*(m+1);}int mol(int a,int b){ return puty(a)-puty(b);}int den(int a,int b){ return putx(a)-putx(b);}int q[N],l,r;signed main(){ scanf("%lld%lld",&n,&m); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); t[i]=t[i-1]+a[i]; t2[i]=t[i]+i; } l=1,r=1; q[l]=0; for(int i=1;i<=n;i++){ while(r>l&&mol(q[l+1],q[l])<=t2[i]*den(q[l+1],q[l])){ l++; } dp[i]=dp[q[l]]+(t2[i]-t2[q[l]]-m-1)*(t2[i]-t2[q[l]]-m-1); while(r>l&&mol(q[r],q[r-1])*den(i,q[r])>=mol(i,q[r])*den(q[r],q[r-1])){ r--; } q[++r]=i; } printf("%lld\n",dp[n]); return 0;}/*dp[i]=dp[j]+(i-(j+1)+t[i]-t[j]-l)^2dp[i]=dp[j]+(i-j-1+t[i]-t[j]-l)^2dp[i]=dp[j]+(i-j-1+t[i]-t[j]-l)^2t2[i]=t[i]+i;dp[i]=dp[j]+(t2[i]-t2[j]-l-1)^2;A=l+1dp[i]=dp[j]+(t2[i]-t2[j])^2-2*t2[i]*A+2*t2[j]*A+A^2dp[i]=dp[j]+t2[i]^2-2*t2[i]*t2[j]+t2[j]^2-2*t2[i]*A+2*t2[j]*A+A^2dp[j]+t2[j]^2=dp[i]+(t2[i]+A)*(2*t2[j])+2*t2[i]*A-A^2y=dp[j]+t2[j]^2+2*t2[j]*Ak=t2[i];x=2*t2[j]b=2*t2[i]*A-A^2+dp[i]-----*/```
## [四边形不等式优化](https://www.luogu.com.cn/problem/UVA10304):
```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=271;int a[N];int dp[N][N];int t[N];int lr[N][N];int n;signed main(){ while(~scanf("%lld",&n)){ fill(dp[0],dp[0]+N*N,0x3f3f3f3f3f3f3f3f); fill(lr[0],lr[0]+N*N,0); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); t[i]=t[i-1]+a[i]; dp[i][i]=0; lr[i][i]=i; } for(int len=2;len<=n;len++){ for(int l=1;l+len-1<=n;l++){ int r=l+len-1; for(int k=lr[l][r-1];k<=lr[l+1][r];k++){ if(dp[l][r]>(k==l?0:dp[l][k-1])+(k==r?0:dp[k+1][r])-a[k]+t[r]-t[l-1]){ dp[l][r]=(k==l?0:dp[l][k-1])+(k==r?0:dp[k+1][r])-a[k]+t[r]-t[l-1]; lr[l][r]=k; } } } } printf("%lld\n",dp[1][n]); } return 0;}```
## 神奇小哈希
```cppstruct custom_hash{8 行未改动 return splitmix64(x+FIXED_RANDOM); }};```
## [高斯消元](https://www.luogu.com.cn/problem/P3389):
```cpp#include<bits/stdc++.h>using namespace std;const int N=311;const double minxp=1e-9;int n;double a[N][N];bool fxbit(){ int top=1; for(int i=1;i<=n+1;i++){ int tp=top; while(tp<=n&&fabs(a[tp][i])<=minxp){ tp++; } if(tp>n){ continue; } if(tp!=top){ for(int j=1;j<=n+1;j++){ swap(a[tp][j],a[top][j]); } } double k=a[top][i]; for(int j=1;j<=n+1;j++){ a[top][j]/=k; } for(int j=1;j<=n;j++){ if(j==top){ continue; } double p=a[j][i]; for(int k=1;k<=n+1;k++){ a[j][k]-=p*a[top][k]; } } top++; } return top==n+1;}int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=1;j<=n+1;j++){ scanf("%lf",&a[i][j]); } } if(!fxbit()){ printf("No Solution"); return 0; } for(int i=1;i<=n;i++){ printf("%.2lf\n",a[i][n+1]); } return 0;} ```
## [矩阵快速幂](https://www.luogu.com.cn/problem/P1939):
```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=111;const int mod=1e9+7;struct matrix{ int n,m; int a[N][N]; void init(int x,int y,int k){ n=x,m=y; fill(a[0],a[0]+N*N,k); } void read(bool k=1,int x=0,int y=0){ if(k==1){ scanf("%lld%lld",&x,&y); } init(x,y,0); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%lld",&a[i][j]); a[i][j]%=mod; } } } void write(bool k=0){ if(k==1){ printf("%lld %lld\n",n,m); } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ printf("%lld ",a[i][j]); } printf("\n"); } }};matrix operator +(matrix &x,matrix &y){ matrix z; z.init(x.n,x.m,0); for(int i=1;i<=x.n;i++){ for(int j=1;j<=x.m;j++){ (z.a[i][j]=x.a[i][j]+y.a[i][j])%=mod; } } return z;}matrix operator *(matrix &x,matrix &y){ matrix z; z.init(x.n,y.m,0); int kl=x.m; for(int i=1;i<=z.n;i++){ for(int j=1;j<=z.m;j++){ for(int k=1;k<=kl;k++){ (z.a[i][j]+=x.a[i][k]*y.a[k][j]%mod)%=mod; } } } return z;}matrix operator ^(matrix &x,int k){ matrix ans; matrix base=x; ans.init(x.n,x.n,0); for(int i=1;i<=ans.n;i++){ ans.a[i][i]=1; } while(k){ if(k&1){ ans=ans*base; } base=base*base; k>>=1; } return ans;}matrix a,b;signed main(){ int T; scanf("%lld",&T); while(T--){ int n; scanf("%lld",&n); a.init(1,3,1); if(n<=3){ printf("%lld\n",a.a[1][n]); continue; } b.init(3,3,0); b.a[1][1]=0; b.a[1][2]=0; b.a[1][3]=1; b.a[2][1]=1; b.a[2][2]=0; b.a[2][3]=0; b.a[3][1]=0; b.a[3][2]=1; b.a[3][3]=1; matrix k=b^(n-3); a=a*k; printf("%lld\n",a.a[1][3]); } return 0;} /*operator*/```
## [扩展中国剩余定理(EXCRT)](https://www.luogu.com.cn/problem/P4777)
```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=1e5+111;__int128 qpow(__int128 a,__int128 b,__int128 c){ __int128 ans=0; while(b){ if(b&1) ans+=a,ans%=c; b>>=1,a<<=1,a%=c; } return ans;}__int128 gcd(__int128 a,__int128 b,__int128 &x,__int128 &y){ if(b==0){ x=1; y=0; return a; } __int128 k=gcd(b,a%b,y,x); y-=a/b*x; return k;}int a[N],b[N];int n;int excrt(){ __int128 m=b[1],ans=a[1]; __int128 x,y; for(__int128 i=2;i<=n;i++){ __int128 ai=m,bi=b[i],c=(a[i]-ans%bi+bi)%bi; __int128 kkk=gcd(ai,bi,x,y),bg=bi/kkk; if(c%kkk!=0){ return -1; } x=((x*c/kkk)%bg+bg)%bg; ans+=x*m; m*=bg; ans=(ans%m+m)%m; } return (ans+m)%m;}signed main(){ int sum=1; bool v=0;scanf("%lld",&n); for(__int128 i=1;i<=n;i++){ scanf("%lld%lld",&b[i],&a[i]); if(a[i]!=0){ v=1; } __int128 x,y; sum=sum*b[i]/gcd(sum,b[i],x,y); } if(v==0){ printf("%lld",sum); return 0; } printf("%lld",excrt()); return 0;}```
## 多矩形小工具:
```cpp#include <iostream>#include <vector>#include <string>#include <algorithm>#include <cmath>
using namespace std;
// 矩形结构体struct Rect { int x1, y1, x2, y2;};
int main() { // 给定的矩形数据 vector<Rect> rects = { {-15, 0, 5, 10}, {-5, 8, 20, 25}, {15, -4, 24, 14}, {0, -6, 16, 4}, {2, 15, 10, 22}, {30, 10, 36, 20}, {34, 0, 40, 16} };
// 1. 计算画布边界(自动扩展 1 个单位以便显示坐标轴) int min_x = rects[0].x1, max_x = rects[0].x2; int min_y = rects[0].y1, max_y = rects[0].y2; for (const auto& r : rects) { min_x = min(min_x, min(r.x1, r.x2)); max_x = max(max_x, max(r.x1, r.x2)); min_y = min(min_y, min(r.y1, r.y2)); max_y = max(max_y, max(r.y1, r.y2)); } // 扩展一点边距,让坐标轴有空间显示刻度 min_x -= 2; max_x += 2; min_y -= 2; max_y += 2;
int width = max_x - min_x + 1; int height = max_y - min_y + 1;
// 创建画布,初始为空格 vector<string> grid(height, string(width, ' '));
// 设置字符的辅助函数 (x,y) 为逻辑坐标 auto setChar = [&](int x, int y, char ch) { int row = max_y - y; int col = x - min_x; if (row >= 0 && row < height && col >= 0 && col < width) grid[row][col] = ch; };
// 2. 绘制矩形边框 for (const auto& r : rects) { int x1 = r.x1, y1 = r.y1, x2 = r.x2, y2 = r.y2; // 水平边(底边和顶边) for (int x = x1; x <= x2; ++x) { setChar(x, y1, '#'); setChar(x, y2, '#'); } // 垂直边(左边和右边) for (int y = y1; y <= y2; ++y) { setChar(x1, y, '#'); setChar(x2, y, '#'); } }
// 3. 绘制坐标轴(先绘制轴,再覆盖原点为'+') // x轴 (y=0) if (min_y <= 0 && 0 <= max_y) { for (int x = min_x; x <= max_x; ++x) { if (grid[max_y - 0][x - min_x] == ' ') // 不覆盖已有图形 setChar(x, 0, '-'); } } // y轴 (x=0) if (min_x <= 0 && 0 <= max_x) { for (int y = min_y; y <= max_y; ++y) { if (grid[max_y - y][0 - min_x] == ' ') setChar(0, y, '|'); } } // 原点 (强制为'+') setChar(0, 0, '+');
// 4. 标注刻度(每5个单位) // 在 x 轴上方 (y=1) 写数字,在 y 轴右侧 (x=1) 写数字 auto labelNumber = [&](int x, int y, int val) { string s = to_string(val); for (size_t i = 0; i < s.size(); ++i) setChar(x + (int)i, y, s[i]); };
// x轴刻度(y=1) for (int x = min_x; x <= max_x; ++x) { if (x % 5 == 0 && x != 0) { if (min_y <= 1 && 1 <= max_y) labelNumber(x, 1, x); } } // y轴刻度(x=1) for (int y = min_y; y <= max_y; ++y) { if (y % 5 == 0 && y != 0) { if (min_x <= 1 && 1 <= max_x) labelNumber(1, y, y); // 从x=1开始向右写,但数字可能会重叠,简单起见直接写 } }
// 额外确保原点为 '+' setChar(0, 0, '+');
// 5. 输出画布 cout << "\n字符画输出(y 轴向上,x 轴向右):\n\n";
// 在顶部打印 x 轴刻度参考(个位数字) cout << " "; for (int x = min_x; x <= max_x; ++x) { int digit = abs(x) % 10; if (x == 0) cout << '0'; else if (x % 5 == 0) cout << digit; else cout << ' '; } cout << "\n "; for (int x = min_x; x <= max_x; ++x) cout << '-'; cout << '\n';
// 逐行打印 for (int row = 0; row < height; ++row) { int y_val = max_y - row; // 左侧显示 y 值(每隔5单位显示) if (y_val % 5 == 0) printf("%3d|", y_val); else cout << " |"; cout << grid[row] << '\n'; }
// 底部 x 轴刻度(仅显示每隔5单位的数字,但可能较拥挤,此处简化) cout << " "; for (int x = min_x; x <= max_x; ++x) { if (x % 5 == 0) { int val = abs(x); if (val >= 100) cout << (val / 100) % 10; else if (val >= 10) cout << (val / 10) % 10; else cout << val % 10; } else { cout << ' '; } } cout << "\n\n坐标范围:x [" << min_x << ", " << max_x << "], y [" << min_y << ", " << max_y << "]\n"; cout << "矩形边框用 '#' 表示,坐标轴用 '-' 和 '|',原点为 '+'\n";
return 0;}```
## 双hash:
```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=2111;const int mod=1e9+7;const int base=1331;const int mod2=998244353;const int base2=13331;int t[N],t2[N];vector<pair<int,int>>tt;int baser[N],baser2[N];void _init(){ baser[0]=baser2[0]=1; for(int i=1;i<=N-11;i++){ baser[i]=baser[i-1]*base%mod; baser2[i]=baser2[i-1]*base2%mod2; }}void init(string s,int n,int t[],int t2[]){ for(int i=1;i<=n;i++){ t[i]=(t[i-1]*base%mod+s[i])%mod; t2[i]=(t2[i-1]*base2%mod2+s[i])%mod2; } }pair<int,int> getcor(int t[],int t2[],int l,int r){ return {(t[r]-t[l-1]*baser[r-l+1]%mod+mod)%mod,(t2[r]-t2[l-1]*baser2[r-l+1]%mod2+mod2)%mod2};}signed main(){ string s; cin>>s; int n=s.length(); s=" "+s; _init(); init(s,n,t,t2); for(int len=1;len<=n;len++){ for(int l=1;l+len-1<=n;l++){ int r=l+len-1; tt.push_back(getcor(t,t2,l,r)); } } sort(tt.begin(),tt.end()); printf("%lld",unique(tt.begin(),tt.end())-tt.begin()); return 0;}```