hdu 4366 Successor(线段树)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4366 给定后继关系,寻找后继者满足能力值大于被继承者中忠诚值最大的。 先将树形结构变为线性结构,用邻接表+dfs处理。然后对能力值大到小排序,保证查询时已经插入的一定是能力值大于该查询的值。 线段树功能转化为求解区间最值。 本来1Y的代码,结果loy和blt的输入搞反了!试了好多数据还没试出来!郁闷,这两天写的代码好多这种脑残错。。。code:#include<cstdlib>#include<cctype>#include<cstring>#include<cst
阅读全文
posted @
2012-08-22 11:10
追逐.
阅读(372)
推荐(0)
poj 4047 Garden(线段树)
摘要:http://poj.org/problem?id=4047 求给定区间连续k个值和的最大值,有单点更改和交换操作。 这题主要是建树,以每个节点开始的连续k个值的和作为一个节点,这样共有n-k+1个节点,更新时计算出更新的点所在节点的范围,更新值为 新值-旧值。 交换即为两次更新,线段树记录节点最大值。code:#include<cstdlib>#include<cctype>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#includ
阅读全文
posted @
2012-08-22 09:16
追逐.
阅读(212)
推荐(0)
hdu 1542 Atlantis (线段树+扫描线)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1542 单纯的线段树+扫描线求面积并,需要离散化。code:#include<cstdlib>#include<cctype>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<sstream>
阅读全文
posted @
2012-08-22 09:08
追逐.
阅读(293)
推荐(0)
hdu 1394 Minimum Inversion Number(线段树)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1394 线段树求逆序数,先求出初始数组的逆序数(n logn),然后o(1)推出其他逆序数。因为输入为0--n-1,所以建树从0开始。code:#include<cstdlib>#include<cctype>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>#include<string>#incl
阅读全文
posted @
2012-08-20 09:37
追逐.
阅读(242)
推荐(0)
hdu 1541 Stars(树状数组)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1541 开始知道是用树状数组做,但想了好半天不知道怎么用,总觉得要先排序才能做。但是排序的话复杂度就上去了啊。 纠结了半天才读到那句Stars are listed in ascending order of Y coordinate.擦了个擦的,早看见不完了。 c[pos]表示1到pos的和,也就是当前星星前有几个(包含当前)。用cnt记录各等级数量。code:#include<cstdlib>#include<cctype>#include<cstring>#inclu
阅读全文
posted @
2012-08-07 20:15
追逐.
阅读(317)
推荐(0)
hdu 1166 敌兵布阵 (树状数组)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1166 相比与线段树,树状数组的编程难度确实很低,不容易出错,而且同等问题上效率较高些。 但是好像树状数组能解决的问题线段树都可以解决,而线段树能解决的它却不一定能解决。code:#include<cstdlib>#include<cctype>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>#include&
阅读全文
posted @
2012-08-07 12:20
追逐.
阅读(196)
推荐(0)
poj 2104 K-th Number (划分树)
摘要:http://poj.org/problem?id=2104 入门的划分树,说实话一开始看着挺晕的,真看明白了,知道了各个操作各个变量的意义就简单多了。 这个有没有线段树的解法?既然是基于线段树的,那么处理这种问题就应该比线段树的复杂度低很多吧。code:#include<cstdlib>#include<cctype>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>#include<st
阅读全文
posted @
2012-08-06 17:59
追逐.
阅读(432)
推荐(0)
二维线段树 树套树
摘要:二维线段树,用的树套树,在第一维每个节点上再建一个线段树,代码量加倍。code:#include<cstdlib>#include<cctype>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<sstream>#include<set>#include<qu
阅读全文
posted @
2012-08-05 15:05
追逐.
阅读(1422)
推荐(0)
线段树 区间更新(hdu1698) 区间合并(poj3667)
摘要:区间更新用到的延迟标记,不过是在更新时只更新到最大父区间,不继续往下更新,把更新的状态记录下来。当访问到子区间时再根据相应记录及时更新,pushodown()。code:#include<cstdlib>#include<cctype>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<
阅读全文
posted @
2012-07-31 12:28
追逐.
阅读(547)
推荐(0)
hdu 1754 I hate it (线段树)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1754 基础线段树,单点替换,区间最值。code:#include<cstdio>#include<algorithm>usingnamespacestd;#definelsonl,m,rt<<1#definersonm+1,r,rt<<1|1constintmaxn=200010;intsum[maxn<<2];template<classT>inlinevoidscan_d(T&ret){charc;ret=0;while((
阅读全文
posted @
2012-05-09 08:39
追逐.
阅读(221)
推荐(0)
hdu 1166 敌兵布阵 (线段树)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1166 最基础的线段树,单点更新。完全跟着HH的代码风格写的。code:#include<cstdio>#definelsonl,m,rt<<1#definersonm+1,r,rt<<1|1constintmaxn=50005;intsum[maxn<<2];voidPushUp(intrt){sum[rt]=sum[rt<<1]+sum[rt<<1|1];}voidbuild(intl,intr,intrt){if(l==r){sca
阅读全文
posted @
2012-05-09 08:35
追逐.
阅读(219)
推荐(0)
poj 1984 Navigation Nightmare (并查集)
摘要:http://poj.org/problem?id=1984 题目描述的如此蛋疼。。。 关系并查集,用cx和cy数组记录点与根结点的相对位置,然后根据输入依次合并,当碰到询问时便计算距离,若不在同一集合中,即无法确定相对位置。code:#include<cstdio>#include<cstring>intp[40001],cx[40001],cy[40001];intf1[10001],f2[10001],fi[10001];structedge{ints,e,l;chard;}q[40001];intabs(inta){returna>0?a:-a;}void
阅读全文
posted @
2012-04-21 21:15
追逐.
阅读(275)
推荐(0)
hdu 1272 小希的迷宫 (并查集)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1272 裸的并查集,注意两个地方: 1. 最后所有的点要在同一个集合中 2. 输入只有0 0时需要特殊处理code:#include<cstdio>#include<cstring>intp[100001];boolvis[100001];voidmake_set(){for(inti=1;i<=100001;i++)p[i]=i;}intfind_set(intx){if(p[x]!=x)p[x]=find_set(p[x]);returnp[x];}voidunion_set
阅读全文
posted @
2012-04-07 13:19
追逐.
阅读(243)
推荐(0)
fzu 1894 志愿者选拔 (单调队列)
摘要:http://acm.fzu.edu.cn/problem.php?pid=1894 简单单调队列。code:#include<cstdio>intdata[1000001];intqueue[1000001];intnum[1000001];inth,r,n;voidinsert(){if(r==h-1||data[n]<queue[r]){r++;queue[r]=data[n];num[r]=n;}else{while(r>=h&&data[n]>=queue[r])r--;queue[++r]=data[n];num[r]=n;}}void
阅读全文
posted @
2012-02-09 23:25
追逐.
阅读(240)
推荐(0)
poj 2823 Sliding Window (单调队列)
摘要:http://poj.org/problem?id=2823 纯粹的单调队列练习题,用了一下输入的加速,结果发现还不如scanf快... 话说前段时间搞单调优化的时候,就是有点卡在单调队列上了,当时真没整明白资料里说的到底在维护什么...今天费老传了DP资料(完整版...)才算真搞懂。唉,浪费了那么多激情... 为DP的单调优化做准备吧,明天开始搞DP了,不能再缩了...code:#include<cstdio>intdata[1000005];intqueue[1000005];intn,k;voidgetMin(){inth,r;h= r = 0 ;queue[0]=0;for
阅读全文
posted @
2012-02-08 00:52
追逐.
阅读(273)
推荐(0)
poj 2912 Rochambeau (并查集+枚举)
摘要:http://poj.org/problem?id=2912 枚举+关系并查集,并查集与1182相似,枚举每个小孩为judge时的情况,若当前枚举情况下每个round都是正确的,则当前枚举编号可能是judge。若只找到一个judge的可能,则找出排除其他编号所需最多的round数,两者即为输出。 做了一整天,WA了9次,原因是偏移量r公式搞错一个地方,列了好多次表才搞定。code:#include<cstdio>#include<cstring>usingnamespacestd;intf[501];intr[501];intx[2001];inty[2001];int
阅读全文
posted @
2012-02-02 23:50
追逐.
阅读(283)
推荐(0)
poj 1182 食物链 (并查集)
摘要:http://poj.org/problem?id=1182 重点依旧是用rank记录相对于根节点的关系,并及时跟新rank的值。code:#include<cstdio>usingnamespacestd;intf[50001];intr[50001];//与根节点的关系intfind_Set(intx){inttemp;if(x==f[x])returnx;temp=f[x];f[x]=find_Set(temp);r[x]=(r[temp]+r[x])%3;/*r[x]是相对于x原根节点temp的关系值,现在相对根节点已不是temp故更新r[x]值时要加上temp相对于其根节
阅读全文
posted @
2012-02-01 00:30
追逐.
阅读(247)
推荐(0)
poj 2236 Wireless Network (并查集)
摘要:http://poj.org/problem?id=2236 给定N个坏掉的无线发射器坐标,给定其能相连通的最大距离,O i 代表修好第i个发射器,S i j 表示判断第i个和第j个是否能接通(可间接相连)。 简单并查集应用,一个bool数组标记是否可用,每修好一个,找N个中已修好且可以直接相连的合并。最后判断i,j 是否都已修好且在同一个集合中即可。 脑残的错,把make_set()放到n的输入之前了,找了N久N久。泪流满面啊...code:#include<cstdio>intnode[1005][2];intp[1005];boolflag[1005];intn,d;void
阅读全文
posted @
2012-01-31 02:22
追逐.
阅读(231)
推荐(0)
poj 1308 Is It A Tree? (并查集)
摘要:http://poj.org/problem?id=1308 题意:给出一些节点的连接情况,问所给出的节点是不是可以构成一棵树。 树的定义已经给出: 1. 只有一个根节点 2. 根节点到每个节点只有唯一路径 3. 题目第一句特别提到空树也是一颗合法树 对于第一条,明显的森林不是树,并查集将每对输入合并,最后看是否在同一个集合中。 第二条,保证唯一路径与题目中给出的第二条定义相似,对于每个节点,只有一条边指向它。如果节点已经存在于集合中,则肯定已经有一边指向它,或者它是作为根节点。也就是说,输入的两个元素不能在同一个集合中。可恶的是这题居然没给数据量!开始以200为上限做的,最后用majia试了
阅读全文
posted @
2012-01-31 00:34
追逐.
阅读(250)
推荐(0)
poj 1703 Finde them, Catch them! (并查集)
摘要:http://poj.org/problem?id=1703 跟2492基本一样,不多说。code:#include<cstdio>usingnamespacestd;intf[100005];intr[100005];intn,m;intfind_Set(intx){inttemp;if(x==f[x]){returnx;}temp=f[x];f[x]=find_Set(temp);r[x]=(r[x]+r[temp])%2;//保持r[x]相对于根节点的稳定returnf[x];}voidUnion(intx,inty,intfx,intfy){f[fy]=fx;r[fy]=(
阅读全文
posted @
2012-01-30 22:32
追逐.
阅读(183)
推荐(0)