12 2023 档案

摘要:1.有点后序遍历的思想,就是先把左子树,右子树的结果算出来,然后合并到根节点。 2.合并时四种情况分类讨论. 3.对于遇到要找的点就可以直接返回,不管另一个点在这个点下面还是在别的子树上,都是正确的 int n, m; int a[N]; int query(int root,int x,int y 阅读全文 »
posted @ 2023-12-24 19:27 potential-star 阅读(20) 评论(0) 推荐(0) 编辑
摘要:利用前序遍历的特性,如果左子树不空,下一个一定是左节点,不然就是# 因为是转中序遍历,就在两次遍历之间输出 #include <iostream> #include <cstring> #include <algorithm> using namespace std; int k; string s 阅读全文 »
posted @ 2023-12-24 19:00 potential-star 阅读(9) 评论(0) 推荐(0) 编辑
摘要:一切的核心是怎么利用中序和按层遍历构建二叉树? 1.优化空间很大,可以提前预处理记录每个数对应的位置,还可以vis数组记录这个点是不是已经作为根了。 2.我们考虑到每次找到当前中序要处理区间,里面的数记为集合mid,我们从前到后看层序遍历中的哪个数最先出现在mid中。那么这个数就是当前中序处理区间的 阅读全文 »
posted @ 2023-12-24 18:24 potential-star 阅读(4) 评论(0) 推荐(0) 编辑
摘要:1.就算不知道用vector的初始化,也可以手动赋值创建子数组。 2.不断找到当前序列对应的根节点,计算他的子节点的总和,在这样递归处理过程中,注意要中序输出,所以对于是先遍历完左子树,然后输出答案,然后遍历右子树 #include <bits/stdc++.h> using namespace s 阅读全文 »
posted @ 2023-12-24 15:31 potential-star 阅读(7) 评论(0) 推荐(0) 编辑
摘要:https://www.acwing.com/problem/content/3601/ 不断找新的先序的根节点,根据位置切割中序,根据中序左右子树大小反切割先序,找到左子树对应的先序中序,然后递归处理 #include<stdio.h> #include<vector> #include<map> 阅读全文 »
posted @ 2023-12-24 15:18 potential-star 阅读(4) 评论(0) 推荐(0) 编辑
摘要:hh的项链:不带修改维护区间种类数 https://www.luogu.com.cn/problem/P1972 树状数组做法 https://zhuanlan.zhihu.com/p/272804539 #include<bits/stdc++.h> using namespace std; in 阅读全文 »
posted @ 2023-12-22 22:34 potential-star 阅读(8) 评论(0) 推荐(0) 编辑
摘要:输出一个整数,表示a*b mod p的值。 数据范围 1≤a,b,p≤1018 ll qadd(ll a, ll b, ll p) { ll res = 0; while (b) { if (b & 1) res = (res + a) % p; a = (a + a) % p; b >>= 1; 阅读全文 »
posted @ 2023-12-16 20:23 potential-star 阅读(114) 评论(0) 推荐(0) 编辑
摘要:SDUT 校赛题目 Description 给定正整数 n,计算 n 个元素的集合 {1,2,,n},所有非空子集和的乘积取模 998244353 后的结果。 Input 一个正整数 n (1n200),代 阅读全文 »
posted @ 2023-12-14 15:30 potential-star 阅读(72) 评论(0) 推荐(0) 编辑
摘要:给定一棵有 n 个点的树,询问树上距离为 k 的点对是否存在。 第一行两个数 n,m,n个点。 第 2 到第 n 行,每行三个整数 u,v,w,代表树上存在一条连接 uv 边权为 w 的路径。 接下来 m 行, 阅读全文 »
posted @ 2023-12-10 03:39 potential-star 阅读(9) 评论(0) 推荐(0) 编辑
摘要:树上启发式合并(常常也叫DSU On Tree,但其实和DSU并没有特别大关系),是一种解决某些树上离线问题的算法,尤其常被用于解决“对每个节点,询问关于其子树的某些信息”这样的问题。 假设我们要对树上的每个节点p求ans[p] ,且这个ans[p] 可以通过合并p的子节点的某些信息得知,一般来说我 阅读全文 »
posted @ 2023-12-10 03:29 potential-star 阅读(47) 评论(0) 推荐(0) 编辑
摘要:已知一棵包含 N 个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 1 x y z,表示将树从 xy 结点最短路径上所有节点的值都加上 z。 2 x y,表示求树从 xy 结点最短路径上所有节点的值之和。 3 x z,表 阅读全文 »
posted @ 2023-12-10 03:16 potential-star 阅读(6) 评论(0) 推荐(0) 编辑
摘要:N,M,S,分别表示树的结点个数、询问的个数和树根结点 理论时间复杂度上界就是O(2n+mlogn) const int N=500010; int n,m,s,a,b; vector<int> e[N]; int fa[N],son[N],dep[N],siz[N]; int top[N]; vo 阅读全文 »
posted @ 2023-12-10 03:02 potential-star 阅读(13) 评论(0) 推荐(0) 编辑
摘要:给定 n×n 的矩阵 A,求 A^k。 typedef long long LL; const int mod=1000000007; struct matrix{ LL c[101][101]; matrix(){memset(c, 0, sizeof c);} } A, res; LL n, k 阅读全文 »
posted @ 2023-12-10 02:51 potential-star 阅读(3) 评论(0) 推荐(0) 编辑
摘要:N≤400,所有 0≤aij<1e9+7 const int N=405,P=1e9+7; int n; LL a[N][N<<1]; LL quickpow(LL a, LL b){ LL ans = 1; while(b){ if(b & 1) ans = ans*a%P; a = a*a%P; 阅读全文 »
posted @ 2023-12-10 02:42 potential-star 阅读(20) 评论(0) 推荐(0) 编辑
摘要:给出一组包含 m 个不等式,有 n 个未知数的形如: \[\begin{cases} x_{c_1}-x_{c'_1}\leq y_1 \x_{c_2}-x_{c'_2} \leq y_2 \ \cdots\ x_{c_m} - x_{c'_m}\leq y_m\end{cas 阅读全文 »
posted @ 2023-12-10 02:31 potential-star 阅读(9) 评论(0) 推荐(0) 编辑
摘要:给定 n 个整数构成的序列 a,将对于指定的闭区间 [l,r] 查询其区间内的第 k 小值。 题目一开始的离散化复杂度为O(nlogn),构建基础主席树复杂度为O(nlogn),统计并插入的复杂度是O(nlogn+nlogn)=O(nlogn),询问的 阅读全文 »
posted @ 2023-12-10 02:15 potential-star 阅读(6) 评论(0) 推荐(0) 编辑
摘要:最大异或和 给定一个非负整数序列 {a},初始长度为 N。 有 M 个操作,有以下两种操作类型: A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N1。 Q l r x:询问操作,你需要找到一个位置 p,满足 \(l \l 阅读全文 »
posted @ 2023-12-10 01:54 potential-star 阅读(12) 评论(0) 推荐(0) 编辑
摘要:题目描述 给出两个字符串 s1s2,若 s1 的区间 [l,r] 子串与 s2 完全相同,则称 s2s1 中出现了,其出现位置为 l。 现在请你求出 s2s1 中所有出现的位置。 定义 阅读全文 »
posted @ 2023-12-10 01:31 potential-star 阅读(9) 评论(0) 推荐(0) 编辑
摘要:给定一个长度为 n 的整数数列,请你计算数列中的逆序对的数量。每个数字不超过1e9。 int n, m; int a[N]; int tr[N]; vector<int>lan; int lowbit(int x){ return x&(-x); } void discrete() { sort(l 阅读全文 »
posted @ 2023-12-09 22:51 potential-star 阅读(9) 评论(0) 推荐(0) 编辑
摘要:const int N = 1e5 + 10; int n, m; int a[N]; struct Tree{ int l,r; ll sum,add; }tr[4*N]; void build(int u,int l,int r){ // l=tr[u].l;r=tr[u].r; //注释掉的部 阅读全文 »
posted @ 2023-12-09 21:56 potential-star 阅读(6) 评论(0) 推荐(0) 编辑
摘要:对于种类并查集主要是考虑清楚到根节点距离分为几类,每一类的意义 有的题目相出d数组的含义才能想到用带权并查集 //find函数需要变化 int find(int x) { if (p[x] != x) { int root = find(p[x]); d[x] += d[p[x]]; p[x] = 阅读全文 »
posted @ 2023-12-09 21:24 potential-star 阅读(11) 评论(0) 推荐(0) 编辑
摘要:维护一个字符串集合,支持两种操作: I x 向集合中插入一个字符串 x; Q x 询问一个字符串在集合中出现了多少次。 所有输入的字符串总长度不超过 105( 也就是节点数) const int N=100010; int n; char s[N]; int ch[N][26],c 阅读全文 »
posted @ 2023-12-09 16:58 potential-star 阅读(10) 评论(0) 推荐(0) 编辑
摘要:https://zhuanlan.zhihu.com/p/646586178 待补 阅读全文 »
posted @ 2023-12-09 16:52 potential-star 阅读(12) 评论(0) 推荐(0) 编辑
摘要:单哈希且用自然溢出代替取模操作,常数小但是容易被卡 单字符串区间内比较,查询子串hash值 typedef unsigned long long ULL; const int N = 100010, P = 131; int n, m; char str[N]; ULL h[N], p[N]; UL 阅读全文 »
posted @ 2023-12-09 15:34 potential-star 阅读(13) 评论(0) 推荐(0) 编辑
摘要:时间复杂度O(nm^2),理论上限 //n,m,s,t,分别代表该网络的点数n,网络的边数m,源点编号s,汇点编号t。 const int N=5010,M=100010,INF=1e8; int n,m,S,T; struct edge{int v,c,w,ne;}e[M]; int h[N],i 阅读全文 »
posted @ 2023-12-09 13:48 potential-star 阅读(68) 评论(0) 推荐(0) 编辑
摘要:时间复杂度为Θ(n^3) const int inf =0x3f3f3f3f; const int N=505; long long w[N][N]; long long la[N],lb[N]; bool va[N],vb[N]; long long match[N]; long long n,m 阅读全文 »
posted @ 2023-12-09 13:25 potential-star 阅读(7) 评论(0) 推荐(0) 编辑
摘要:#define LL long long #define N 10010 #define M 200010 using namespace std; int n,m,S,T; //n,m,s,t,分别表示点的个数、有向边的个数、源点序号、汇点序号 struct edge{LL v,c,ne;}e[M 阅读全文 »
posted @ 2023-12-09 02:50 potential-star 阅读(6) 评论(0) 推荐(0) 编辑
摘要:二分图最大匹配可以转换成网络流模型。 将源点连上左边所有点,右边所有点连上汇点,容量皆为1。原来的每条边从左往右连边,容量也皆为1,最大流即最大匹配。 如果使用 Dinic 算法 求该网络的最大流,可在O(sqrt(n) * m)求出。 #define N 1010 #define M 200001 阅读全文 »
posted @ 2023-12-09 02:33 potential-star 阅读(142) 评论(0) 推荐(0) 编辑
摘要:scc:极大的强连通子图(两两相互可达) const int N=10010; int n,m,a,b; vector<int> e[N]; int dfn[N],low[N],tot; int stk[N],instk[N],top; int scc[N],siz[N],cnt; void tar 阅读全文 »
posted @ 2023-12-09 01:59 potential-star 阅读(7) 评论(0) 推荐(0) 编辑
摘要:单测试点有多组测试数据,注意fill手动清空 int e[M],ne[M],w[M],h[N],idx; int d[N],cnt[N],vis[N]; //为了卡常用spfa的时候就用链式前向星 void init(){ fill(h,h+n+1,-1);idx=0; } void add(int 阅读全文 »
posted @ 2023-12-09 00:54 potential-star 阅读(7) 评论(0) 推荐(0) 编辑
摘要:染色法判别二分图 —— 模板题 AcWing 860. 染色法判定二分图 时间复杂度是 O(n+m), n 表示点数,m 表示边数 bool dfs(int u,int c){//判断存在奇环,存在返回true color[u]=c; for(auto v:e[u]){ if 阅读全文 »
posted @ 2023-12-08 23:14 potential-star 阅读(6) 评论(0) 推荐(0) 编辑
摘要:假设 n 表示图中点数,m 表示图中边数。 Prim算法堆优化 时间复杂度 O(nlogn)。 核心思想:每次挑一条与当前集合相连的最短边。 code int ans,cnt; struct edge{int v,w;}; vector<edge> e[N]; int d[N 阅读全文 »
posted @ 2023-12-08 22:20 potential-star 阅读(6) 评论(0) 推荐(0) 编辑
摘要:const int N = 100010; int n,m,a,b; vector<int> e[N], tp; int din[N];//入度数组 bool toposort(){ queue<int> q; for(int i = 1; i <= n; i++) if(din[i]==0) q. 阅读全文 »
posted @ 2023-12-08 22:10 potential-star 阅读(5) 评论(0) 推荐(0) 编辑
摘要:单源最短路: 求一个点到其他点的最短路 多源最短路: 求任意两个点的最短路 稠密图用邻接矩阵存,稀疏图用邻接表存储。 稠密图: m 和 n2 一个级别 稀疏图: m 和 n 一个级别 朴素dij: int n,m,s,a,b,c; const int N=100010; struct edge{in 阅读全文 »
posted @ 2023-12-07 23:10 potential-star 阅读(12) 评论(0) 推荐(0) 编辑
摘要:https://blog.csdn.net/Xavier_97/article/details/126931927 由于很玄学,我们考虑统一使用库函数round和自己手写round来实现最终输出整数的四舍五入和小数保留k位的四舍五入 #include <iostream> #include <cma 阅读全文 »
posted @ 2023-12-07 15:09 potential-star 阅读(32) 评论(0) 推荐(0) 编辑
摘要:无向图无环的,无环的图总是可以展成树的形式。 然后考虑无向图,顶多有回边指向父节点。不可能有回边指向祖先。更不可能有横向边,因为无环啊。所以只需要考虑指向父节点的回边就可以无需使用Vis。 阅读全文 »
posted @ 2023-12-05 22:05 potential-star 阅读(12) 评论(1) 推荐(0) 编辑
摘要:请你在树中找到一个点,使得该点到树中其他结点的最远距离最近。这个点被称为树的中心。 题解:https://www.cnblogs.com/dx123/p/17302104.html 评测:https://www.acwing.com/problem/content/1075/ 暴力做法是以每个点为根 阅读全文 »
posted @ 2023-12-05 22:00 potential-star 阅读(39) 评论(0) 推荐(1) 编辑
摘要:树上任意两节点之间最长的简单路径即为树的「直径」。 树形 DP的做法 可以在存在负权边的情况下求解出树的直径。 const int N=10010,M=20010; int n,a,b,c,ans; struct edge{int v,w;}; vector<edge> e[N]; int dfs( 阅读全文 »
posted @ 2023-12-05 20:44 potential-star 阅读(52) 评论(0) 推荐(0) 编辑
摘要:const int N=100010; int n, a, b; vector<int> e[N];//用vector作邻接表 int siz[N], pos, ans=1e9; void dfs(int x, int fa){ siz[x]=1; int mx=0; for(auto y : e[ 阅读全文 »
posted @ 2023-12-05 19:52 potential-star 阅读(5) 评论(0) 推荐(0) 编辑
摘要:https://www.luogu.com.cn/problem/P4316 本题暂时只写了用期望dp经典套路,套上期望DP的基本套路,设dp(u)为到达u点的期望长度。 期望dp,也叫概率dp 一般来说,期望dp找到正确的状态后,转移是比较容易想到的。 但一般情况下,状态一定是“可数”的 事实上, 阅读全文 »
posted @ 2023-12-05 00:56 potential-star 阅读(18) 评论(0) 推荐(0) 编辑
摘要:https://www.acwing.com/problem/content/description/4012/ Acwing long double卡常,注意cin读小数。 #include <bits/stdc++.h> using namespace std; #define ll long 阅读全文 »
posted @ 2023-12-04 23:11 potential-star 阅读(12) 评论(0) 推荐(0) 编辑
摘要:void floyd() { for (int k = 1; k <= n; k ++ ) for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); 阅读全文 »
posted @ 2023-12-03 21:48 potential-star 阅读(4) 评论(0) 推荐(0) 编辑
摘要:struct DSU { vector<int> f, siz; DSU() {} DSU(int n) { init(n); } void init(int n) { f.resize(n); std::iota(f.begin(), f.end(), 0); siz.assign(n, 1); 阅读全文 »
posted @ 2023-12-03 21:01 potential-star 阅读(22) 评论(0) 推荐(0) 编辑
摘要:https://www.acwing.com/activity/content/competition/problem_list/3648/ B题收获: 1.利用题目告诉的结论:1e9范围质数之差小于300 2.一个数不被2-a的任何数整除 等价于他的最小质因子需要大于a c题:初步宏观思路:不难想 阅读全文 »
posted @ 2023-12-03 20:53 potential-star 阅读(5) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示