跳到正文

个人记录

板子

wurang约 26 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 26 分钟

版本

  1. v2当前
  2. v1

数据结构

栈

stack<int>s

队列

queue<int>q

deque<int>q

priority_queue<int>q

C++
struct hcx
{
    int val;// 关键字
    bool operator < (const node &a) const // 固定 < 号
    {
        return a.val < val;
        // 即堆头是最大值
    }
};

链表

单向链表模板:

C++
struct Node
{
    int data;
    Node *next;
    Node *right;
}

void insertNode(int i, Node *p) {
  Node *node = new Node;
  node->value = i;
  node->next = p->next;
  p->next = node;
}

void deleteNode(Node *p) {
  p->value = p->next->value;
  Node *t = p->next;
  p->next = p->next->next;
  delete t;
}

并查集

C++
struct DSU
{
	int fa[200005];
	int siz[200005];
	DSU(){for(int i = 1; i <= 200000; i++)fa[i] = i,siz[i] = 1;}
	int find(int x){return fa[x] == x ? x : fa[x] = find(fa[x]);}
	void unite(int x, int y)
	{
        x = find(x), y = find(y);
		if(x == y)return;
		if (siz[x] < siz[y]) swap(x, y);
    	siz[x] += siz[y];
    	fa[y] = x;
	}
}dsu;

线段树(区间加乘)

C++
struct node
{
    int sum, lz_add, maxx, minn;
    // 区间总和, 懒标记, 区间最大值, 区间最小值
    int lz_mul; // 乐子标记(乘)
    int l, r;
    // 区间左右端点下标
}t[400005];

void build(int l, int r, int p)
// 建树
{
    t[p].l = l, t[p].r = r; // 记录下标
	t[p].lz_mul = 1, t[p].lz_add = 0; // 初始化懒标记
    if(l == r)
    {
        t[p].sum = a[l] % mod; // 对于叶子节点存储单点权值,取模 p
        return ;
    }
    int mid = (l + r) >> 1; // 分化左右子节点
    build(l, mid, p << 1); build(mid + 1, r, p << 1 | 1); // 递归建树
    t[p].sum = (t[p << 1].sum + t[p << 1 | 1].sum) % mod; // 计算区间总和,取模 p
}

void push_down(int p)
// 下传懒标记
{
	t[p<<1].sum = (t[p<<1].sum * t[p].lz_mul + t[p].lz_add * (t[p<<1].r - t[p<<1].l + 1)) % mod; // 左儿子
	t[p<<1].lz_mul = (t[p<<1].lz_mul * t[p].lz_mul) % mod; // 左儿子乘懒标记
	t[p<<1].lz_add = (t[p<<1].lz_add * t[p].lz_mul + t[p].lz_add) % mod; // 左儿子加懒标记
	t[p<<1|1].sum = (t[p<<1|1].sum * t[p].lz_mul + t[p].lz_add * (t[p<<1|1].r - t[p<<1|1].l + 1)) % mod; // 右儿子
	t[p<<1|1].lz_mul = (t[p<<1|1].lz_mul * t[p].lz_mul) % mod; // 右儿子乘懒标记
	t[p<<1|1].lz_add = (t[p<<1|1].lz_add * t[p].lz_mul + t[p].lz_add) % mod; // 右儿子加懒标记
	t[p].lz_mul = 1, t[p].lz_add = 0; // 清空当前节点的懒标记
    return ;
}

int getsum(int l, int r, int p)
// 区间总和计算
{
    if (t[p].l >= l && t[p].r <= r) // 若完全覆盖
        return t[p].sum; // 直接加
    push_down(p);
    int sum = 0;
    if (l <= t[p << 1].r)  // 若与左儿子有交集
        sum = (sum + getsum(l, r, p << 1)) % mod; // 递归求和,取模 p
    if (r >= t[p << 1 | 1].l) // 若与右儿子有交集
        sum = (sum + getsum(l, r, p << 1 | 1)) % mod; // 递归求和,取模 p
    return sum;
}

void add(int l, int r , int p, int val)
// 对于区间 [l,r] 中的每个数增加 val
{
    if(t[p].l >= l && t[p].r <= r) // 若完全覆盖
    {
        t[p].sum = (t[p].sum + val * (t[p].r - t[p].l + 1)) % mod; // 计算区间和,取模 p
        t[p].lz_add = (t[p].lz_add + val) % mod; // 加懒标记,取模 p
        return ; // 返回
    }
    push_down(p); // 向下传递
    if(t[p << 1].r >= l) // 若与左儿子有交集
        add(l, r, p << 1, val); // 递归
    if(t[p << 1 | 1].l <= r) // 若与右儿子有交集
        add(l, r, p << 1 | 1, val); // 递归

    t[p].sum = (t[p << 1].sum + t[p << 1 | 1].sum) % mod; // 更新当前节点的区间和,取模 p
}

void mul(int l, int r , int p, int val)
// 对于区间 [l,r] 中的每个数乘 val
{
    if(t[p].l >= l && t[p].r <= r)
    {
        t[p].sum = (t[p].sum * val) % mod; // 计算区间和,取模 p
        t[p].lz_mul = (t[p].lz_mul * val) % mod; // 乘懒标记,取模 p
        t[p].lz_add = (t[p].lz_add * val) % mod; // 乘懒标记,取模 p
        return ; // 返回
    }
    push_down(p); // 向下传递
    if(t[p << 1].r >= l) // 若与左儿子有交集
        mul(l, r, p << 1, val); // 递归
    if(t[p << 1 | 1].l <= r) // 若与右儿子有交集
        mul(l, r, p << 1 | 1, val); // 递归

    t[p].sum = (t[p << 1].sum + t[p << 1 | 1].sum) % mod; // 更新当前节点的区间和,取模 p
}

平衡树

AVL

C++
int n, root;

struct node
{
    int l, r, h, siz, x;
}t[100005];
int cnt;

inline void upd(int p){if(p == 0)return;t[p].siz = 1 + (t[p].l ? t[t[p].l].siz : 0) + (t[p].r ? t[t[p].r].siz : 0);}
inline void upd_h(int p){if(p == 0)return;t[p].h = max((t[p].l ? t[t[p].l].h : 0), (t[p].r ? t[t[p].r].h : 0)) + 1;}

void LL_rorate(int &p)
{
    int q = t[p].l;
    t[p].l = t[q].r;
    t[q].r = p;
    t[q].siz = t[p].siz;
    upd(p);
    upd_h(p);
    upd_h(q);
    p = q;
}

void RR_rorate(int &p)
{
    int q = t[p].r;
    t[p].r = t[q].l;
    t[q].l = p;
    t[q].siz = t[p].siz;
    upd(p);
    upd_h(p);
    upd_h(q);
    p = q;
}

void LR_rorate(int &p)
{
	RR_rorate(t[p].l);
	LL_rorate(p);
}

void RL_rorate(int &p)
{
	LL_rorate(t[p].r);
	RR_rorate(p);
}

void fix(int &p)
{
	if(p == 0) return;
    if(t[t[p].l].h - t[t[p].r].h == 2)
    {
        if(t[t[t[p].l].l].h >= t[t[t[p].l].r].h) LL_rorate(p);
        else LR_rorate(p);
    }
    if(t[t[p].r].h - t[t[p].l].h == 2)
    {
        if(t[t[t[p].r].r].h >= t[t[t[p].r].l].h) RR_rorate(p);
        else RL_rorate(p);
    }
    upd(p), upd_h(p);
}

void insert(int &p, int x)
{
    if(p == 0)
    {
        p = ++cnt;
        t[p].l = t[p].r = 0;
        t[p].h = 1;
        t[p].siz = 1;
        t[p].x = x;
    }
    else if(x < t[p].x)
    {
        insert(t[p].l, x);
        fix(p);
    }
    else
    {
        insert(t[p].r, x);
        fix(p);
    }
}

void del(int &p, int x)
{
    if(p == 0) return;
    if(x < t[p].x)
        del(t[p].l, x);
    else if(x > t[p].x)
        del(t[p].r, x);
    else
    {
        if(t[p].l == 0 && t[p].r == 0) p = 0;
        else if(t[p].l == 0) p = t[p].r;
        else if(t[p].r == 0) p = t[p].l;
        else
        {
            int q = t[p].l;
            while(t[q].r) q = t[q].r;
            t[p].x = t[q].x;
            del(t[p].l, t[q].x);
        }
    }
    fix(p);
}
int query_rank(int p, int x)
{
    if(p == 0) return 0;
    if(x <= t[p].x) return query_rank(t[p].l, x);
    else return query_rank(t[p].r, x) + t[t[p].l].siz + 1;
}

int find(int p, int x)
{
    if(p == 0) return 0;
    if(x == t[t[p].l].siz + 1) return t[p].x;
    if(x <= t[t[p].l].siz) return find(t[p].l, x);
    else return find(t[p].r, x - t[t[p].l].siz - 1);
}

int pre(int p, int x)
{
    if(p == 0) return 0;
    if(t[p].x >= x) return pre(t[p].l, x);
    else
    {
        int op = pre(t[p].r, x);
        return (op != 0 ? op : t[p].x);
    }
}

int suf(int p, int x)
{
    if(p == 0) return 0;
    if(t[p].x <= x) return suf(t[p].r, x);
    else
    {
        int op = suf(t[p].l, x);
        return (op ? op : t[p].x);
    }
}

treap

C++
class treap
{
	int cnt;

	struct node
	{
	    int l, r;
	    int x, val;
	    int siz;
	}t[1000005];

	public:
		treap(){t[0].l = t[0].r = t[0].siz = t[0].x = t[0].val = 0;}
		int root;
		inline void upd(int p){t[p].siz = 1 + (t[p].l ? t[t[p].l].siz : 0) + (t[p].r ? t[t[p].r].siz : 0);}

		void l_rorate(int &p)
		{
		    int q = t[p].r;
		    t[p].r = t[q].l;
		    t[q].l = p;
		    t[q].siz = t[p].siz;
		    upd(p);
		    p = q;
		}

		void r_rorate(int &p)
		{
		    int q = t[p].l;
		    t[p].l = t[q].r;
		    t[q].r = p;
		    t[q].siz = t[p].siz;
		    upd(p);
		    p = q;
		}

		void insert(int &p, int x)
		{
		    if(p == 0)
		    {
		        p = ++cnt;
		        t[p].l = t[p].r = 0;
		        t[p].x = x;
		        t[p].val = rand();
		        upd(p);
		        return;
		    }
		    t[p].siz++;
		    if(x < t[p].x)
		    {
		        insert(t[p].l, x);
		        if(t[t[p].l].val > t[p].val) r_rorate(p);
		    }
		    else
		    {
		        insert(t[p].r, x);
		        if(t[t[p].r].val > t[p].val) l_rorate(p);
		    }
		    upd(p);
		}

		void erase(int &p, int x)
		{
		    if(p == 0) return ;
		    if(x < t[p].x)
		    {
		        erase(t[p].l, x);
		        upd(p);
		    }
		    else if(x > t[p].x)
		    {
		        erase(t[p].r, x);
		        upd(p);
		    }
		    else
		    {
		        if(t[p].l == 0 || t[p].r == 0)
		            p = (t[p].l ? t[p].l : t[p].r);
		        else
		        {
		            if(t[t[p].l].val < t[t[p].r].val)
		            {
		                r_rorate(p);
		                erase(t[p].r, x);
		            }
		            else
		            {
		                l_rorate(p);
		                erase(t[p].l, x);
		            }
		        }
		        if(p) upd(p);
		    }
		}

		int query_rank(int p, int x)
		{
		    if(p == 0) return 0;
		    if(x <= t[p].x) return query_rank(t[p].l, x);
		    else return query_rank(t[p].r, x) + t[t[p].l].siz + 1;
		}

		int find(int p, int x)
		{
		    if(p == 0) return 0;
		    if(x == t[t[p].l].siz + 1) return t[p].x;
		    if(x <= t[t[p].l].siz) return find(t[p].l, x);
		    else return find(t[p].r, x - t[t[p].l].siz - 1);
		}

		int pre(int p, int x)
		{
		    if(p == 0) return 0;
		    if(t[p].x >= x) return pre(t[p].l, x);
		    else
		    {
		        int op = pre(t[p].r, x);
		        return (op ? op : t[p].x);
		    }
		}

		int suf(int p, int x)
		{
		    if(p == 0) return 0;
		    if(t[p].x <= x) return suf(t[p].r, x);
		    else
		    {
		        int op = suf(t[p].l, x);
		        return (op ? op : t[p].x);
		    }
		}
}tree;

矩阵

C++
struct Matrix
{
    int x, y;
    int a[sz][sz];
    Matrix(){memset(a, 0, sizeof(a));}

    Matrix operator*(const Matrix &T) const
    {
        Matrix res;
        res.x = x, res.y = T.y;
        int r;
        for (int i = 0; i <= x; ++i)
            for (int k = 0; k <= T.y; ++k)
            {
                r = a[i][k];
                for (int j = 0; j <= y; ++j)
                    res.a[i][j] = (res.a[i][j] + T.a[k][j] * r) % mod;
            }
        return res;
    }

    Matrix operator^(int val) const
    {
        Matrix res, bas;
        res.x = res.y = bas.x = bas.y = x;
        for (int i = 0; i <= x; ++i)
            res.a[i][i] = 1;
        for (int i = 0; i <= x; ++i)
            for (int j = 0; j <= x; ++j)
                bas.a[i][j] = a[i][j];
        while (val)
        {
            if (val & 1)
                res = res * bas;
            bas = bas * bas;
            val >>= 1;
        }
        return res;
    }
}

图论

最短路

floyd

C++
  // 关于if目前不清楚哈
	for(int k = 1; k <= n; k++)
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				/*if(i != j && i != k && j != k)*/dis[i][j]  =  min(dis[i][j],dis[i][k]+dis[k][j]);

dijkstra

C++
//无负边权
// 复杂度待补充
struct edge{int v, w;};
struct node
{
	int u, dis;
	bool operator > (const node& a) const { return dis > a.dis;}
}op;

int n, m;
int dis[10005], vis[10005];
vector<edge>e[10005];
priority_queue<node, vector<node>, greater<node> >q;

void dijkstra()
{
	memset(dis, 0x3f, sizeof(dis));
	memset(vis, 0, sizeof(vis));
	dis[1] = 0;
	q.push({1, 0});
	while(!q.empty())
	{
		op = q.top();q.pop();
		int u = op.u;
		if(vis[u])continue;
		vis[u] = 1;
		for(int i = 0; i < e[u].size(); i++)
		{
			int v = e[u][i].v,w = e[u][i].w;
			if (dis[v] > dis[u] + w)
			{
        		dis[v] = dis[u] + w;
        		q.push({v, dis[v]});
      		}
		}
	}
}

spfa

C++
//spfa单源+负环
//O(nm)
const int MAXN = 1e4 + 5;
struct edge {
	int v, w;
};
vector<edge> e[MAXN];
int dis[MAXN], cnt[MAXN], vis[MAXN];
queue<int> q;
int u,v,w;
int sum;
bool spfa(int n,int s)
{
	memset(dis, 0x3f, sizeof(dis));
	dis[s] = 0;
	q.push(s);
	while (!q.empty())
	{
		int u = q.front();q.pop();
		vis[u] = 1;
		for (int i = 0; i < e[u].size(); i++)
		{
			int v = e[u][i].v, w = e[u][i] .w;
			if (dis[v] > dis[u] + w)
			{
				dis[v] = dis[u] + w;
				cnt[v] = cnt[u] + 1;
				if (cnt[v] >= n)return false;
				// if (vis[v])continue;
				q.push(v);
				vis[v] = 1;
			}
		}
	}
	return true;
}

最小生成树

prim

C++
//prim算法,以边为基准,适用于稠密图
//与dijkstra相似,一次确定一条边
//此处为堆优化,O((n+m)log n)
struct Edge{int v,w;};
struct node
{
	int u,dis;
	bool operator>(const node& a) const { return dis > a.dis;}
}op;

int ans;
int n,m;
vector<Edge>e[105];
int a[3007][3007];
int dis[10007],vis[10007];
priority_queue<node,vector<node>,greater<node> >q;

void prim()
{
	while(!q.empty())
	{
		int now=q.top().u;q.pop();
		if(vis[now]) continue;
		vis[now]=1;
		for(int i=1;i<=n;i++)
		{
			if(i == now)continue;
			int v=i;
			if(vis[v]==0 && dis[v]>a[now][v])
			{
				dis[v]=a[now][v];
				q.push({v,dis[v]});
			}
		}
	}
}

kruskal

C++
//kruskal,瓶颈排序,m log m,适用稀疏图
//码量小,易调试
int n,m;
int ans;
struct edge
{
	int u,v,w;
}e[10005];
bool cmp(edge a,edge b){return a.w < b.w;}

int fa[150005];
int find(int x)
{
    if(fa[x] != x)
    {
        fa[x] = find(fa[x]);
    }
    return fa[x];
}

背包

分组

C++
	for (int k = 1; k <= ttt; k++)
  	{
		for (int i = W; i >= 0; i--)
		{
			for (int j = 1; j <= sum[k]; j++)
			{
				if (i >= w[t[k][j]]) dp[i] = max(dp[i],dp[i - w[t[k][j]]] + v[t[k][j]]);
			}
		}
	}

多重

C++
    for(int i = 1; i <= N; i++)
    {
        cin>>opv>>opw>>opsum;
        int t = 1;
        while(opsum > t)
        {
            n++;
            v[n] = opv*t;
            w[n] = opw*t;
			opsum-=t;
            t*=2;
        }
        if(opsum!=0)
        {
            n++;
            v[n] = opv*opsum;
            w[n] = opw*opsum;
        }
    }

快读

C++
inline 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-48;ch=getchar();}return x*f;}
原文