跳到正文

算法·理论

些许板子

yaoyg约 22 分钟
原文
文章信息与版本

文章信息

发布
2025年11月23日
收录
2025年12月12日
更新
2026年10月27日
篇幅
约 22 分钟

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1

双端队列

C++
#include<bits/stdc++.h>
using namespace std;
//有个东西叫做deque
struct Dque
{
  int q[10000*2];
  int head=10000;
  int tail=10000-1;
}
bool r;
bool empty(Dque p)
{
  return p.tail<p.head;
}
int size(Dque p)
{
  return p.tail-p.head+1;
}
int front(Dque p,bool r)
{
  if(r)
  {
    return p.q[p.tail];
  }
  else
  {
    return p.q[p.head];
  }
}
int back(Dque p,bool r)
{
  if(!r)
  {
    return p.q[p.tail];
  }
  else
  {
    return p.q[p.head];
  }
}
int push_front(Dque p,int x,bool r)
{
  if(r)
  {
    p.q[++p.tail]=x;
  }
  else
  {
    p.q[--p.head]=x;
  }
}
int push_back(Dque p,int x,bool r)
{
  if(!r)
  {
    p.q[++p.tail]=x;
  }
  else
  {
    p.q[--p.head]=x;
  }
}
int pop_front(Dque p,bool r)
{
  if(r)
  {
    p.tail--;
  }
  else
  {
    p.head++;
  }
}
int pop_back(Dque p,bool r)
{
  if(!r)
  {
    p.tail--;
  }
  else
  {
    p.head++;
  }
}
void reverse(bool r)
{
  r^=1;
}
//代码有亿点长,使用三目运算符可以简化一下代码
int main()
{
  //主函数内容不必多说了吧(^-^)
    return 0;
}

并查集

C++
#include<bits/stdc++.h>
using namespace std;

int fa[1000000],n,m;

int find(int x)//寻找父亲
{
    if(fa[x]!=x)
    {
        fa[x]=find(fa[x]);
    }
    return fa[x];
}

int merge(int x,int y)//合并
{
    x=find(x),y=find(y);
    if(x!=y)
    { 
        fa[x]=y;
    }
    return 0;
}

int haveCA(int x,int y)//有无共同祖先
{
    x=find(x),y=find(y);
    if(x==y)
    { 
        //cout<<"Y"<<endl;
        return 1;
    }
    else
    {
        //cout<<"N"<<endl;
        return 0;
    }
}

int main(){
    //...(0-O)你说这里应该不需要给出来的,对吧?
    return 0;
}

平衡树

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int inf=0x3f3f3f3f;
int gen1,id;
int n,opt,x;
struct node 
{
    int l,r;//左子右子
    int key;//值
    int val;//随机值(随缘旋转)
    int size;//子树长度
    int cnt;//有多少个数
}tr[N];
int get(int key)
{
    tr[++id].key=key;
    tr[id].val=rand();
    tr[id].size=tr[id].cnt=1;
    return id;
}
void push_up(int q)
{
    tr[q].size=tr[tr[q].r].size+tr[tr[q].l].size+tr[q].cnt;
    return;
}
void build()
{
    get(-inf);
    get(inf);
    gen1=1;
    tr[1].r=2;
    push_up(1);
}
void zig(int& p)//左
{
    int q=tr[p].l;
    tr[p].l=tr[q].r;
    tr[q].r=p;
    p=q;
    push_up(tr[p].r);
    push_up(p);
}
void zag(int& p)//右
{
    int q=tr[p].r;
    tr[p].r=tr[q].l;
    tr[q].l=p;
    p=q;
    push_up(tr[p].l);
    push_up(p);
}
void insert(int& p,int key)
{
    if (!p)
    {
        p=get(key);
        return;
    }
    else
    {
        if (tr[p].key==key)
        {
            tr[p].cnt++;
        }
        else
        {
            if (tr[p].key>key)
            {
                insert(tr[p].l,key);
                if(tr[tr[p].l].val>tr[p].val)
                {
                    zig(p);
                }
            }
            else
            {
                insert(tr[p].r,key);
                if(tr[tr[p].r].val>tr[p].val)
                {
                    zag(p);
                }
            }
        }
        push_up(p);
    }
}
void remove(int& p,int key)
{
    if (!p)
    {
        return;
    }
    if (tr[p].key==key)
    {
        if (tr[p].cnt>1)
        {
            tr[p].cnt--;
        }
        else
        {
            if (tr[p].l||tr[p].r)
            {
                if (!tr[p].r||tr[tr[p].l].val>tr[tr[p].r].val)
                {
                    zig(p);
                    remove(tr[p].r,key);
                }
                else
                {
                    zag(p);
                    remove(tr[p].l,key);
                }
            }
            else
            {
                p=0;
            }
        }
    }
    else if (tr[p].key>key)
    {
        remove(tr[p].l,key);
    }
    else
    {
        remove(tr[p].r,key);
    }
    push_up(p);
}

int get_rank(int p,int key)
{
    if(!p)
    {
        return 1;
    }
    if(tr[p].key==key)
    {
        return tr[tr[p].l].size+1;
    }
    else if(tr[p].key>key)
    {
        return get_rank(tr[p].l,key);
    }
    else
    {
        return tr[tr[p].l].size+tr[p].cnt+get_rank(tr[p].r,key);
    }
}
int get_key(int p,int rank)
{
    if(!p)
    {
        return inf;
    }
    if(tr[tr[p].l].size>=rank)
    {
        return get_key(tr[p].l,rank);
    }
    else if(tr[tr[p].l].size+tr[p].cnt>=rank)
    {
        return tr[p].key;
    }
    else
    {
        return get_key(tr[p].r,rank-tr[tr[p].l].size-tr[p].cnt);
    }
}
int get_last(int p,int key)
{
    if(!p)
    {
        return -inf;
    }
    if(tr[p].key>=key)
    {
        return get_last(tr[p].l,key);
    }
    else
    {
        return max(tr[p].key,get_last(tr[p].r,key));
    }
}
int get_next(int p,int key)
{
    if(!p)
    {
        return inf;
    }
    if(tr[p].key<=key)
    {
        return get_next(tr[p].r,key);
    }
    else
    {
        return min(tr[p].key,get_next(tr[p].l,key));
    }
}
int main()
{
    //...(-.-)zZ  我是一个懒人
    return 0;
}

dijkstra

C++
#include <bits/stdc++.h>

using namespace std;
long long n, m, s;

struct Edge
{
    long long to,w;
    bool operator<(const Edge &s) const
    {
        return w>s.w;
    }
};

bool S[10001]; 
vector<Edge> G[10001]; 
long long dis[10001];

long long dijkstra(long long start)
{

    priority_queue<Edge> q;
	
	q.push({start, 0});
    for(long long i=1;i<=n;i++)
    {
        dis[i]=INT_MAX;
    }

    dis[start]=0;
	while (q.size()>0) 
    {
		Edge a=q.top();
		q.pop();
		
		long long u=a.to;
		if(S[u]) 
        {
            continue;
        }
		S[u]=1;
		for (int i=0;i<G[u].size();i++) 
        {
			int v = G[u][i].to;
			if (S[v] == 0) {
				dis[v] = min(dis[v],G[u][i].w+a.w);
				q.push({v,dis[v]});
			}
		}
	}
    return 0;
}

int main()
{
    cin>>n>>m>>s;
    for(long long i=0;i<m;i++)
    {
        long long u,v,w;
        cin>>u>>v>>w;
        G[u].push_back({v,w});
    }
    dijkstra(s);
    for(long long i=1;i<=n;i++)
    {
        cout<<dis[i]<<" ";
    }
    return 0;
}

欧拉回路

C++
#include<bits/stdc++.h>

using namespace std;

const int N=505;

int n,ca;
int e[N][N],du[N];
int ans[1205];

void dfs(int x)
{
    for(int i=1;i<=500;i++)
    {
        if(e[x][i])
        {
            e[x][i]--;
            e[i][x]--;
            dfs(i);
        }
    }
    ans[++ca]=x;
}

int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int a,b;
        cin>>a>>b;
        e[a][b]++;
        e[b][a]++;
        du[a]++;
        du[b]++;
    }
    int cnt=0,mn=501;
    for(int i=1;i<=500;i++)
    {
        if(du[i]%2==1)
        {
            cnt++;
            mn=min(mn,i);
        }
    }
    if(cnt==0)
    {
        for(int i=1;i<=500;i++)
        {
            if(du[i])
            {
                dfs(i);
            }
        }
    }
    else
    {
        dfs(mn);
    }
    for(int i=n+1;i>=1;i--)
    {
        cout<<ans[i]<<endl;
    }
    return 0;
}

线段树

C++
#include<bits/stdc++.h>

using namespace std;

int a[100050];

struct node 
{
    int l,r;
    long long v,lazy;
}tr[400050];
void build(int l,int r,int p) 
{
    tr[p].l=l;
    tr[p].r=r;
    if(l==r) 
    {
        tr[p].v=a[l];
        return;
    }
    build(l,(l+r)/2,p*2);
    build((l+r)/2+1,r,p*2+1);
    tr[p].v=tr[p*2].v+tr[p*2+1].v;
    return;
}

void down(int p) 
{
    if(tr[p].lazy)
    {
        tr[p*2].lazy+=tr[p].lazy;
        tr[p*2+1].lazy+=tr[p].lazy;
        tr[p*2].v+=(tr[p*2].r-tr[p*2].l+1)*tr[p].lazy;
        tr[p*2+1].v+=(tr[p*2+1].r-tr[p*2+1].l+1)*tr[p].lazy;
        tr[p].lazy=0;
    }

    return;
}

long long interval_count(int p, int l, int r) {
    if (tr[p].l>r||tr[p].r<l)
    {
        return 0;
    }
    if (tr[p].l>=l&&tr[p].r<=r)
    {
        return tr[p].v;
    }
    down(p);
    return interval_count(p*2,l,r)+interval_count(p*2+1,l,r);
}


void interval_add(int p, int l, int r, int x) {
    if (tr[p].l>r||tr[p].r<l) 
    {
        return;
    }

    if (tr[p].l>=l&&tr[p].r<=r) 
    {
        tr[p].lazy+=x;
        tr[p].v+=(tr[p].r-tr[p].l+1)*x;
        return;
    }
    down(p);
    interval_add(p*2,l,r,x);
    interval_add(p*2+1,l,r,x);
    tr[p].v=tr[p*2].v+tr[p*2+1].v;
    return;
}

int main() 
{

}

可持久化FHQ

C++
#include<bits/stdc++.h>
using namespace std;
const long long N=5e5+10;
long long n;
struct FHQ
{
    struct node
    {
        long long l,r;
        long long val;
        long long siz;
        long long rnd;
    }tr[N*50];
    long long seg;
    long long rt[N*50];
    void push_up(long long k)
    {
        tr[k].siz=tr[tr[k].l].siz+tr[tr[k].r].siz+1;
        return;
    }
    void newp(long long &k,long long v)
    {
        k=++seg;
        tr[k].val=v;
        tr[k].rnd=rand();
        tr[k].siz=1;
    }
    long long merge(long long x,long long y)
    {
        if(!x||!y)return x+y;
        if(tr[x].rnd>=tr[y].rnd)
        {
            long long p=++seg;
            tr[p]=tr[x];
            tr[p].r=merge(tr[p].r,y);
            push_up(p);
            return p;
        }
        else
        {
            long long p=++seg;
            tr[p]=tr[y];
            tr[p].l=merge(x,tr[p].l);
            push_up(p);
            return p;
        }
    }
    void split(long long r,long long v,long long &x,long long &y)
    {
        if(!r)
        {
            x=y=0;
            return;
        }
        if(tr[r].val<=v)
        {
            x=++seg;
            tr[x]=tr[r];
            split(tr[x].r,v,tr[x].r,y);
            push_up(x);
        }
        else
        {
            y=++seg;
            tr[y]=tr[r];
            split(tr[y].l,v,x,tr[y].l);
            push_up(y);
        }
    }
    void ins(long long &r,long long v)
    {
        long long x=0,y=0,z=0;
        newp(z,v);
        split(r,v,x,y);
        r=merge(merge(x,z),y);
        return;
    }
    void del(long long &r,long long v)
    {
        long long x=0,y=0,z=0;
        split(r,v,x,z);
        split(x,v-1,x,y);
        y=merge(tr[y].l,tr[y].r);
        r=merge(merge(x,y),z);
        return;
    }
    long long find_val(long long r,long long rk)
    {
        if(tr[tr[r].l].siz>=rk)
        {
            return find_val(tr[r].l,rk);
        }
        else if(tr[tr[r].l].siz+1==rk)
        {
            return tr[r].val;
        }
        else 
        {
            return find_val(tr[r].r,rk-tr[tr[r].l].siz-1);
        }
    }
    long long find_rank(long long r,long long v)
    {
        if(!r)
        {
            return 0;
        }
        if(tr[r].val<v)
        {
            return tr[tr[r].l].siz+1+find_rank(tr[r].r,v);
        }
        else
        {
            return find_rank(tr[r].l,v);
        }
    }
    long long find_pre(long long r,long long v)
    {
        return find_val(r,find_rank(r,v));
    }
    long long find_suf(long long r,long long v)
    {
        return find_val(r,find_rank(r,v+1)+1);
    }
}fhq;

树状数组

C++
#include <iostream>

using namespace std;

#define int long long

int c[100005];

int lowbit(int x)
{
    return x&(-x);
}

void add(int x,int y)
{
    for(;x>0;x-=lowbit(x))
    {
        c[x]+=y;
    }
}

int sum(int x)
{
    int ans=0;
    for(;x>0;x-=lowbit(x))
    {
        ans+=c[x];
    }
    return ans;
}

signed main()
{

    return 0;
}
原文