版本比较
各个算法标准代码
正文
增加 318 行删除 8 行
11234510 行未改动16171819202122232224252627282925 行未改动555657585659606162636415 行未改动808182838084858687888951 行未改动14114214314414014514614714814915054 行未改动20520620720820320921021121221321471 行未改动28628728828928329029129229329429542 行未改动33833934034133434234334434534634740 行未改动388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692一些很有用的知识:::::info[一些很有用的知识:]
2维vector数组定义:
- 空二维 vector(先定义,后赋值)10 行未改动统一设置所有元素的初始值
vector<vector<int>>mat(3,vector<int>(4,10));
::::
各种算法标准代码:
并查集::::info[并查集]
```cpp#include <bits/stdc++.h>using namespace std;int f[5005];//f[i]表示i的目前最远祖先25 行未改动 cout<<(findfather(x)==findfather(y)?"Yes":"No"); }//输出查询结果}```美国血统(求后序遍历)::::::::info[美国血统(求后序遍历)]```cpp#include <bits/stdc++.h>using namespace std;void dfs(string bef,string mid){15 行未改动 dfs(bef,mid); return 0;}```单调栈::::::::info[单调栈]```cpp#include<bits/stdc++.h>#define ll long longusing namespace std;51 行未改动 out();//输出 return 0;}```单调队列::::::::info[单调队列]```cpp#include <bits/stdc++.h>#define ll long longusing namespace std;54 行未改动 out();//输出 return 0;}```归并排序::::::::info[归并排序]```cpp#include <bits/stdc++.h>#define ll long longusing namespace std;71 行未改动 out();//输出 return 0;}```链式前向星(无向图,无边权,求深度)::::::::info[链式前向星(无向图,无边权,求深度)]```cpp#include <bits/stdc++.h>using namespace std;int n,d,elen=0,ans=0;//n->节点数量 d->深度上限 elen->当前边总数 ans->结果42 行未改动 cout<<ans; return 0;}```堆(大根堆)::::::::info[堆(大根堆)]```cpp#include<bits/stdc++.h>using namespace std;vector<int>h;//堆(大根堆)40 行未改动 } } return 0;}```::::::::info[对顶堆]```cpp#include<bits/stdc++.h>using namespace std;vector<int>up;//大根堆vector<int>dn;//小根堆int len_up=0,len_dn=0;int n,k;void up_up(int x){//大根堆向上调整 while(x>1&&up[x]<up[x>>1]){ swap(up[x],up[x>>1]); x>>=1; } }void up_dn(int x){//小根堆向上调整 while(x>1&&dn[x]>up[x>>1]){ swap(up[x],up[x>>1]); x>>=1; }}void down_up(int x){//大根堆向下调整 int maxs; while(x<<1<=len_up){ maxs=x<<1; if(maxs+1<=n&&up[maxs+1]>up[maxs]){ maxs++; } if(up[maxs]<=up[x]){ break; } swap(up[x],up[maxs]); x=maxs; }}void down_dn(int x){//小根堆向下调整 int mins; while(x<<1<=len_dn){ mins=x<<1; if(mins+1<=n&&dn[mins+1]<dn[mins]){ mins++; } if(dn[mins]>=dn[x]){ break; } swap(dn[x],dn[mins]); x=mins; }}void insert_up(int x){//大根堆插入 up[++len_up]=x; up_up(len_up);}void insert_dn(int x){//小根堆插入 dn[++len_dn]=x; up_dn(len_up);}void delete_up(){//大根堆删除 swap(up[1],up[len_up]); up[len_up--]=0; down_up(1);}void delete_dn(){//小根堆删除 swap(dn[1],dn[len_dn]); up[len_dn--]=0; down_dn(1);}void balance(){//维护对顶堆 int x; if(len_dn==k){ return; } else if(len_dn<k){ while(len_dn!=k){ x=up[1]; delete_up(); insert_dn(x); } } else{ while(len_dn!=k){ x=dn[1]; delete_dn(); insert_up(x); } }}void insert(int x){//在序列中插入 if(x>=dn[1]){ insert_dn(x); } else{ insert_up(x); }}int main(){ int op,x; scanf("%d%d",&n,&k); up.resize(n+5); dn.resize(n+5); for(int i=1;i<=n;i++){ scanf("%d",&op); if(op==1){//插入 scanf("%d",&x); insert(x); } else if(op==2){//查询 printf("%d\n",dn[1]); } else if(op==3){//删除 delete_dn(); } else{//调整k值 scanf("%d",&x); k+=x; } balance();//维护对顶堆 }}::::::::info[左偏树(封装为类)]```cpp#include<bits/stdc++.h>using namespace std;struct tnode{ int value; int dist; tnode* left; tnode* right; tnode(int v):value(v),dist(0),left(nullptr),right(nullptr){}};struct heap{ private: tnode* root; bool cmp(tnode* x,tnode* y){//compare x with y return x->value<y->value; } tnode* mergenode(tnode* x,tnode* y){//merge two heaps who use x and y for root if(!x)return y; if(!y)return x; if(cmp(y,x))swap(x,y); x->right=mergenode(x->right,y); int ld=(x->left)?x->left->dist:-1; int rd=(x->right)?x->right->dist:-1; if(ld<rd)swap(x->left,x->right); x->dist=(x->right)?x->right->dist+1:0; return x; } void free(tnode* x){//free the heap who use x for root if(!x)return; free(x->left); free(x->right); delete x; } heap(const heap&)=delete;//you mustn't copy heap& operator=(const heap&)=delete;//you mustn't use '=' to give a heap to another heap public: heap():root(nullptr){}//build heap ~heap(){//delete heap free(root); } bool empty()const{//x->empty() return root==nullptr; } int top()const{//x->top() return (root==nullptr)?throw runtime_error("heap is empty"):root->value; } void merge(heap* other){//x->merge(heap* y) if(!other||other->empty())return; root=mergenode(root,other->root); other->root=nullptr; delete other; } void push(int k){//x->push(int k) tnode* newnode=new tnode(k); root=mergenode(root,newnode); } void pop(){//x->pop() if(empty())throw runtime_error("pop from empty heap"); tnode* old=root; root=mergenode(root->left,root->right); delete old; } static heap* mergeheap(heap* x,heap* y){//z=heap::mergeheap(heap* x,heap* y) if(!x)return y; if(!y)return x; tnode* xr=x->root; tnode* yr=y->root; heap* z=new heap(); z->root=mergenode(xr,yr); if(x)x->root=nullptr; if(y)y->root=nullptr; delete x; delete y; return z; }};int main(){ }```::::::::info[2-3-4树]```cpp#include<bits/stdc++.h>using namespace std;struct num{ int val; int count; num(int v):val(v),count(1){}};struct tnode{ num* left; num* mid; num* right; int size; tnode* left_left; tnode* left_right; tnode* right_left; tnode* right_right; tnode(int v):left(nullptr),mid(new num(v)),right(nullptr),size(1),left_left(nullptr),left_right(nullptr),right_left(nullptr),right_right(nullptr){}};struct _2_3_4_tree{ private: tnode* root; void fill(num& x,int k){//在x的位置填充k这个数 num* old=x; x=new num(k); delete old; } void move(num& x,num& y){//将x的位置上的数移动到y的位置 y->val=x->val; y->count=x->count; x->val=0; x->count=0; } pair<int,int> insert_leaf(tnode* x,int k){ x->size++; if(!x->left&&k<x->mid){ x->left=new num(k); return {0,0}; } if(!x->right&&k>x->mid){ x->right=new num(k); return {0,0}; } if(k==x->mid->val){ x->mid->count++; return {0,0}; } if(k==x->left->val){ x->left->count++; return {0,0}; } if(k==x->right->val){ x->right->count++; return {0,0}; } x->size--; if(x->left&&x->right){ int up=x->mid->val,upcount=x->mid->count; if(k<x->mid->val){ move(x->left,x->mid); } else{ move(x->right,x->mid); } insert_leaf(x,k); return {up,upcount}; } x->size++; if(k<x->mid->val){ move(x->mid,x->right); if(k<x->left){ move(x->left,x->mid); fill(x->left,k); } else{ fill(x->mid,k); } return {0,0}; } else{ move(x->mid,x->left); if(k>x->right){ move(x->right,x->mid); fill(x->right,k); } else{ fill(x->mid,k); } return {0,0}; } }};int main(){ }```::::