随笔分类 - 数据结构--------------------------
摘要:【平衡树】★平衡树 by onion_cyc 【莫队算法】 问题:给定长度为n的序列和m个区间询问,支持快速增减相邻元素维护区间信息。 将询问按左端点分块,块大小为$Q=\frac{n}{\sqrt m}$,块内按右端点排序。 然后依次回答询问,需要O(1)从(l,r)转移到(l,r+1),(l,r
阅读全文
摘要:【算法】平衡树(treap)||双向链表 【题解】treap知识见数据结构。 #include<cstdio> #include<algorithm> #include<ctime> using namespace std; const int maxn=100010,inf=0x3f3f3f3f;
阅读全文
摘要:【算法】平衡树(treap) 【题解】 treap知识见数据结构。 解法,具体细节见程序。 #include<cstdio> #include<algorithm> #include<ctime> using namespace std; const int maxn=100010; struct
阅读全文
摘要:【算法】平衡树(treap) 【题解】treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorithm> #include<ctime> using namespace std; const int maxn=5001
阅读全文
摘要:【题意】w组数据,给定n和m,给出m段区间[s,t](1<=s<=t<=n)的数字和,求是否矛盾。n<100,m<1000,w<100。 【算法】带权并查集 【题解】由于存在负数,唯一判断信息合法性的途径只有多个已知区间能组成一个已知大区间。 假设有n+1个点0~n表示sum[0]~sum[n],对
阅读全文
摘要:【算法】可并堆(左偏树) #include<cstdio> #include<algorithm> using namespace std; const int maxn=1000010; int l[maxn],r[maxn],fa[maxn],d[maxn],a[maxn],n,m; bool
阅读全文
摘要:【算法】贪心+堆 #include<cstdio> #include<algorithm> using namespace std; const int maxn=20010; int n,heap[maxn],sz; void heap_push(int x) { heap[++sz]=x;//新
阅读全文
摘要:【算法】树链剖分+线段树 【题解】 树链剖分算法:http://www.cnblogs.com/onioncyc/p/6207462.html 定义线段树结构体有l,r,lc,rc,sum,data。 lc表示左端颜色,rc表示右端颜色,sum表示颜色种类,data表示区间置为同一个数的标记。 修改
阅读全文
摘要:【算法】树链剖分+线段树 【题解】模板题,见http://www.cnblogs.com/onioncyc/p/6207462.html 调用线段数时要用新编号pos[i] !!! #include<cstdio> #include<cctype> #include<algorithm> using
阅读全文
摘要:【算法】贪心&&堆 【题解】反过来看就是合并任意两块木板,花费为木板长度之和。 显然从最小的两块开始合并即可,用堆(优先队列)维护。 经典DP问题石子归并是只能合并相邻两堆石子,所以不能贪心。 手写堆版本见http://www.cnblogs.com/onioncyc/p/6212840.html
阅读全文
摘要:【算法】线段树 #include<cstdio> #include<cctype> #include<algorithm> using namespace std; const int inf=0x3f3f3f3f,maxn=50010; struct tree{int l,r,mins,maxs;
阅读全文
摘要:【算法】(强连通分量)并查集 【题解】 1.用tarjan计算强连通分量并缩点,在新图中找入度为0的点的个数就是答案。 但是,会爆内存(题目内存限制64MB)。 2.用并查集,最后从1到n统计fa[i]==i的数量即是答案。(n个点n条有向边,连通子图个数就是答案) (tarjan) #includ
阅读全文
摘要:【算法】线段树||二分+前缀和 【题解】线段树记录区间加值和区间最大值。 #include<cstdio> #include<algorithm> using namespace std; const int maxn=1e6; struct treess{int l,r,ms,delta;}t[m
阅读全文
摘要:【算法】线段树 【题解】 学自:https://vijos.org/p/1083/solution(wang_yanheng的回答) 回溯时维护一段区间的以下域: sumL:从左端点起连续区间的最大和 sumR:从右端点起连续区间的最大和 sum:整段区间的和 subSum:最大子区间和 以上域在叶
阅读全文
摘要:【算法】线段树 【题解】区间加上同一个数+区间查询最大值。注意和谐值可以是负数,初始化ans为负无穷大。 #include<cstdio> #include<algorithm> using namespace std; const int maxn=100010,ainf=-0x3f3f3f3f;
阅读全文
摘要:【题意】两种操作,[L,R]种新的树(不覆盖原来的),或查询[L,R]树的种类数。n<=50000。 【算法】树状数组||线段树 【题解】这题可以用主席树实现……不过因为不覆盖原来的,所以有更简单的方法。 括号法,对于每个K=1的操作标记左右括号的位置。 对于每个K=2的操作,答案就是right前面
阅读全文
摘要:【算法】线段树 【题解】将所有坐标按x(第一)和y(第二)从小到大排序,再按顺序插入线段树,即在线段树中将y坐标位置+1,这样就能保证每个坐标能包含的点一定先被处理了,每次询问查询1...a[i].y区间的和。 #include<cstdio> #include<algorithm> using n
阅读全文
摘要:【算法】线段树 【注意】修改或查询区间时,若区间能包含某棵子树就立即返回,否则线段树就失去了意义。 #include<cstdio> #include<algorithm> using namespace std; const int inf=0x3f3f3f3f; struct treess{in
阅读全文
摘要:【算法】线段树 #include<cstdio> #include<algorithm> using namespace std; struct treess{int l,r,ms;}t[400010]; const int maxn=200010,inf=0x3f3f3f3f; int a[max
阅读全文
摘要:【算法】 【算法】网络流 【算法】树 【算法】数学 ————【专题】生成树计数(矩阵树定理) ————【专题】计数问题(排列组合,容斥原理,卡特兰数) ————【算法专题】卡特兰数(计数数列) ————【专题】数论 ————【专题】概率和期望 【算法】动态规划 【算法】数据结构 ————【专题】平衡
阅读全文