数据结构
SolarDomo
每天都被头像萌醒
展开
-
HDU 4812 D Tree 点分治 + 逆元
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5977题目要求在树上找到一条链使得这条链上的点的乘积模mod等于k,求链首尾字典序最小的一条看到题目就能知道是一个点分治的题目,将树按照重心分治之后,就是要统计以重心为根的子树中,过树根的mod为k的链字典序最小的一条,这里的统计必需是在时间复杂度O(n) 以下才能过 代码:#pragma commen原创 2016-12-07 13:02:08 · 381 阅读 · 0 评论 -
CodeForces 570C Replacement 线段树
题目链接:http://codeforces.com/problemset/problem/570/C单点更新区间的状态,并记录下每一个子区间左边,右边有多少个’.’,合并的时候要注意一下就可以了代码:#include <bits/stdc++.h>#define sf scanf#define pf printfusing namespace std;const int maxn = 300原创 2016-11-04 15:03:49 · 316 阅读 · 0 评论 -
CodeForces 459D Pashmak and Parmida's problem 树状数组 + 离散化
题目链接:http://codeforces.com/problemset/problem/459/D将序列离散化之后,很方便就可以处理出f值 逆序将后缀F值加入树状数组中,同时更新ans值代码:#include <bits/stdc++.h>#define sf scanf#define pf printfusing namespace std;typedef long long LL;原创 2016-11-04 14:59:46 · 365 阅读 · 0 评论 -
SPOJ Count on a tree 在线LCA + 主席树
题意:求树上a , b路径上第k大的点 主席树a + 主席树b - 主席树(LCA(a,b)) - 主席树(FA(LCA(a,b)))代码:#include <bits/stdc++.h>#define sf scanf#define pf printfusing namespace std;const int maxn = 100000 + 50;/** 主席树 */const in原创 2016-10-13 22:12:50 · 336 阅读 · 0 评论 -
UVA 11922 Permutation Transformer Splay 区间翻转 + 区间合并
题目链接:http://vjudge.net/problem/18902/origin把一个区间翻转后拿出来加到序列的后面代码:#include <bits/stdc++.h>#define sf scanf#define pf printfusing namespace std;const int maxn = 100000 + 50;/** Splay */int ch[maxn][2原创 2016-10-13 17:12:53 · 352 阅读 · 0 评论 -
HDU 5923 Prediction 并查集 合并
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5923代码#include <bits/stdc++.h>#define sf scanf#define pf printfusing namespace std;const int maxn = 500 + 5,maxm = 10000 + 50;int Fa[maxm][maxn],n,m原创 2016-10-13 11:55:21 · 397 阅读 · 0 评论 -
树链剖分模板
const int maxn = 100000 + 5; //树中节点个数//边集struct Edge{ int v,pre,c;}Es[maxn * 2];int head[maxn],TOT_EDGE;void INIT_EDGE(){ //边集初始化 memset(head,-1,sizeof head);原创 2016-09-19 20:01:42 · 253 阅读 · 0 评论 -
Treap 模板
/**** ch:节点的孩子节点** fa:夫节点** fix;优先级** size:当前子树的大小** tot:节点个数(包括sroot)** sroot:超级根节点** key:节点的键值*/const int maxn = 15000 + 50,INF = 0x7fffffff;int ch[maxn][2],fa[maxn],fix[maxn],size[m原创 2016-09-26 10:09:22 · 239 阅读 · 0 评论 -
伸展树模板
已过的题:HDU 4585/** ---- SPLAY ---- **/int ch[maxn][2],fa[maxn],size[maxn];int key[maxn],pos[maxn];int tot,root,sroot;void Splay_Init(){ tot = 1; root = sroot = 0; ch[0][0] = ch[0][1] = 0;原创 2016-09-26 11:20:03 · 385 阅读 · 0 评论 -
POJ 1741 Tree 点分治
题目链接:http://poj.org/problem?id=1741题目链接: 要求出树中相距距离小于等于k的二元组个数 树分治的入门题:看看漆子超的论文: http://wenku.baidu.com/link?url=7KOPn20aLvKK5PqDmuLjIyj4sqZ6CL1H9qP__JSGvX-AWgX7LR6gC-BZ3PTVCP2ojBHxKZcJ5U3csiRjuspqcoF原创 2016-10-18 12:26:04 · 309 阅读 · 0 评论 -
POJ 3264 RMQ 分块算法
题目连接:http://poj.org/problem?id=3264分块算法:将N个数 每sqrt(N)个数分成一个块,每一个块中加入一个value域保存这个块的最大值,查找的时候左右两个块在原序列上遍历查找最大值,中间的区间直接在预处理的块上查找最大值, 复杂度为:O(sqrt(N)) 代码:#include <cstdio>#include <cmath>#include <algor原创 2016-10-29 16:59:30 · 612 阅读 · 0 评论 -
HDU 4123 Bob’s Race 树形DP + RMQ
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123代码://#include <bits/stdc++.h>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int maxn = 50000 + 5;int max(int a,int原创 2016-12-05 16:55:27 · 413 阅读 · 0 评论 -
POJ 1195 Mobile phones 二维线段树
题目连接:http://poj.org/problem?id=1195 二维线段树 单点更新 区间查询 代码:#include <cstdio>#include <cstring>using namespace std;const int maxn = 1000 + 50;int sum[4 * maxn][4 * maxn];#define lson rt << 1,l,mid#d原创 2016-12-04 16:22:05 · 384 阅读 · 0 评论 -
HDU 3415 Max Sum of Max-K-sub-sequence 单调队列
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3415 题意:求序列中长度不大于k的连续子序列的最大和使多少 我们可以用ANS[i] 表示 以第i个数为右边界的一个K区间的最大和的值 那么有方程 显然这里可以用一个长度为k的单调递增队列来维护最小值代码:#include <bits/stdc++.h>#define sf scanf#def原创 2016-11-02 21:15:19 · 295 阅读 · 0 评论 -
FZU 1894 志愿者选拔 单调队列
题目连接: http://acm.fzu.edu.cn/problem.php?pid=1894有出队入、队操作,查找最大值 操作直接用单调队列模拟代码:#include <cstdio>#define sf scanf#define pf printfusing namespace std;const int maxn = 1000000 + 5;int q[maxn],v[maxn];原创 2016-11-02 21:08:38 · 373 阅读 · 0 评论 -
HDU 5945 Fxx and game 单调队列优化DP
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5945题意:数x, 1.如果x%k == 0 ,那么可以对x除以k 2.对x减去包括t以内的任意一个数 问多少步操作能使x变为0dp[j] 表示 j最少能在多少步以内达到1 那么有转移公式 1. 2. 第一个转移方程可以用一个单调递增队列来维护 这样总的复杂度就是O(X)的了代码:#inc原创 2016-11-02 17:26:08 · 287 阅读 · 0 评论 -
HDU 1521 Monkey King 左偏树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意: N个猴子开始互相不认识,而且每个猴子有一个初始的战斗力。 每次两个猴子遇到后,如果互不认识,就是找自己认识的战斗力最强的猴子打架。每个猴子打架之后战斗力就会减半。之后,他们两队人就算是认识了。 给出M个询问,不认识就输出合为一对之后的最大战斗力。认识输出-1原创 2016-10-08 16:03:34 · 247 阅读 · 0 评论 -
HDU 1394 Minimum Inversion Number 线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394代码:#include <bits/stdc++.h>#define sf scanf#define pf printfusing namespace std;/** 线段树模板 *//** 以求区间和为例 */#define lson rt << 1 , l ,mid#define原创 2016-10-08 10:51:29 · 249 阅读 · 0 评论 -
HDU 5692 Snacks DFS序 + 线段树区间更新
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5692 题意:问一个子树中,到树的根节点路径上节点最大和是多少这样可以用DFS序来维护这个树结构, 将一棵子树映射到一段区间类,区间每个树存的是相应节点到根的路径节点和,那么显然可以用一个线段树来维护这个区间的最大值。 在更新节点值的时候,我们只需要将这个节点为根的子树映射到的区间更新即可 代码:#原创 2016-10-10 14:22:14 · 293 阅读 · 0 评论 -
线段树 区间更新模板
测试题目:HDU 1166,/** 线段树模板 *//** 以求区间和为例 */#define lson rt << 1 , l ,mid#define rson rt << 1 | 1,mid + 1,rconst int maxn = 50000 + 50;int v[maxn << 2],laze[maxn << 2];int A[maxn];// 向下Push laze标记vo原创 2016-10-08 10:16:22 · 252 阅读 · 0 评论 -
HDU 2665 Kth number 主席树模板题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2665主席树模板题代码 :#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define sf scanf#define pf printf#define totNode maxn * 60原创 2016-10-12 19:56:28 · 352 阅读 · 0 评论 -
HDU 4585 Shaolin Splay 寻找前驱后继
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 每次添加一个数在集合中,并寻找这个数在集合排序后的前驱和后继 Splay模板题 每次将节点插入后 查找根左子树最右节点 和 根右子树最左节点代码:#include <bits/stdc++.h>#define sf scanf#define pf printfusing namespa原创 2016-09-26 11:02:40 · 514 阅读 · 0 评论 -
POJ 3468 A Simple Problem with Integers 线段树 区间更新
POJ 3468 A Simple Problem with Integers: http://poj.org/problem?id=3468 区间更新 和 区间求和代码:#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#define sf scanf#define pf printf原创 2016-08-12 21:58:22 · 307 阅读 · 0 评论 -
HDU 1698 Just a Hook 线段树区间更新
HDU 1698 Just a Hook:http://acm.hdu.edu.cn/showproblem.php?pid=1698 √ 题意:问一个数组中改变一段区间的数的大小 最后求争端区间的和 模板题代码:#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#define sf s原创 2016-08-12 21:57:10 · 396 阅读 · 0 评论 -
POJ 2492 二分图判断 并查集
POJ - 2492 题意:给出N个BUG 和 M个BUG的性关系 判断是否有同性关系可以用二分图判断来做 也可以用并查集来做 明天补上并查集二分图:如果这N个BUG间只有异性间有关系 那么对这N个BUG做一个关系图 同性之间的BUG之间没有联系 联系建立在不同的性别之间 可以将点集分成两个类别 A B 集合内部没有联系 则这个图为二分图 于是使用二分图判断的常用方法,图染色法 两种颜色将原创 2016-07-27 21:03:30 · 1428 阅读 · 0 评论 -
HDU 5289 Assignment ST + 二分区间
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意: 求多少个区间[l,r] 有MAX[l,r] - MIN[l,r] < k 令F[l,r] = MAX[l,r] - MIN[l,r] 对于固定的左区间l,F[l,r]是一个非减序列,所以我们可以二分找到小于K的最右边界 ST预处理一边就可以在O(1)计算F原创 2016-09-14 23:01:50 · 291 阅读 · 0 评论 -
HDU 5875 Function ST + 二分区间
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5875代码:#include <bits/stdc++.h>#define sf scanf#define pf printfusing namespace std;const int maxn = 100000 + 5;int MIN[maxn][32];int n;void INIT_R原创 2016-09-12 21:04:10 · 259 阅读 · 0 评论 -
HDU 5877 Weak Pair 树状数组 + DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5877题意: 求有序对个数(i,j)满足: i是j的祖先节点,且ai * aj <= k 代码://#include <bits/stdc++.h>#include <algorithm>#include <cstdio>#define sf scanf#define pf printfus原创 2016-09-12 20:12:13 · 251 阅读 · 0 评论 -
POJ 2777 Count Color 线段树 区间更新
题目链接: http://poj.org/problem?id=2777代码:#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#define sf scanf#define pf printfusing namespace std;typedef long long LL;const原创 2016-08-22 11:15:50 · 292 阅读 · 0 评论 -
线段树 学习资料
http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html习题:http://blog.csdn.net/shiqi_614/article/details/8228102 http://blog.csdn.net/metalseed/article/details/8039326成段更新 :HDU 1698 Just a Hook: http:/原创 2016-08-10 14:54:52 · 298 阅读 · 0 评论 -
51NOD 1672 区间交 线段树
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#define sf scanf#define pf printf#define L (rt <原创 2016-09-06 21:17:34 · 466 阅读 · 0 评论 -
HYSBZ 2243 染色 树链剖分 点上剖分
题目链接:http://bak2.vjudge.net/problem/38096/origin 题意: 两种操作: 1.修改u到v路径上的点的颜色为c 2.询问u到v路径上有多少段颜色线段树上用区间合并来维护答案 主要是在询问时要考虑一下,维护上个段的最后节点颜色来维护颜色段数#include <bits/stdc++.h>#define sf scanf#define pf pri原创 2016-09-19 19:15:52 · 408 阅读 · 0 评论 -
HYSBZ 1588 营业额统计 伸展树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题意:每次查找集合中一个key对于给定的tmp满足 |tmp-key|最小,将tmp加入集合 维护每次的最小差值 第一道伸展树题,大神的模板很好用代码:#include <bits/stdc++.h>#define sf scanf#define pf printfusin原创 2016-09-22 10:41:42 · 361 阅读 · 0 评论 -
POJ 2352 Stars Treap
题目链接:http://poj.org/problem?id=2352代码:#include <cstdio>#include <iostream>#include <cstdlib>#include <cstring>#define sf scanf#define pf printfusing namespace std;const int maxn = 15000 + 50,INF原创 2016-09-23 21:58:05 · 248 阅读 · 0 评论 -
SPOJ QTREE 树链剖分 边上剖分
题目链接:http://bak2.vjudge.net/problem/13013/origin#include <cstdio>#include <cstring>#include <algorithm>#define sf scanf#define pf printfusing namespace std;/** Edge */const int maxn = 10000 + 50;原创 2016-09-21 10:33:36 · 379 阅读 · 0 评论 -
POJ 3481 Double Queue Treap
题目链接: http://poj.org/problem?id=3481 对于每个节点有val和key 操作3种: 1.加入节点val和key 2.查找key最大的节点,输出val,并删除节点 3.查找key最小的节点,输出val,并删除节点Treap模板题 按key值构造Treap,最大点递归查找左孩子,最小点递归查找右孩子代码:#include <algorithm>#inclu原创 2016-09-23 19:27:11 · 222 阅读 · 0 评论 -
HDU 5927 Auxiliary Set DFS好题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5927 题意:给你一颗树,然后把树上一些点染黑,其它点为白点,问白点和白点的最近公共祖先有多少个,最近公共祖先可以是黑点。 已经知道了黑点的个数m,那么白点的个数就是树上节点的个数n减去m。 所以这个题目主要是求有多少个黑点是白点的最近公共祖先。 首先我们看一个黑点的子树,我们原创 2016-10-08 20:34:47 · 335 阅读 · 0 评论 -
POJ 1442 Black Box Treap 模板题
题目链接:http://poj.org/problem?id=1442 给两个序列A,B 求A中前B[i]个数第i小的数是几poj不支持srand(time(NULL)) RE的可能是这个原因 代码://#include <bits/stdc++.h>#include <cstdio>#include <cstdlib>#include <ctime>#define sf scanf原创 2016-09-22 18:06:53 · 276 阅读 · 0 评论 -
HDU 5893 List wants to travel 树链剖分 边权剖分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5893题意:给出2种操作 1.修改u到v的路径上的边的颜色为c 2.查询u到v的路径上有多少段颜色将无根树边为有根树之后,对树进行树链剖分,得到每个点的编号 用边上深度大的节点表示边,这样就是一个树上点剖分 这样就和HYSBZ 2243一样了代码:#include <bits/stdc++.h>原创 2016-09-19 19:49:53 · 521 阅读 · 0 评论 -
HYSBZ 1036 树的统计Count 树链剖分 点权剖分
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=10361.更改点权值 2.求路径上权值和 3.求路径上最大权值 代码:#include <bits/stdc++.h>#define sf scanf#define pf printfusing namespace std;const int maxn = 30000 + 50原创 2016-09-19 14:18:19 · 228 阅读 · 0 评论