### 一维数组初始化
```cpp
for(int i=0;i<n;i++)
a[i]=0;memset(a,0,sizeof(a));fill(a,a+n,0);读入优化
- 关闭同步流
cin.tie(0)->sync_with_stdio(0);
cout.tie(0)->sync_with_stdio(0);- getchar() / getchar_unlocked()
int read(){
int res=0,flag=1;
char ch;
ch=getchar(); //ch=getchar_unlocked()
if(ch=='-')
flag=-1;
else if(ch>='0' && ch<='9')
res=ch-'0';
while((ch=getchar() /*ch=getchar_unlocked()*/)>='0' && ch<='9')
res=res*10+(ch-'0');
return res*flag;
}- fread()
char buf[1<<20],*p1,*p2;
#define gc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<20,stdin)),*p1++)
int read(){
int k=0,f=1,c=gc();
for(;!isdigit(c);c=gc())
if(c=='-')
f=-1;
for(;isdigit(c);c=gc())
k=k*10+(c^48);
return k*f;
}三分查找
//凸函数极大值
while(l < r) {
int lmid=l+(r-l)/3;
int rmid=r-(r-l)/3;
if(calc(lmid)>=calc(lmid))
r=rmid-1;
else
l=lmid+1;
}
cout<<max(calc(l),calc(r));线性筛素数
埃氏筛 >
void sieve(int n){
memset(f,true,sizeof(f));
f[0]=f[1]=false;
for(int i=2;i<=sqrt(n);i++)
if(f[i]==true)
for(int j=i*i;j<=n;j+=i)
f[j]=false;
}欧拉筛 >
void euler(int n){
memset(f,true,sizeof(f));
f[0]=f[1]=false;
for(int i=2;i<=n;i++){
if(f[i]==true)
prime[++cnt]=i;
for(int j=1;j<=cnt && prime[j]<=n/i;j++){
f[i*prime[j]]=false;
if(i%prime[j]==0)
break;
}
}
return;
}Miller-Rabin算法
int mul(int a,int b,int mod){ //快速幂取模
return (__int128)a*b%mod;
}
int pow(int b,int ep,int mod){
int res=1;
while(ep>0){
if(ep&1)
res=mul(res,b,mod);
b=mul(b,b,mod);
ep>>=1;
}
return res;
}
bool miller_rabin(int n,int a){ //Miller-Rabin素性测试
int d=n-1,s=0,x;
if(n%a==0)
return false;
while(d%2==0){
d/=2;
s++;
}
x=pow(a,d,n);
if(x==1 || x==n-1)
return true;
for(int i=1;i<s;i++){
x=mul(x,x,n);
if(x==n-1)
return true;
}
return false;
}
bool check(int n){ //判断是否为质数
if(n<2) return false;
else if(n==2 || n==3) return true;
else if(n%2==0) return false;
//基于Miller-Rabin算法的测试基
int test[]={2,3,5,7,11,13,17,19,23,29,31,37};
for(int a:test){
if(a>=n)
break;
if(!miller_rabin(n,a))
return false;
}
return true;
}排序(一维数组)
冒泡排序
for(int i=1;i<n;i++)
for(int j=1;j<=n-i;j++)
if(a[j]<a[j+1])
swap(a[j],a[j+1]);选择排序
for(int i=1;i<n;i++){
int p=i;
for(int j=i+1;j<=n;j++)
if(a[j]<a[p])
p=j;
swap(a[i],a[p]);
}插入排序
///std
for(int i=1;i<n;i++){
int k=a[i];
int j=i-1;
while(j>=0 && a[j]>k){
a[j+1]=a[j];
j--;
}
a[j+1]=k;
}//折半插入
void mid_sort(){
if(n<2)
return;
for(int i=1;i!=n;i++){
int k=a[i];
auto idx=upper_bound(a,a+i,k)-a;
memmove(a+idx+1,a+idx,(i-idx)*sizeof(int));
a[idx]=k;
}
}归并排序
void merge(int l,int mid,int r){
int p1=l,p2=mid+1,pos=l;
while(p1<=mid && p2<=r){
if(a[p1]<=a[p2])
b[pos++]=a[p1++];
else
b[pos++]=a[p2++];
}
while(p1<=mid)
b[pos++]=a[p1++];
while(p2<=r)
b[pos++]=a[p2++];
for(int i=l;i<=r;i++)
a[i]=b[i];
return;
}
void merge_sort(int l,int r){
if(l>=r)
return;
int mid=(l+r)/2;
merge_sort(l,mid);
merge_sort(mid+1,r);
merge(l,mid,r);
return;
}
int main(){
merge_sort(1,n);
}sort >
sort(a+1,a+1+n/*,cmp*/);堆排序
//小根堆->从小到大排
int n,x,cnt,a[maxn],hp[maxn];
void hp_push(int x){
hp[cnt]=x;
cnt++;
int t=cnt-1;
while(t>0){
if(hp[(t-1)/2]<hp[t])
break;
swap(hp[(t-1)/2],hp[t]);
t=(t-1)/2;
}
return;
}
void hp_pop(){
if(cnt==0){
cout<<"Empty!\n";
return;
}
hp[0]=hp[cnt-1];
cnt--;
int t=0;
while(t*2+1<cnt){
int p=t*2+1;
if(p<cnt-1 && hp[p]>hp[p+1])
p++;
if(hp[t]<=hp[p])
break;
swap(hp[p],hp[t]);
t=p;
}
return;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>x;
hp_push(x);
}
for(int i=1;i<=n;i++){
a[i]=hp[0];
hp_pop();
}
return 0;
}树结构
定义
void push1(int u,int v,bool f){ //邻接表存储(通用树)
vector<int> tree[maxn]; //tree[u]存储节点u的所有子节点/邻居
if(f==false){ //无向树
tree[u].push_back(v);
tree[v].push_back(u);
}
else //有向树
tree[u].push_back(v);
}
void push2(){ // 数组模拟 (仅完全二叉树 )
int tree[maxn]; // tree[i]存储节点i的值
// 节点i(从1开始)的:
// 左子节点=2*i
// 右子节点=2*i+1
// 父节点=i/2(整除)
// 例如:tree[1]是根,tree[2]和tree[3]是根的左右子节点
}DFS
void per_dfs(int dep){ //前序 根左右
if(tree[dep].size()==0) //叶子结点
return;
cout<<dep<<" ";
for(int i=0;i<tree[dep].size();i++)
per_dfs(tree[dep][i]);
}
void in_dfs(int dep){ //中序 左根右(仅二叉树)
if(tree[dep].size()==0) //叶子结点
return;
if(tree[dep].size()>=1)
in_dfs(tree[dep][0]);
cout<<dep<<" ";
if(tree[dep].size()>=2)
in_dfs(tree[dep][1]);
}
void post_dfs(int dep){ //后序 左右根
if(tree[dep].size()==0) //叶子结点
return;
for(int i=0;i<tree[dep].size();i++)
post_dfs(tree[dep][i]);
cout<<dep<<" ";
}
void dfs(int dep,int f){ //通用树
cout<<dep<<" ";
for(vector<int>::iterator it=tree[dep].begin();it!=tree[dep].end();it++){
if(*it==f)
continue; //不返回父节点
dfs(*it,dep);
}
}堆结构
手写堆
//小根堆
int cnt,hp[maxn];
bool hp_empty(){ //判空
return cnt==0;
}
int hp_top(){ //取堆顶(最小值)
return hp[0];
}
void hp_push(int x){ //入堆
hp[cnt]=x;
cnt++;
int t=cnt-1;
while(t>0){
if(hp[(t-1)/2]<=hp[t])
break;
swap(hp[(t-1)/2],hp[t]);
t=(t-1)/2;
}
return;
}
void hp_pop(){ //去堆顶
if(cnt==0){
cout<<"Empty!\n";
return;
}
hp[0]=hp[cnt-1];
cnt--;
int t=0;
while(t*2+1<cnt){
int p=t*2+1;
if(p<cnt-1 && hp[p]>hp[p+1])
p++;
if(hp[t]<=hp[p])
break;
swap(hp[p],hp[t]);
t=p;
}
return;
}//大根堆
int cnt,hp[maxn];
bool hp_empty(){ //判空
return cnt==0;
}
int hp_top(){ //取堆顶(最大值)
return hp[0];
}
void hp_push(int x){ //入堆
hp[cnt]=x;
cnt++;
int t=cnt-1;
while(t>0){
if(hp[(t-1)/2]>=hp[t])
break;
swap(hp[(t-1)/2],hp[t]);
t=(t-1)/2;
}
return;
}
void hp_pop(){ //去堆顶
if(cnt==0){
cout<<"Empty!\n";
return;
}
hp[0]=hp[cnt-1];
cnt--;
int t=0;
while(t*2+1<cnt){
int p=t*2+1;
if(p<cnt-1 && hp[p]<hp[p+1])
p++;
if(hp[t]>=hp[p])
break;
swap(hp[p],hp[t]);
t=p;
}
return;
}STL
priority_queue<int> hp1; //大根堆
priority_queue<int,vector<int>,greater<int>> hp2; //小根堆
q.top() //取得堆顶元素,并不会弹出
q.pop() //弹出堆顶元素
q.push() //往堆里面插入一个元素
q.empty() //查询堆是否为空,为空则返回1否则返回0
q.size() //查询堆内元素数量链表
void init(){ //清空链表
tail=cnt=0;
lis[tail].r=-1;
mp.clear();
}
void add_tail(int x){ //尾部添加元素x O(1)
lis[++cnt]={tail,-1,x};
lis[tail].r=cnt;
tail=mp[x]=cnt;
}
void del_tail(){ //删除尾部元素 O(1)
mp[lis[tail].val]=0;
lis[lis[tail].l].r=-1;
tail=cnt;
}
void insert(int k,int x,int opt){
//opt=1:O(1) 元素k后添加; opt=2:O(k) 第k位后添加
int nw=0;
if(opt==1)
nw=mp[x];
else
for(int i=1;i<=k;i++){
nw=lis[nw].r;
if(nw==-1)
return;
}
if(nw==tail){
add_tail(x);
return;
}
lis[++cnt]={nw,lis[nw].r,x};
lis[lis[nw].r].l=cnt;
lis[nw].r=mp[x]=cnt;
}
void del(int x){ //删除元素x O(1)
int nw=mp[x];
if(nw==tail){
del_tail();
return;
}
mp[x]=0;
lis[lis[nw].l].r=lis[nw].r;
lis[lis[nw].r].l=lis[nw].l;
}
bool check(int x){ //检查链表内有无元素x O(1)
return (mp[x]!=0);
}
void out(){ //输出链表 O(n)
if(lis[0].r==-1){
cout<<"NULL\n";
return;
}
for(int i=lis[0].r;i!=-1;i=lis[i].r)
cout<<lis[i].val<<" ";
cout<<"\n";
}并查集
void init(){
for(int i=1;i<=n;i++)
f[i]=i;
memset(sz,1,sizeof(sz));
}
int find(int u){
if(u==f[u])
return u;
return f[u]=find(f[u]);
}
void merge(int u,int v){
u=find(u);
v=find(v);
if(sz[u]<sz[v]){
f[u]=v;
sz[v]+=sz[u];
return;
}
f[v]=u;
sz[u]+=sz[v];
}
bool check(int u,int v){
return (find(u)==find(v));
}求星期几
int dow(int y,int m,int d){
static int t[]={0,3,2,5,0,3,5,1,4,6,2,4};
y-=m<3;
return (y+y/4-y/100+y/400+t[m-1]+d)%7;
}