
数据结构
文章平均质量分 75
KinderRiven
这个作者很懒,什么都没留下…
展开
-
【SPOJ QTREE】树链剖分模板
用线段树求解,这里注意由于求的是路径最大值,注意一下细节。#include#include#includeusing namespace std;const int MAXN = 10010;#define lson (pos<<1)#define rson (pos<<1|1)const int INF = (1 << 30);int n;//---------------原创 2015-08-16 21:53:46 · 726 阅读 · 0 评论 -
ZOJ 3324 Machine(线段树区间合并)
这道题网上很多代码是错误的,但是可以AC。比如这组数据10 3p 0 9r 0 5r 6 9输出应该是 0 1 1所有有的人直接记录该区间是否被覆盖过的方法是错误的正确方法应该是记录这段区间的最小高度(就是最接近初始位置的高度),和最小高度对应的最长左区间和右区间开一个sum记录这段区间最小高度的块数,min_v 记录该区间最小高度cover作为懒惰标记下推原创 2015-04-30 19:04:13 · 801 阅读 · 0 评论 -
HDU 1754
/*HDU 1754单点更新区间最值*/#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 222222;int sum[maxn << 2];void pushup(int pos){ sum[pos] =原创 2015-04-30 13:49:10 · 445 阅读 · 0 评论 -
HDU 1542
/*Atlantis*/#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 2005;int cnt,ret;struct Seg{ double a,b,h; int c; Seg(double原创 2015-04-30 14:52:20 · 431 阅读 · 0 评论 -
HDU 1255
/*扫描线求矩阵的覆盖面离散化线段树求面积并升级版*/#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)typedef long long LL;const int maxn = 10005;int cnt,ret;struct Seg{原创 2015-04-30 14:48:50 · 400 阅读 · 0 评论 -
HDU 4553
/*HDU 4553约会安排线段树区间合并线段树最左子区间*/#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 122222;struct Node{ int l,r; int lx1,rx1,mx原创 2015-04-30 14:43:43 · 489 阅读 · 0 评论 -
HDU 4027
/*Can you answer these queries?*/#include#include#include#include#includeusing namespace std;typedef long long LL;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 100005;LL原创 2015-04-30 14:18:20 · 477 阅读 · 0 评论 -
ZOJ 1610
/*Count the Colors区间覆盖*/#include#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 8888;const int maxd = 8001;int cover[maxn << 2];i原创 2015-04-30 14:10:19 · 538 阅读 · 0 评论 -
POJ 2528
/*Mayor's posters线段树区间覆盖离散化特殊数据:131 101 46 10输出:3*/#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 55555;int lx[maxn],rx[ma原创 2015-04-30 14:02:30 · 409 阅读 · 0 评论 -
POJ 3468
/*POJ 3468区间增加区间求和懒惰标记*/#include#include#include#define lson (pos<<1)#define rson (pos<<1|1)typedef long long LL;const int maxn = 100005;int n,m;LL sum[maxn << 2],col[maxn << 2];void pu原创 2015-04-30 13:57:32 · 407 阅读 · 0 评论 -
HDU 1698
/*Just a Hook线段树区间更新区间求和*/#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 100005;int cover[maxn << 2],sum[maxn << 2];void pushdown原创 2015-04-30 14:06:44 · 378 阅读 · 0 评论 -
HDU 3974
/*Assign the taskdfs建立线段树*/#include#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 50005;int n,m;int num[maxn],vis[maxn];vectorG[原创 2015-04-30 14:29:20 · 580 阅读 · 0 评论 -
POJ 3264
/*Balanced Lineup区间最值的查询*/#include#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 55555;int min_v[maxn << 2],max_v[maxn << 2];void原创 2015-04-30 14:14:54 · 424 阅读 · 0 评论 -
HDU 1166
/*HDU 1166敌兵布阵单点更新区间求和*/#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 55555;int sum[maxn << 2];void pushup(int pos){ sum[po原创 2015-04-30 13:45:53 · 419 阅读 · 0 评论 -
HDU 4614
/*HDU 4614Vases and Flowers二分 + 线段树*/#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 55555;int n,m,pos_x,pos_y;struct Node{ in原创 2015-04-30 14:39:51 · 469 阅读 · 0 评论 -
HDU 4578
/*Transformation*/#include#include#includeusing namespace std;typedef long long LL;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 100005;const int mod = 10007;struct Node{原创 2015-04-30 14:34:58 · 2861 阅读 · 0 评论 -
POJ 1177
/*扫描线求矩阵的周长交并*/#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)typedef long long LL;const int ADD = 10005;const int maxn = 25555;struct Seg{ in原创 2015-04-30 14:46:50 · 430 阅读 · 0 评论 -
HDU 1540
void pushup(int pos){ node[pos].lx = node[lson].lx; node[pos].rx = node[rson].rx; if(node[pos].lx == node[lson].len()) node[pos].lx += node[rson].lx; if(node[pos].rx == node[rson].len(原创 2015-04-30 14:23:35 · 617 阅读 · 0 评论 -
【HDU】How Many Answers Are Wrong(带权并查集)
num[i]代表i到根节点的值这道题一开始竟然以为是线段树= =!后来发现线段树无法进行子区间的维护#include#include#includeusing namespace std;const int maxn = 222222;int fa[maxn],num[maxn];int find_father(int u){ if(fa[u] == u)原创 2015-05-04 21:59:10 · 830 阅读 · 0 评论 -
【HDU】5222 Exploration(并查集+拓扑排序)
对于无向边使用并查集合并成一个集合,对于有向边使用判断是否存在环唯一无语的地方就是看输入是百万级的,加个输入挂跑9s,不加挂跑了5s#include#include#include#includeusing namespace std;#pragma comment(linker, "/STACK:102400000,102400000")const int maxn = 10原创 2015-05-05 23:11:52 · 1120 阅读 · 0 评论 -
【FZU】Problem 2059 MM(离线处理并查集)
离线处理,并查集 #include#include#includeusing namespace std;const int maxn = 100005;struct Node{ int id,value;}node[maxn],input[maxn];bool cmp(Node p,Node q){ return p.value > q.value;原创 2015-05-15 22:10:35 · 889 阅读 · 0 评论 -
【POJ 3241】曼哈顿最小生成树(模板整理)
关于 曼哈顿最小生成树 的证明见:http://www.2cto.com/kf/201505/399861.html模板:#include#include#include#includeusing namespace std;const int MAXN = 100010;const int INF = 0x3f3f3f3f;struct Point{ int x,原创 2015-08-16 21:45:15 · 1442 阅读 · 0 评论 -
【HDU 5372】Segment Game(树状数组)
题解:对于新插入的线段,查询有多少个线段左端点大于等于该线段的左端点。 再查询有多少个线段的右端点大于该线段右端点, 两者之差就是答案。这里注意两个问题,一个是离散化,第二个这道题时间卡的可能比较严,线段树貌似会超时~好久没写离散化了。。。生疏了#include#include#includeusing namespace std;const int max原创 2015-08-13 23:22:01 · 822 阅读 · 0 评论 -
【HDU 5384】Danganronpa(AC自动机)
看官方题解貌似是个DP比赛的时候用kuangbin的AC自动机模板瞎搞的,竟然A了,而且跑的还不慢。。存下模板吧#include#include#include#include#include#include#includeusing namespace std;const int maxn = 500010;const int maxd = 26;struct T原创 2015-08-13 20:24:25 · 982 阅读 · 0 评论 -
【HDU 2586】LCA模板
在一棵树上 求2个点的最短距离,那么首先利用LCA找到2个点的最近公共祖先公式:ans = dis(x) + dis(y) - 2 * dis(lca(x,y))这里的dis(x)指的上x距离根节点的距离注意一些细节方面,比如数组的越界问题:#include#include#include#includeusing namespace std;typedef long l原创 2015-07-30 10:37:55 · 700 阅读 · 0 评论 -
【HDU5371】Hotaru's problem(Manacher + set)
利用Manacher求出每两个数字中间位置的回文长度之后利用set进行维护,大题思路如下:要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,比如上边的两个字符串,共享 8 9 10这一部分。 也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样。 因为我们已经记录下来以第i个点和第i+1个点为中心的回文串长度, 那么问题可以转化成,相距x原创 2015-08-11 20:40:13 · 832 阅读 · 0 评论 -
【CodeForces】E. Xenia and Tree(分块 + LCA)
对于两个操作,对于操作1,每次储存要修改的点,直到存了sqrt(m)个点的时候进行更新,并将图重建一次(重新记录每个点到最近染色点的距离)对于操作2,利用LCA求现在存着的点(也就是还没有更新到图上的点)和这个点的距离的最小值以及这个点在当前图中和最近的染色的那个点的距离。话说LCA真是个好东西= =!这几天补一补去题解在这里:http://codeforces.com/blog/e原创 2015-07-29 19:47:27 · 889 阅读 · 0 评论 -
【HDU 5316】Magician(线段树)
一开始度错题了,题意是求一段和最大的【子序列】,要求相邻两个元素的位置必须互为奇偶。这样我们可以使用线段树维护4个值:一段区间内开头结尾元素为:奇奇奇偶偶奇偶偶的最大值之后在pushup的的时候根据题目所给的意思进行合并。#include#include#includeusing namespace std;#define lson (pos<<1)#原创 2015-07-30 09:43:16 · 692 阅读 · 0 评论 -
【HDU 4763】Theme Section(KMP)
这题数据水的一B,直接暴力都可以过。比赛的时候暴力过的,回头按照正法做了一发。匹配的时候 失配函数 其实就是前缀 后缀的匹配长度,之后就是乱搞了。KMP的题可能不会很直接的出,但是KMP的思想经常渗透在很多题目里面,最近需要多练习一下。#include#include#includeusing namespace std;const int maxn = 1000005;原创 2015-08-10 22:49:35 · 854 阅读 · 0 评论 -
【HDU 4760】Good Firewall(Trie水题)
计算机网络没学好,比赛的时候脑子一抽题意理解错了,赛后自己写了1A,内流满面。。。。和一般的trie不太一样,这里的trie每个节点开一个vector存网络的id恩,就这样,跑的挺快的 78ms#include#include#include#include#include#include#include#define LL long longusing namespa原创 2015-08-10 20:21:51 · 780 阅读 · 0 评论 -
【HDU 4819】Mosaic 二维线段树模板
二维线段树的模板题,和一维一样的思路,更新的时候注意一下细节。存模板:/* 二维线段树模板整理*/#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 805;const int INF = (1 << 30);int n;原创 2015-08-09 17:58:22 · 1164 阅读 · 0 评论 -
【HDU 4122】RMQ
这道题暴力竟然比标程跑的快!!!!在这里RMQ维护的是一段区间内最小代价的下标。另外就是注意一下 求某两天之间有多少天的函数怎么写。感觉这道题没做出来一个是题没读懂,第二个就是不敢去暴,第三个就是对一些问题的转化不够。#include#include#include#include#include#include#includeusing namespace std;原创 2015-08-07 21:15:59 · 724 阅读 · 0 评论 -
【HDU】2818-Building Block(并查集)
坑死了,这道题QAQ。。。说好的没有0,但是后台数据有0,WA了一下午= =利用num[i]代表i下面有几个格子,Size[i]代表i做底的时候这一堆有几个箱子#include#includeusing namespace std;const int maxn = 30005;int n,fa[maxn];int num[maxn],Size[maxn];char op[5];原创 2015-06-04 16:22:12 · 554 阅读 · 0 评论 -
12166 - Equilibrium Mobile(DFS)
枚举每个点,判断其余深度是否存在点的权值为(1 deep1,deep2为2点的深度写的比较麻烦#include#include#include#includeusing namespace std;typedef long long LL;const int maxn = 1000005;char str[maxn];vectorG[20];LL base[20];原创 2015-05-19 08:55:20 · 754 阅读 · 0 评论 -
806 - Spatial Structures(DFS)
没什么思路,难就难在麻烦,各种DFS,挺练基本功的... Problem VerdictLangTimeBestRankSubmit Time | discuss806 - Spatial Structures AcceptedC++0.2720.045902 mins ago原创 2015-05-19 19:21:46 · 851 阅读 · 0 评论 -
【HDU】5249-KPI(线段树+离散化)
好久没写线段树都不知道怎么写了。。。非常简单的线段树二分问题#include#include#include#include#includeusing namespace std;typedef long long LL;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 10005;int n,Case原创 2015-06-03 17:01:08 · 850 阅读 · 0 评论 -
【HDU】5253-连接的管道(并查集)
十分简单的并查集问题,注意建图的方式#include#include#include#includeusing namespace std;const int maxn = 1005;const int maxd = 5000005;const int dir[2][2] = {{0,1},{1,0}};int n,m,mat[maxn][maxn],cnt,fa[maxd];原创 2015-06-03 20:12:49 · 1179 阅读 · 0 评论 -
【Codeforces】542C - Idempotent functions(思路)
题目大意思:给定一个数的next值,问最小的k值,使得每次进行k次操作得到的数都是一样的。将每个位置i记为点i记circle[i]为从i出发出现环的环内元素,我们的任务就是求这些数的最小公倍数lcm,还要考虑‘6’型循环的情况,这个我们只需要求最大的多余长度之后对lcm向上取整就好了#include#include#include#includeusing namespac原创 2015-05-15 09:41:46 · 1311 阅读 · 0 评论 -
【HDU】1828-Picture(线段树扫描线)
矩阵交并周长的模板题这题不需要离散化,需要注意的时候负坐标转化成正坐标#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)typedef long long LL;const int maxn = 30005;const int ADD = 1000原创 2015-04-27 21:40:20 · 667 阅读 · 0 评论 -
【POJ】3667-Hotel(线段树的区间合并)
标记线段树的时候利用 lsum rsum msum 记录最左边 zui右边 以及整个区间的最大连续空位的值维护的时候注意合并#include#include#includeusing namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 55555;int n,m;struct No原创 2015-04-27 16:02:42 · 785 阅读 · 0 评论