版本比较
个人代码基础模板
正文
增加 2695 行删除 124 行
112345675687899111213141516171819202122232410111213141516171819262728293031323334353637383940412021222324252627282930313233343536434445464737383940414243444950454647485253545556574950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862875960616228828964656667686970717229029129229329429529629729874757677787980818283299300301302858630330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939088899091929394959697989910010110210310410510610710810911011111211311411539139239339439539639739839940040140240340440540640740840941041141241341441512712812941641741841942042142242342478 行未改动503504505506218507508509510511223512513226227228229230231514515516517518233234235236237519520521522523524525526527528239240529530531532533534535245536537538539540541542543253254544545546547548258549550551552553232 行未改动全部展开786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069## [欧拉路径](https://www.luogu.com.cn/problem/P7771):# Aurelia_Veil 好菜呀
## [欧拉回路](https://www.luogu.com.cn/problem/P7771)
```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=1e6;const int N=2e5+111;struct pai{ long long sum,maxx,tag;};int n,m;struct tree{ pai t[N]; int _merge(int a,int b,int op){ if(op==1){ return a+b; } if(op==2){ return max(a,b); } return 0; } pai merge(pai a,pai b){ pai ret{_merge(a.sum,b.sum,1),_merge(a.maxx,b.maxx,2),0}; return ret;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); } // void push_down(int k,int cl,int cr){ // if(t[k].tag==0){ // return; // } // int lc=k<<1,rc=lc+1; // int mid=(cl+cr)>>1; // t[lc].tag+=t[k].tag; // t[rc].tag+=t[k].tag; // t[lc].sum+=(mid-cl+1)*t[k].tag; // t[rc].sum+=(cr-mid)*t[k].tag; // t[k].tag=0; // } void build(int a[],int k,int cl,int cr){ if(cl==cr){ t[k].maxx=t[k].sum=a[cl]; return; 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; } int lc=k<<1,rc=lc+1; int mid=(cl+cr)>>1; build(a,lc,cl,mid); build(a,rc,mid+1,cr); t[k]=merge(t[lc],t[rc]); if(in[i]==out[i]+1){ cntin++; } if(in[i]+1==out[i]){ top=i; cntout++; } } void init(int a[]){ build(a,1,1,n); if(cntin>=2||cntout>=2){ printf("No"); return 0; } long long qry(int k,int l,int r,int cl,int cr){ if(cl>r||cr<l){ return 0; } if(cl>=l&&cr<=r){ return t[k].sum; 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>using namespace std;#define ll long long#define db double#define itn int#define fro for#define scnaf scanf#define snacf scanf#define prinf printfinline 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;}inline string readS(){ string s; char ch=getchar(); while(ch==' '||ch=='\n'){ ch=getchar(); } while(ch!=' '&&ch!='\n'){ s+=ch; ch=getchar(); } return s;}inline string readSL(){ string s; s.clear(); char ch=getchar(); while(ch=='\n'){ ch=getchar(); } while(ch!='\n'){ s+=ch; ch=getchar(); } return s;}inline void readC(char s[]){ int tot=0; char ch=getchar(); while(ch==' '||ch=='\n'){ ch=getchar(); } while(ch!=' '&&ch!='\n'){ s[tot++]=ch; ch=getchar(); }}inline char readc(){ char c=getchar(); while(c==' '||c=='\n'){ c=getchar(); } return c;}inline void write(int x){ if(x<0){ putchar('-'); x=-x; } if(x>9){ write(x/10); } putchar(x%10+'0'); return;}inline void writeS(string x){ int len=x.length(); for(int i=0;i<len;++i){ putchar(char(x[i])); }}signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); return 0;}```
```cpp#include<bits/stdc++.h>#include<unistd.h>using namespace std;#pragma GCC diagnostic error "-std=c++11"#pragma GCC optimize(2)#pragma GCC optimize(3)#pragma GCC optimize("Ofast")#pragma GCC optimize("inline")#pragma GCC optimize("-fgcse")#pragma GCC optimize("-fgcse-lm")#pragma GCC optimize("-fipa-sra")#pragma GCC optimize("-ftree-pre")#pragma GCC optimize("-ftree-vrp")#pragma GCC optimize("-fpeephole2")#pragma GCC optimize("-ffast-math")#pragma GCC optimize("-fsched-spec")#pragma GCC optimize("unroll-loops")#pragma GCC optimize("-falign-jumps")#pragma GCC optimize("-falign-loops")#pragma GCC optimize("-falign-labels")#pragma GCC optimize("-fdevirtualize")#pragma GCC optimize("-fcaller-saves")#pragma GCC optimize("-fcrossjumping")#pragma GCC optimize("-fthread-jumps")#pragma GCC optimize("-funroll-loops")#pragma GCC optimize("-fwhole-program")#pragma GCC optimize("-freorder-blocks")#pragma GCC optimize("-fschedule-insns")#pragma GCC optimize("inline-functions")#pragma GCC optimize("-ftree-tail-merge")#pragma GCC optimize("-fschedule-insns2")#pragma GCC optimize("-fstrict-aliasing")#pragma GCC optimize("-fstrict-overflow")#pragma GCC optimize("-falign-functions")#pragma GCC optimize("-fcse-skip-blocks")#pragma GCC optimize("-fcse-follow-jumps")#pragma GCC optimize("-fsched-interblock")#pragma GCC optimize("-fpartial-inlining")#pragma GCC optimize("no-stack-protector")#pragma GCC optimize("-freorder-functions")#pragma GCC optimize("-findirect-inlining")#pragma GCC optimize("-fhoist-adjacent-loads")#pragma GCC optimize("-frerun-cse-after-loop")#pragma GCC optimize("inline-small-functions")#pragma GCC optimize("-finline-small-functions")#pragma GCC optimize("-ftree-switch-conversion")#pragma GCC optimize("-foptimize-sibling-calls")#pragma GCC optimize("-fexpensive-optimizations")#pragma GCC optimize("-funsafe-loop-optimizations")#pragma GCC optimize("inline-functions-called-once")#pragma GCC optimize("-fdelete-null-pointer-checks")constexpr int BUFSIZE=1<<14; char ibuf[BUFSIZE],*ibg=ibuf+BUFSIZE,obuf[BUFSIZE],*obg=obuf;const char *ied=ibuf+BUFSIZE,*oed=obuf+BUFSIZE;#define INLINE inline __attribute__ ((always_inline))//INLINE char get(){// if(ibg==ied){// cin.read(ibg=ibuf,BUFSIZE);// }// return *ibg++; //} INLINE void put(char a){ if(obg==oed){ write(1,obg=obuf,BUFSIZE); } *(obg++)=a;} INLINE int read(){ if(ied-ibg<=15){unsigned len=ied-ibg; memmove(ibuf,ibg,len); cin.read((ibg=ibuf)+len,BUFSIZE-len); } unsigned a=0;char c=*ibg++;bool f=1; while(c<'0'||c>'9'){ if(c=='-')f=0; //if(c==0)break; c=*ibg++; } while(c>='0'&&c<='9'){ a=(a<<1)+(a<<3)+(c^48); c=*ibg++;//cerr<<c<<' '; } return f?a:(~(a-1));}constexpr unsigned lg10[15]={1,10,100,1000,10000,100000,1000000, 10000000,100000000,1000000000};INLINE void write(unsigned a){ if((int)(a)<0){ put('-'); a=(~(a-1)); } if(a==0){ put('0');put('\n');return; } const unsigned *p=upper_bound(lg10,lg10+10,a)-1; for(;p>=lg10;p--){ put(a/(*p)+'0');a%=(*p); } put('\n');}//#define int long longsigned main(){ //...... write(1,obuf,obg-obuf); return 0;}
```
## [LCA](https://www.luogu.com.cn/problem/P3379)
### 倍增法:
```cpp#include<bits/stdc++.h>using namespace std;const int N=5e5+111;int dep[N];int net[N][37];vector<int>g[N];void dfs(int u,int dad){ dep[u]=dep[dad]+1; net[u][0]=dad; for(int i=1;i<=27;i++){ net[u][i]=net[net[u][i-1]][i-1]; } for(int i=0;i<g[u].size();i++){ int v=g[u][i]; if(v==dad){ continue; } int lc=k<<1,rc=lc+1; int mid=(cl+cr)>>1; // push_down(k,cl,cr); return _merge(qry(lc,l,r,cl,mid),qry(rc,l,r,mid+1,cr),1); dfs(v,u); } void modify(int k,int l,int r,int cl,int cr){ if(cl>r||cr<l){ return; } // cerr<<l<<" "<<r<<"\n"; if(cl==cr){ // cerr<<k<<" "<<t[k].sum<<"\n"; t[k].maxx=t[k].sum=sqrt(t[k].sum); return;}int lca(int x,int y){ if(dep[x]<dep[y]){ swap(x,y); } for(int i=27;i>=0;i--){ if(dep[net[x][i]]>=dep[y]){ x=net[x][i]; } int lc=k<<1,rc=lc+1; int mid=(cl+cr)>>1; // push_down(k,cl,cr); if(t[lc].maxx>1){ modify(lc,l,r,cl,mid); } if(t[rc].maxx>1){ modify(rc,l,r,mid+1,cr); } t[k]=merge(t[lc],t[rc]); if(x==y){ return x; } }}tr;int a[N]; for(int i=27;i>=0;i--){ if(net[x][i]!=net[y][i]){ x=net[x][i]; y=net[y][i]; } } return net[x][0];}int main(){ int n,m,begi; scanf("%d",&n); scanf("%d%d",&m,&begi); int x,y; for(int i=1;i<n;i++){ scanf("%d%d",&x,&y); g[x].push_back(y); g[y].push_back(x); } dfs(begi,0); for(int i=1;i<=m;i++){ int x,y; scanf("%d%d",&x,&y); printf("%d\n",lca(x,y)); } return 0;}```
### 树剖法
```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=1e6;int n,m,begi;int a[N],b[N];vector<int>g[N];int tp[N],son[N],dfn[N];int siz[N],dep[N],fa[N];int tot;void dfs1(int u,int dad){ dep[u]=dep[dad]+1; siz[u]=1; fa[u]=dad; int maxx=-1,maxi=-1; for(int i=0;i<g[u].size();i++){ int v=g[u][i]; if(v==dad){ continue; } dfs1(v,u); if(siz[v]>maxx){ maxi=v; maxx=siz[v]; } siz[u]+=siz[v]; } son[u]=maxi;}void dfs2(int u,int dad,int top){ dfn[u]=++tot; tp[u]=top; if(son[u]!=-1){ dfs2(son[u],u,top); } for(int i=0;i<g[u].size();i++){ int v=g[u][i]; if(v==dad||v==son[u]){ continue; } dfs2(v,u,v); }}int lca(int x,int y){ while(tp[x]!=tp[y]){ if(dep[tp[x]]<dep[tp[y]]){ swap(x,y); } x=fa[tp[x]]; } if(dep[x]>dep[y]){ swap(x,y); } return x;}signed main(){ int tttk=0; while(~scanf("%lld",&n)){ printf("Case #%lld:\n",++tttk); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); } tr.init(a); scanf("%lld",&m); for(int i=1;i<=m;i++){ int op; scanf("%lld",&op); if(op==0){ int x,y; scanf("%lld%lld",&x,&y); if(x>y){ swap(x,y); } tr.modify(1,x,y,1,n); }else{ int x,y; scanf("%lld%lld",&x,&y); if(x>y){ swap(x,y); } printf("%lld\n",tr.qry(1,x,y,1,n)); } } printf("\n"); scanf("%lld%lld%lld",&n,&m,&begi); for(int i=1;i<n;i++){ int x,y; scanf("%lld%lld",&x,&y); g[x].push_back(y); g[y].push_back(x); } dfs1(begi,0); dfs2(begi,0,begi); while(m--){ int x,y; scanf("%lld%lld",&x,&y); printf("%lld\n",lca(x,y)); } return 0;}```
## [线段树](https://www.luogu.com.cn/problem/P3372):
```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=1e6;struct pai{ long long thi,tag;};int n,m;struct tree{ struct pai{ long long thi,tag; }; pai t[N]; int _merge(int a,int b){ return a+b; }78 行未改动 } return 0;}```- ### [树状数组](https://www.luogu.com.cn/problem/P3368):## [树状数组](https://www.luogu.com.cn/problem/P3368):```cpp#include<bits/stdc++.h>using namespace std;const int N=1e6+101;long long tree[N<<1];long long a[N];int n,m;int lowbit(int x){ return x&(-x);}void add(int x,int y){ for(int i=x;i<=n;i+=lowbit(i)){ tree[i]+=y;struct trma{ long long tree[N<<1]; int lowbit(int x){ return x&(-x); }}long long sum(int x){ long long sum=0; for(int i=x;i!=0;i-=lowbit(i)){ sum+=tree[i]; void add(int x,int y){ for(int i=x;i<=n;i+=lowbit(i)){ tree[i]+=y; } } long long sum(int x){ long long sum=0; for(int i=x;i!=0;i-=lowbit(i)){ sum+=tree[i]; } return sum;} return sum; }}tr;int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); add(i,a[i]-a[i-1]); tr.add(i,a[i]-a[i-1]); } for(int i=1;i<=m;i++){ int k; scanf("%d",&k); if(k==1){ int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,z); add(y+1,-z); tr.add(x,z); tr.add(y+1,-z); }else{ int x; scanf("%d",&x); printf("%lld\n",sum(x)); printf("%lld\n",tr.sum(x)); } } return 0;}232 行未改动全部展开 } 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>using namespace std;#define int long longconst int N=1e7+111;struct pai{ int thi,tag,lef,rig;};long long n,m;struct tree{ pai t[N]; int tot; void in_it(int x){ t[x]={0,0,0,0}; } void _init(){ tot=1; in_it(1); } int _merge(int a,int b){ return a+b; } pai merge(pai a,pai b,pai c){ pai ret{_merge(a.thi,b.thi),0,c.lef,c.rig}; return ret; } void push_down(int k,long long cl,long long cr){ if(t[k].tag==0){ return; } if(t[k].lef==0){ t[k].lef=++tot; in_it(t[k].lef); } if(t[k].rig==0){ t[k].rig=++tot; in_it(t[k].rig); } int lc=t[k].lef,rc=t[k].rig; long long mid=(cl+cr)>>1; t[lc].tag+=t[k].tag; t[rc].tag+=t[k].tag; t[lc].thi+=(mid-cl+1)*1ll*t[k].tag; t[rc].thi+=(cr-mid)*1ll*t[k].tag; t[k].tag=0; } int qry(int k,long long l,long long r,long long cl,long long cr){ if(k==0){ return 0; } if(cl>=l&&cr<=r){ return t[k].thi; } long long mid=(cl+cr)>>1; push_down(k,cl,cr); int lc=t[k].lef,rc=t[k].rig; int ansl=0,ansr=0; if(mid>=l){ ansl=qry(lc,l,r,cl,mid); } if(mid+1<=r){ ansr=qry(rc,l,r,mid+1,cr); } return _merge(ansl,ansr); } int qry2(int k,long long cl,long long cr,int x){ if(k==0){ return 0; } if(cl==cr){ return cl; } long long mid=(cl+cr)>>1; push_down(k,cl,cr); int lc=t[k].lef,rc=t[k].rig; // cerr<<t[lc].thi<<" "<<t[rc].thi<<"|"<<x<<"|"<<lc<<" "<<rc<<"|"<<cl<<" "<<cr<<"\n"; if(lc!=0&&t[lc].thi>=x){ return qry2(lc,cl,mid,x); }else{ x-=t[lc].thi; return qry2(rc,mid+1,cr,x); } } void modify(int& k,long long l,long long r,long long cl,long long cr,int x){ if(k==0){ k=++tot; in_it(k); } if(l<=cl&&r>=cr){ t[k].tag+=x; t[k].thi+=x*1ll*(cr-cl+1); return; } long long mid=(cl+cr)>>1; push_down(k,cl,cr); if(mid>=l){ modify(t[k].lef,l,r,cl,mid,x); } if(mid+1<=r){ modify(t[k].rig,l,r,mid+1,cr,x); } t[k]=merge(t[t[k].lef],t[t[k].rig],t[k]); }}tr;int a[N];signed main(){ n=3e7; scanf("%lld",&m); int k; tr._init(); for(int i=1;i<=m;i++){ k=1; int op,x; scanf("%lld%lld",&op,&x); if(op!=4){ x+=1e7+1; } if(op==1){ tr.modify(k,x,x,1,n,1); } if(op==2){ tr.modify(k,x,x,1,n,-1); } if(op==3){ printf("%lld\n",tr.qry(1,1,x-1,1,n)+1); } if(op==4){ printf("%lld\n",(int)(tr.qry2(1,1,n,x)-1e7-1)); } if(op==5){ int y=tr.qry(1,1,x-1,1,n); printf("%lld\n",(int)(tr.qry2(1,1,n,y)-1e7-1)); } if(op==6){ int y=tr.qry(1,1,x,1,n); printf("%lld\n",(int)(tr.qry2(1,1,n,y+1)-1e7-1)); } } return 0;}```## 并查集:
```cppstruct disjoint_set{ int fa[N]; void _init(){ for(int i=0;i<=N-11;i++){ fa[i]=i; } } int find(int x){ if(fa[x]!=x){ fa[x]=find(fa[x]); } return fa[x]; } bool make(int x,int y){ int fx=find(x); int fy=find(y); if(fx==fy){ return 0; } fa[fx]=fy; return 1; }};```
## [线段树合并](https://www.luogu.com.cn/problem/P3224):
```cpp#include<bits/stdc++.h>using namespace std;const int N=6.4e6+111;struct pai{ int thi,tag,lef,rig;};int n,m;int a[N],a_[N];struct tree{ pai t[N]; int tot=0,grd=0; int ver[N]; void in_it(int x){ t[x]={0,0,0,0}; } void _init(int grd){ tot++; ver[grd]=tot; in_it(tot); } int _merge(int a,int b){ return a+b; } pai merge(pai a,pai b,pai c){ pai ret{_merge(a.thi,b.thi),0,c.lef,c.rig}; return ret; } void push_down(int k,int cl,int cr){ if(t[k].tag==0){ return; } if(t[k].lef==0){ t[k].lef=++tot; in_it(t[k].lef); } if(t[k].rig==0){ t[k].rig=++tot; in_it(t[k].rig); } int lc=t[k].lef,rc=t[k].rig; int mid=(cl+cr)>>1; t[lc].tag=t[k].tag; t[rc].tag=t[k].tag; t[lc].thi=t[k].tag*(mid-cl+1); t[rc].thi=t[k].tag*(cr-mid); t[k].tag=0; } int _make(int ka,int kb,int l,int r){ if(ka==0&&kb==0){ return 0; } if(ka==0){// printf("|kb|id=%d l=%d r=%d thi=%d\n",kb,l,r,t[kb].thi); return kb; } if(kb==0){// printf("|ka|id=%d l=%d r=%d thi=%d\n",ka,l,r,t[ka].thi); return ka; } if(l==r){// printf("|kab|id=[%d,%d] l=%d r=%d|ll=%d rr=%d\n",ka,kb,l,r,t[ka].thi,t[kb].thi); t[ka].thi=_merge(t[ka].thi,t[kb].thi); return ka; } push_down(ka,l,r); push_down(kb,l,r); int mid=(l+r)/2; t[ka].lef=_make(t[ka].lef,t[kb].lef,l,mid); t[ka].rig=_make(t[ka].rig,t[kb].rig,mid+1,r); t[ka]=merge(t[t[ka].lef],t[t[ka].rig],t[ka]);// printf("|%d|%d %d|l=%d r=%d thi=%d\n",ka,l,r,t[ka].lef,t[ka].rig,t[ka].thi); return ka; } void make(int &ka,int kb,int l,int r){ ka=_make(ka,kb,l,r); } int qry(int k,int l,int r,int cl,int cr){ if(k==0){ return 0; } if(cl>=l&&cr<=r){ return t[k].thi; } int mid=(cl+cr)>>1; push_down(k,cl,cr); int lc=t[k].lef,rc=t[k].rig; int ansl=0,ansr=0; if(mid>=l){ ansl=qry(lc,l,r,cl,mid); } if(mid+1<=r){ ansr=qry(rc,l,r,mid+1,cr); } return _merge(ansl,ansr); } int rank(int k,int cl,int cr,int x){ if(k==0){ return -1; } if(cl==cr){ return cl; } int mid=(cl+cr)>>1; push_down(k,cl,cr); int lc=t[k].lef,rc=t[k].rig;// printf("||%d|%d %d|l=%d r=%d|ll=%d rr=%d x=%d\n",k,cl,cr,lc,rc,t[lc].thi,t[rc].thi,x); if(t[lc].thi>=x){ return rank(lc,cl,mid,x); }else{ return rank(rc,mid+1,cr,x-t[lc].thi); } } void modify(int& k,int l,int r,int cl,int cr,int x){ if(k==0){ k=++tot; in_it(k); } if(l<=cl&&r>=cr){ t[k].tag=x; t[k].thi=x*(cr-cl+1); return; } int mid=(cl+cr)>>1; push_down(k,cl,cr); if(mid>=l){ modify(t[k].lef,l,r,cl,mid,x); } if(mid+1<=r){ modify(t[k].rig,l,r,mid+1,cr,x); } t[k]=merge(t[t[k].lef],t[t[k].rig],t[k]); }}tr;struct disjoint_set{ int fa[N]; void _init(){ for(int i=0;i<=N-11;i++){ fa[i]=i; } } int find(int x){ if(fa[x]!=x){ fa[x]=find(fa[x]); } return fa[x]; } bool make(int x,int y){ int fx=find(x); int fy=find(y); if(fx==fy){ return 0; } fa[fy]=fx; return 1; }}st;signed main(){ st._init(); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); a_[a[i]]=i; tr._init(i); tr.modify(tr.ver[i],a[i],a[i],1,n,1); } for(int i=1;i<=m;i++){ int x,y; scanf("%d%d",&x,&y); x=st.find(x); y=st.find(y); if(x==y){ continue; } st.make(x,y); tr.make(tr.ver[x],tr.ver[y],1,n); } int k; scanf("%d",&k); while(k--){ char op; cin>>op; if(op=='Q'){ int x,y; scanf("%d%d",&x,&y); x=st.find(x); int ans=tr.rank(tr.ver[x],1,n,y); printf("%d\n",ans==-1?ans:a_[ans]); }else{ int x,y; scanf("%d%d",&x,&y); x=st.find(x); y=st.find(y); if(x==y){ continue; } st.make(x,y); tr.make(tr.ver[x],tr.ver[y],1,n); } } return 0;}```
## [线段树分裂](https://www.luogu.com.cn/problem/P5494)
```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=6.4e6+111;struct pai{ int thi,tag,lef,rig;};int n,m;int a[N];struct tree{ pai t[N]; int tot=0,grd=0; int ver[N]; void in_it(int x){ t[x]={0,0,0,0}; } void _init(int grd){ tot++; ver[grd]=tot; in_it(tot); } int _merge(int a,int b){ return a+b; } pai merge(pai a,pai b,pai c){ pai ret{_merge(a.thi,b.thi),0,c.lef,c.rig}; return ret; } void push_down(int k,int cl,int cr){ if(t[k].tag==0){ return; } if(t[k].lef==0){ t[k].lef=++tot; in_it(t[k].lef); } if(t[k].rig==0){ t[k].rig=++tot; in_it(t[k].rig); } int lc=t[k].lef,rc=t[k].rig; int mid=(cl+cr)>>1; t[lc].tag+=t[k].tag; t[rc].tag+=t[k].tag; t[lc].thi+=t[k].tag*(mid-cl+1); t[rc].thi+=t[k].tag*(cr-mid); t[k].tag=0; } void kill(int &k,int &tp,int l,int r,int cl,int cr){ if(cr<l||cl>r||k==0){ return; } if(cl>=l&&cr<=r){ tp=k; k=0; return; } if(tp==0){ tp=++tot; in_it(tp); } int mid=(cl+cr)>>1; int lc=t[k].lef,rc=t[k].rig; int ltc=t[tp].lef,rtc=t[tp].rig; if(mid>=l){ kill(t[k].lef,t[tp].lef,l,r,cl,mid); } if(mid+1<=r){ kill(t[k].rig,t[tp].rig,l,r,mid+1,cr); } t[k]=merge(t[t[k].lef],t[t[k].rig],t[k]); t[tp]=merge(t[t[tp].lef],t[t[tp].rig],t[tp]); } int make(int ka,int kb,int l,int r){ if(ka==0&&kb==0){ return 0; } if(ka==0){// printf("|kb|id=%lld l=%lld r=%lld thi=%lld\n",kb,l,r,t[kb].thi); return kb; } if(kb==0){// printf("|ka|id=%lld l=%lld r=%lld thi=%lld\n",ka,l,r,t[ka].thi); return ka; } if(l==r){// printf("|kab|id=[%lld,%lld] l=%lld r=%lld|ll=%lld rr=%lld\n",ka,kb,l,r,t[ka].thi,t[kb].thi); t[ka].thi=_merge(t[ka].thi,t[kb].thi); return ka; } push_down(ka,l,r); push_down(kb,l,r); int mid=(l+r)/2; t[ka].lef=make(t[ka].lef,t[kb].lef,l,mid); t[ka].rig=make(t[ka].rig,t[kb].rig,mid+1,r); t[ka]=merge(t[t[ka].lef],t[t[ka].rig],t[ka]);// printf("|%lld|%lld %lld|l=%lld r=%lld thi=%lld\n",ka,l,r,t[ka].lef,t[ka].rig,t[ka].thi); return ka; } int qry(int k,int l,int r,int cl,int cr){ if(k==0){ return 0; } if(cl>=l&&cr<=r){ return t[k].thi; } int mid=(cl+cr)>>1; push_down(k,cl,cr); int lc=t[k].lef,rc=t[k].rig; int ansl=0,ansr=0; if(mid>=l){ ansl=qry(lc,l,r,cl,mid); } if(mid+1<=r){ ansr=qry(rc,l,r,mid+1,cr); } return _merge(ansl,ansr); } int rank(int k,int cl,int cr,int x){ if(k==0){ return -1; } if(cl==cr){ return cl; } int mid=(cl+cr)>>1; push_down(k,cl,cr); int lc=t[k].lef,rc=t[k].rig;// printf("||%lld|%lld %lld|l=%lld r=%lld|ll=%lld rr=%lld x=%lld\n",k,cl,cr,lc,rc,t[lc].thi,t[rc].thi,x); if(t[lc].thi>=x){ return rank(lc,cl,mid,x); }else{ return rank(rc,mid+1,cr,x-t[lc].thi); } } void modify(int& k,int l,int r,int cl,int cr,int x){ if(k==0){ k=++tot; in_it(k); } if(l<=cl&&r>=cr){ t[k].tag+=x; t[k].thi+=x*(cr-cl+1); return; } int mid=(cl+cr)>>1; push_down(k,cl,cr); if(mid>=l){ modify(t[k].lef,l,r,cl,mid,x); } if(mid+1<=r){ modify(t[k].rig,l,r,mid+1,cr,x); } t[k]=merge(t[t[k].lef],t[t[k].rig],t[k]); }}tr;signed main(){ scanf("%lld%lld",&n,&m); for(int i=1;i<=n;i++){ tr._init(i); } for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); tr.modify(tr.ver[1],i,i,1,n,a[i]); } int top=1; while(m--){ int op; scanf("%lld",&op); if(op==0){ int x,y,z; scanf("%lld%lld%lld",&x,&y,&z); tr.kill(tr.ver[x],tr.ver[++top],y,z,1,n); }else if(op==1){ int x,y; scanf("%lld%lld",&x,&y); tr.ver[x]=tr.make(tr.ver[x],tr.ver[y],1,n); }else if(op==2){ int x,y,z; scanf("%lld%lld%lld",&x,&y,&z); tr.modify(tr.ver[x],z,z,1,n,y); }else if(op==3){ int x,y,z; scanf("%lld%lld%lld",&x,&y,&z); printf("%lld\n",tr.qry(tr.ver[x],y,z,1,n)); }else if(op==4){ int x,y; scanf("%lld%lld",&x,&y); int ans=tr.rank(tr.ver[x],1,n,y); printf("%lld\n",ans); } } return 0;}```
## 多合一快读
```cpp#include <cctype>#include <cstdio>#include <cstring>#include <string>#include <type_traits>#include <iostream>#include <iomanip>
#ifdef __SIZEOF_INT128__using int128_t = __int128;#endif
class FastReader {private: static const int SIZE = 1 << 16; char buf[SIZE], *p = buf, *end = buf; inline void refill() { if (p == end) { p = buf; end = buf + fread(buf, 1, SIZE, stdin); } } inline char getChar() { refill(); return *p++; } inline void ungetChar() { if (p > buf) p--; } // 跳过空白字符 inline void skipWhitespace() { while (true) { refill(); if (p == end) return; char c = *p; if (c != ' ' && c != '\n' && c != '\r' && c != '\t') break; p++; } } // 读取有符号整数 template<typename T> void readSignedInt(T& x) { skipWhitespace(); bool negative = false; char ch = getChar(); if (ch == '-') { negative = true; ch = getChar(); } else if (ch == '+') { ch = getChar(); } x = 0; while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0'); ch = getChar(); } if (!(ch >= '0' && ch <= '9')) { ungetChar(); } if (negative) x = -x; } // 读取无符号整数 template<typename T> void readUnsignedInt(T& x) { skipWhitespace(); char ch = getChar(); x = 0; while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0'); ch = getChar(); } if (!(ch >= '0' && ch <= '9')) { ungetChar(); } } // 读取bool类型 void readBool(bool& x) { skipWhitespace(); char ch = getChar(); if (ch == '1') { x = true; } else if (ch == '0') { x = false; } else if (ch == 't') { // 检查"true" getChar(); // 跳过 't' getChar(); // 跳过 'r' getChar(); // 跳过 'u' getChar(); // 跳过 'e' x = true; } else if (ch == 'f') { // 检查"false" getChar(); // 跳过 'f' getChar(); // 跳过 'a' getChar(); // 跳过 'l' getChar(); // 跳过 's' getChar(); // 跳过 'e' x = false; } else { // 其他情况视为false x = false; } } // 读取char类型 void readChar(char& x) { skipWhitespace(); x = getChar(); } // 读取double类型 void readDouble(double& x) { skipWhitespace(); x = 0.0; char ch = getChar(); bool negative = false; if (ch == '-') { negative = true; ch = getChar(); } else if (ch == '+') { ch = getChar(); } // 整数部分 while (ch >= '0' && ch <= '9') { x = x * 10.0 + (ch - '0'); ch = getChar(); } // 小数部分 if (ch == '.') { ch = getChar(); double fraction = 1.0; while (ch >= '0' && ch <= '9') { fraction *= 0.1; x += (ch - '0') * fraction; ch = getChar(); } } // 科学计数法 if (ch == 'e' || ch == 'E') { ch = getChar(); int expSign = 1; if (ch == '-') { expSign = -1; ch = getChar(); } else if (ch == '+') { ch = getChar(); } int exponent = 0; while (ch >= '0' && ch <= '9') { exponent = exponent * 10 + (ch - '0'); ch = getChar(); } // 计算10的exponent次方 double pow10 = 1.0; while (exponent > 0) { pow10 *= 10.0; exponent--; } if (expSign == -1) x /= pow10; else x *= pow10; } if (!(ch >= '0' && ch <= '9') && ch != '.' && ch != 'e' && ch != 'E' && ch != '+' && ch != '-') { ungetChar(); } if (negative) x = -x; } // 读取float类型 void readFloat(float& x) { double tmp; readDouble(tmp); x = static_cast<float>(tmp); } // 读取字符串(单词) void readStringWord(std::string& s) { skipWhitespace(); s.clear(); char ch = getChar(); while (ch != ' ' && ch != '\n' && ch != '\r' && ch != '\t' && ch != EOF) { s.push_back(ch); ch = getChar(); } if (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == EOF) { ungetChar(); } } // 读取整行 void readStringLine(std::string& s) { s.clear(); char ch = getChar(); while (ch != '\n' && ch != EOF) { s.push_back(ch); ch = getChar(); } // 处理Windows换行符\r\n if (ch == '\r') { ch = getChar(); if (ch != '\n') { ungetChar(); } } } // 读取C风格字符串 void readCString(char* s) { skipWhitespace(); char ch = getChar(); int i = 0; while (ch != ' ' && ch != '\n' && ch != '\r' && ch != '\t' && ch != EOF) { s[i++] = ch; ch = getChar(); } s[i] = '\0'; if (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == EOF) { ungetChar(); } } #ifdef __SIZEOF_INT128__ // 读取__int128类型 void readInt128(int128_t& x) { skipWhitespace(); bool negative = false; char ch = getChar(); if (ch == '-') { negative = true; ch = getChar(); } else if (ch == '+') { ch = getChar(); } x = 0; while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0'); ch = getChar(); } if (!(ch >= '0' && ch <= '9')) { ungetChar(); } if (negative) x = -x; }#endif
public: // 统一的读取函数 - 通过简单的重载实现 FastReader& read(char& x) { readChar(x); return *this; } FastReader& read(short& x) { readSignedInt(x); return *this; } FastReader& read(int& x) { readSignedInt(x); return *this; } FastReader& read(long& x) { readSignedInt(x); return *this; } FastReader& read(long long& x) { readSignedInt(x); return *this; } FastReader& read(unsigned short& x) { readUnsignedInt(x); return *this; } FastReader& read(unsigned int& x) { readUnsignedInt(x); return *this; } FastReader& read(unsigned long& x) { readUnsignedInt(x); return *this; } FastReader& read(unsigned long long& x) { readUnsignedInt(x); return *this; } FastReader& read(bool& x) { readBool(x); return *this; } FastReader& read(float& x) { readFloat(x); return *this; } FastReader& read(double& x) { readDouble(x); return *this; } FastReader& read(std::string& x) { readStringWord(x); return *this; } FastReader& read(char* x) { readCString(x); return *this; } #ifdef __SIZEOF_INT128__ FastReader& read(int128_t& x) { readInt128(x); return *this; }#endif // 读取整行的辅助类 class Line { public: std::string& str; explicit Line(std::string& s) : str(s) {} }; FastReader& read(Line line) { readStringLine(line.str); return *this; } // 重载>>运算符 template<typename T> FastReader& operator>>(T& x) { return read(x); } FastReader& operator>>(Line line) { return read(line); }};
// 全局FastReader实例FastReader fin;
// 统一的read函数 - 简单直接的实现template<typename T>void read(T& x) { fin.read(x);}
template<typename T, typename... Args>void read(T& first, Args&... args) { read(first); read(args...);}
// 读取整行的辅助函数inline FastReader::Line readline(std::string& str) { return FastReader::Line(str);}
inline std::string readline() { std::string str; fin.read(readline(str)); return str;}
int main() { // 测试各种类型 char c; short s; int i; long long ll; double d; bool b; std::string word; std::string line; // 使用read函数读取多个参数 read(c, s, i, ll, d, b); // 读取一个单词 read(word); // 读取整行 line=readline(); std::cout << "char: " << c << "\n"; std::cout << "short: " << s << "\n"; std::cout << "int: " << i << "\n"; std::cout << "long long: " << ll << "\n"; std::cout << "double: " << std::fixed << std::setprecision(6) << d << "\n"; std::cout << "bool: " << std::boolalpha << b << "\n"; std::cout << "string (word): " << word << "\n"; std::cout << "string (line): " << line << "\n"; // 使用>>运算符 int x, y; fin >> x >> y; std::cout << "x: " << x << ", y: " << y << "\n"; // 读取C风格字符串 char buffer[100]; fin >> buffer; std::cout << "C string: " << buffer << "\n"; // 测试__int128(如果支持) #ifdef __SIZEOF_INT128__ int128_t sdfgi128; fin >> sdfgi128; std::cout << "__int128: " << (long long)(sdfgi128 % 1000000000) << "...\n"; #endif return 0;}```## [树链剖分](https://www.luogu.com.cn/problem/P3178)
```cpp#include<bits/stdc++.h>using namespace std;#define int long longint mod;const int N=2e5;int n,m,begi;int a[N],b[N];vector<int>g[N];struct tree{ struct pai{ long long thi,tag; }; pai t[N<<2]; int _merge(int a,int b){ return (a+b)%mod; } pai merge(pai a,pai b){ pai ret{_merge(a.thi,b.thi),0}; return ret; } void push_down(int k,int cl,int cr){ if(t[k].tag==0){ return; } int lc=k<<1,rc=lc+1; int mid=(cl+cr)>>1; (t[lc].tag+=t[k].tag)%=mod; (t[rc].tag+=t[k].tag)%=mod; (t[lc].thi+=(mid-cl+1)*t[k].tag)%=mod; (t[rc].thi+=(cr-mid)*t[k].tag)%=mod; t[k].tag=0; } void build(int a[],int k,int cl,int cr){ if(cl==cr){ t[k].thi=a[cl]; return; } int lc=k<<1,rc=lc+1; int mid=(cl+cr)>>1; build(a,lc,cl,mid); build(a,rc,mid+1,cr); t[k]=merge(t[lc],t[rc]); } void init(int a[]){ build(a,1,1,n); } long long qry(int k,int l,int r,int cl,int cr){ if(cl>r||cr<l||l>r){ return 0; } if(cl>=l&&cr<=r){ return t[k].thi%mod; } int lc=k<<1,rc=lc+1; int mid=(cl+cr)>>1; push_down(k,cl,cr); return _merge(qry(lc,l,r,cl,mid),qry(rc,l,r,mid+1,cr)); } void modify(int k,int l,int r,int cl,int cr,long long x){ if(cl>r||cr<l||l>r){ return; } if(l<=cl&&r>=cr){ (t[k].tag+=x)%=mod; (t[k].thi+=x*1ll*(cr-cl+1)%mod)%=mod; return; } int lc=k<<1,rc=lc+1; int mid=(cl+cr)>>1; push_down(k,cl,cr); modify(lc,l,r,cl,mid,x); modify(rc,l,r,mid+1,cr,x); t[k]=merge(t[lc],t[rc]); }}tr;int tp[N],son[N],dfn[N];int siz[N],dep[N],fa[N];int tot;void dfs1(int u,int dad){ dep[u]=dep[dad]+1; siz[u]=1; fa[u]=dad; int maxx=-1,maxi=-1; for(int i=0;i<g[u].size();i++){ int v=g[u][i]; if(v==dad){ continue; } dfs1(v,u); if(siz[v]>maxx){ maxi=v; maxx=siz[v]; } siz[u]+=siz[v]; } son[u]=maxi;}void dfs2(int u,int dad,int top){ dfn[u]=++tot; tp[u]=top; if(son[u]!=-1){ dfs2(son[u],u,top); } for(int i=0;i<g[u].size();i++){ int v=g[u][i]; if(v==dad||v==son[u]){ continue; } dfs2(v,u,v); }}int qrylist(int x,int y){ int ans=0; while(tp[x]!=tp[y]){ if(dep[tp[x]]<dep[tp[y]]){ swap(x,y); } ans+=tr.qry(1,dfn[tp[x]],dfn[x],1,n); ans%=mod; x=fa[tp[x]]; } if(dfn[x]>dfn[y]){ swap(x,y); } ans+=tr.qry(1,dfn[x],dfn[y],1,n); ans%=mod; return ans;}void modilist(int x,int y,int z){ while(tp[x]!=tp[y]){ if(dep[tp[x]]<dep[tp[y]]){ swap(x,y); } tr.modify(1,dfn[tp[x]],dfn[x],1,n,z%mod); x=fa[tp[x]]; } if(dfn[x]>dfn[y]){ swap(x,y); } tr.modify(1,dfn[x],dfn[y],1,n,z%mod);}int qrytree(int x){ return tr.qry(1,dfn[x],dfn[x]+siz[x]-1,1,n);}void moditree(int x,int y){ tr.modify(1,dfn[x],dfn[x]+siz[x]-1,1,n,y%mod);}signed main(){ scanf("%lld%lld%lld%lld",&n,&m,&begi,&mod); for(int i=1;i<=n;i++){ scanf("%lld",&b[i]); } for(int i=1;i<n;i++){ int x,y; scanf("%lld%lld",&x,&y); g[x].push_back(y); g[y].push_back(x); } dfs1(begi,0); dfs2(begi,0,begi); for(int i=1;i<=n;i++){ a[dfn[i]]=b[i]; } tr.init(a); while(m--){ int op; scanf("%lld",&op); if(op==1){ int x,y,z; scanf("%lld%lld%lld",&x,&y,&z); modilist(x,y,z); }else if(op==2){ int x,y; scanf("%lld%lld",&x,&y); printf("%lld\n",qrylist(x,y)); }else if(op==3){ int x,y; scanf("%lld%lld",&x,&y); moditree(x,y); }else{ int x; scanf("%lld",&x); printf("%lld\n",qrytree(x)); } } return 0;}```## [treap/splay/FHQ-treap大合集]((https://www.luogu.com.cn/problem/P3369))
```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=2e5+111;int n;struct Splay{ #define ll s[0] #define rr s[1] int root,tot=0; struct tree{ int s[3]; int thi,fa,siz,t; }tr[N]; bool right(int u){ return tr[tr[u].fa].rr==u; } void init(int u,int x,int fa){ tr[u].thi=x; tr[u].fa=fa; tr[u].siz=tr[u].t=1; } void merge(int u){ tr[u].siz=tr[tr[u].ll].siz+tr[tr[u].rr].siz+tr[u].t; } void flyup(int u){ int y=tr[u].fa; int w=tr[y].fa; bool nokx=right(u); bool noky=right(y); tr[w].s[noky]=u; tr[u].fa=w; tr[y].s[nokx]=tr[u].s[nokx^1]; tr[tr[u].s[nokx^1]].fa=y; tr[u].s[nokx^1]=y; tr[y].fa=u; merge(y); merge(u); } void splay(int u,int to){ while(tr[u].fa!=to){ int y=tr[u].fa; int w=tr[y].fa; if(w!=to){ if(right(u)!=right(y)){ flyup(u); }else{ flyup(y); } } flyup(u); } if(to==0){ root=u; } } void find(int x){ int u=root; if(u==0){ return; } while(tr[u].thi!=x&&tr[u].s[tr[u].thi<x]!=0){ u=tr[u].s[tr[u].thi<x]; } splay(u,0); } void add(int x){ int u=root,fa=0; while(u!=0&&tr[u].thi!=x){ fa=u; u=tr[u].s[tr[u].thi<x]; } if(u!=0){ tr[u].t++; splay(u,0); return; } u=++tot; init(u,x,fa); if(fa!=0){ tr[fa].s[tr[fa].thi<x]=u; } splay(u,0); } int prext(int x,bool f){ find(x); int u=root; if(f==0&&tr[u].thi<x){ return u; } if(f==1&&tr[u].thi>x){ return u; } u=tr[u].s[f]; while(tr[u].s[f^1]!=0){ u=tr[u].s[f^1]; } splay(u,0); return u; } void del(int x){ int l=prext(x,0); int r=prext(x,1); splay(l,0); splay(r,l); if(tr[tr[r].ll].t>1){ tr[tr[r].ll].t--; splay(tr[r].ll,0); return; } tr[r].ll=0; merge(r); merge(l); } int rank(int u){ add(u); find(u); int k=tr[tr[root].ll].siz; del(u); return k; } int rankth(int th){ th++; int u=root; if(tr[u].siz<th){ return -1; } while(1){ if(th>tr[tr[u].ll].siz+tr[u].t){ th-=tr[tr[u].ll].siz+tr[u].t; u=tr[u].rr; }else if(th<=tr[tr[u].ll].siz){ u=tr[u].ll; }else{ splay(u,0); return tr[u].thi; } } } void _init(){ add(-0x3f3f3f3f); add(0x3f3f3f3f); } #undef ll #undef rr};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("%lld%lld",&op,&x); if(op==1){ t3.add(x); }else if(op==2){ t3.del(x); }else if(op==3){ printf("%lld\n",t3.rank(x)); }else if(op==4){ printf("%lld\n",t3.rankth(t3.root,x)); }else if(op==5||op==6){ 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{ static uint64_t splitmix64(uint64_t x){ x+=0x9e3779b97f4a7c15; x=(x^(x>>30))*0xbf58476d1ce4e5b9; x=(x^(x>>27))*0x94d049bb133111eb; return x^(x>>31); } size_t operator()(uint64_t x)const{ static const uint64_t FIXED_RANDOM=chrono::steady_clock::now().time_since_epoch().count(); 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;}```