随笔分类 -  线段树

摘要:题意:一个老鼠在一条长度为L的直线上跑,吃蛋糕,老鼠只能沿直线移动。开始时没有蛋糕,老鼠的初始位置是0.有两个操作,0 x 代表在位置x添加一个蛋糕; 1 代表老鼠想吃蛋糕。老鼠每次都会选择离自己最近的点,如果两边距离相同,老鼠优先选择与自己当前移动方向相同的点。求最终移动的总距离。题解:线段树单点修改+查询区间端点。设当前位置为pos,每次查询区间[0, pos]的最右端和区间[pos, L]的最左端,比较选择哪个更近。详细题解见代码注释。#include #include #include #include #include #define lson l, m, rt > 1; bu 阅读全文
posted @ 2014-03-05 08:43 冰鸮 阅读(231) 评论(0) 推荐(0)
摘要:线段树基本操作练习,防手生#include #include #include #define lson l, m, rt > 1 ) ); sum[rc] = lazy[rt]*( m >> 1 ); lazy[rt] = 0; } return;}void build( int l, int r, int rt ){ lazy[rt] = 0; sum[rt] = 1; if ( l == r ) return; int m = ( l + r ) >> 1; build( lson ); build( rson ... 阅读全文
posted @ 2013-11-05 17:46 冰鸮 阅读(208) 评论(0) 推荐(0)
摘要:这题跟ZOJ 3606的解题思路很相似。题意:有3中操作:1.向集合中增加一个数x(1≤x≤1e9);2.从集合中删去一个数x(保证这个数存在);3.查询集合中所有位置满足i%5==3的数a[i]的和,集合中的数按升序排列。给你一共N个操作,输出每次查询的和。做法:因为操作只有10^5个,所以将所有查询中的数保存下来,排序之后离散化。每个数对应一个“位置”,通过标记这个“位置”是否已用来表示该数是否在集合中。线段树节点记录两个信息:cnt:这一段“位置”中含有多少个数,pushUp的时候cnt[rt] = cnt[ rt #include #include #include #define L 阅读全文
posted @ 2013-10-10 19:39 冰鸮 阅读(281) 评论(5) 推荐(0)
摘要:这题主要考察观察能力。2^63最多只需要开7次根号就会变成1,当数字变成1之后就不需要再对其进行操作。对于含有大于1数字的区间,向下更新。对于数字全为1的区间,直接返回。#include #include #include #include #include #define LL long long int#define lson l, m, rt > 1; build(lson); build(rson); PushUp( rt ); return;}void Update( int L, int R, int l, int r, int rt ){ if ( ... 阅读全文
posted @ 2013-10-10 17:07 冰鸮 阅读(169) 评论(0) 推荐(0)
摘要:求覆盖三次及其以上的长方体体积并。这题跟http://wenku.baidu.com/view/d6f309eb81c758f5f61f6722.html 这里讲的长方体体积并并不一样。因为本题Z坐标范围非常小,所以可以离散化Z坐标,枚举每个体积块。对每一个体积块:用底面积*高求其体积。底面积直接用“线段树求长方形面积并”来得到即可。对于覆盖次数,pushUp的时候:1.满足 当前覆盖次数大于等于3的,直接求线段长。2.小于3的,由 左右儿子覆盖次数=3 - 当前覆盖次数 的两个儿子更新上来得到。#include #include #include #include using namespa 阅读全文
posted @ 2013-10-09 18:33 冰鸮 阅读(278) 评论(0) 推荐(0)
摘要:卖切糕的小女孩http://www.cnblogs.com/wuyiqi/archive/2012/04/28/2474672.html#include #include #include #define lson l, m, rt > 1; build( lson ); build( rson ); return;}void PushUp( int rt ){ cnt[rt] = cnt[lc] + cnt[rc]; for ( int i = 0; i > 1; if ( L maxAve ) { maxAve = ... 阅读全文
posted @ 2013-09-25 22:42 冰鸮 阅读(465) 评论(0) 推荐(0)
摘要:两种做法。第一种:标记区间最大值和最小值,若区间最小值>=P,则本区间+2c,若区间最大值#include #include #define lson l, m, rt > 1; build( lson ); build( rson ); return;}inline void PushDown( int rt, int m ){ if ( lazy[rt] ) { lazy[lc] += lazy[rt]; lazy[rc] += lazy[rt]; sum[lc] += lazy[rt]*(m - (m >> 1) )... 阅读全文
posted @ 2013-09-24 22:09 冰鸮 阅读(267) 评论(0) 推荐(0)
摘要:参考:http://www.cnblogs.com/oyking/p/3323306.html相当不错的思路,膜拜之~个人理解改日补充。#include #include #include #include #define lson l, m, rt > 1 ) ); maxi[rc] = mini[rc] = mini[rt]; sum[rc] = (LL)maxi[rt]*( m >> 1 ); } return;}void PushUp( int rt ){ sum[rt] = sum[lc] + sum[rc]; maxi[rt] =... 阅读全文
posted @ 2013-09-16 20:58 冰鸮 阅读(322) 评论(0) 推荐(0)
摘要:线段树成段更新+区间最值。注意某人的乘车区间是[a, b-1],因为他在b站就下车了。#include #include #include #include #define lson l, m, rt > 1; build( lson ); build( rson ); return;}void PushUp( int rt ){ maxi[rt] = max( maxi[lc], maxi[rc] ); return;}void PushDown( int rt ){ if ( lazy[rt] ) { lazy[lc] += lazy... 阅读全文
posted @ 2013-09-15 11:01 冰鸮 阅读(246) 评论(0) 推荐(0)
摘要:成功袭击次数=所有袭击次数-成功防守次数需要一个辅助pre来记录上一次袭击成功什么时候,对于每个查询,从上一次袭击成功开始,每隔t更新一次。感觉这样做最坏时间复杂度是O(n^2),这里 说是O(q)*O(q/t)log(n),应该是超时边缘。能AC或许因为数据不强。PS.初始化的时候直接memset会超时,每次根据N的大小build线段树,memset也根据N的大小来就能AC。#include #include #include #define lson l, m, rt >1)); sum[rc] += flag[rt]*(m>>1); flag[rt] = 0;... 阅读全文
posted @ 2013-09-14 21:30 冰鸮 阅读(230) 评论(0) 推荐(0)
摘要:参考:http://www.cnblogs.com/slon/archive/2012/03/30/2426104.html题意:给一个有n个点的点集,有q个询问,每个询问询问一个点p,求与p曼哈顿距离最小的点,并输出曼哈顿距离的最小值。分析:使得abs(qx - xi) + abs(qy - yi)最小,因为带了个绝对值,所以没法直接套用一些数据结构中查询最值的操作,一时间也没什么头绪。后来看到上面的博客,才明白可以分情况讨论,把绝对值去掉。一共四种情况:qx >= xi && qy >= yiqx >= xi && qy = yiqx xi 阅读全文
posted @ 2013-09-04 22:22 冰鸮 阅读(431) 评论(0) 推荐(0)
摘要:http://blog.csdn.net/acm_cxlove/article/details/7548087感觉最巧的是定义了min_dis……将区间内有无英雄升级分开处理#include #include #include #include #define lson l, m, rt > 1; build( lson ); build( rson ); return;}void PushDown( int rt ){ if ( Tr[rt].flag ) { Tr[lc].exp += Tr[rt].flag * Tr[lc].level; ... 阅读全文
posted @ 2013-08-20 10:38 冰鸮 阅读(331) 评论(0) 推荐(0)
摘要:裸线段树,不过不是很好写,需要注意的地方挺多的。用sum[0],sum[1],sum[2]分别记录一次方和,平方和和立方和,更新的时候推一个增量公式:一次方:sum[0]+=(r - l + 1)*c;二次方: ∵(a+b)2=a2+2*a*b+b2; ∴ (xl+c)2 + (xl+1+c)2+……+(xr+c)2=xl2+xl+12+……+xr2+2*c*(xl+xl+1+……+xr)+(r-l+1)*c2; ∴ sum[1] = sum[1]+2*c*sum[0]+(r-l+1)*c2;(注:此处的sum[0]是未更新前的sum[0])三次方: ∵(... 阅读全文
posted @ 2013-08-13 09:48 冰鸮 阅读(507) 评论(0) 推荐(0)
摘要:题意:设原数组为a[i],pos[i]代表第 i 个位置之前有多少个数比a[i]大,求原数组a[i]。这个题意是看了别人的题解才明白,我自己没读出来……方法:假设我们从左往右放,因为后面的数还有可能影响前面的数的位置,所以在最后一个数放完之前,我们没法确定每个数的位置,所以我们反过来考虑,从右往左放。因为每个数前面比它大的数的个数pos[i]已知,我们可以不必关心这些数的具体数值,从而转化为它从右往左走了多少个空格,即pos[i]个,因此这个数放在第 pos[i] + 1 个空格位置上。这个空格所在位置的下标id,即是a[i]。a[i] = id;树状数组或线段树记录区间[1, i ]的空格个 阅读全文
posted @ 2013-07-29 19:38 冰鸮 阅读(298) 评论(0) 推荐(0)
摘要:离散化的时候,排序后相邻点差值大于1的话需要加点。 1 #include 2 #include 3 #include 4 #include 5 6 #define lson l, m, rt > 1; 45 build(lson); 46 build(rson); 47 return; 48 } 49 50 void Update( int L, int R, int c, int l, int r, int rt ) 51 { 52 if ( L > 1; 59 if ( L m ) Update( L ,R, c, rson... 阅读全文
posted @ 2013-07-26 11:10 冰鸮 阅读(218) 评论(0) 推荐(0)
摘要:线段树节点记录内容请见代码注释操作1:每次查询区间 [A, N - 1] 中第一个空格的位置,然后在[ A, N - 1 ]中二分右端点的位置。实际放的花的数量是 要求数量 与 剩余空格数量 之间的最小值。如果[ A, N - 1 ]中已经没有空格了,就不能放了。操作2:求和与成端更新,线段树基本操作不解释比赛的时候把PushDown函数写错了,样例死活不过,比赛结束之后才看出来,2A之,orz……要不要这么伤人…… 1 #include 2 #include 3 #include 4 #include 5 6 #define lson l, m, rt > 1; 33 ... 阅读全文
posted @ 2013-07-25 17:24 冰鸮 阅读(399) 评论(0) 推荐(0)
摘要:同 HDU 2836只不过改成了求最长子串。DP+线段树单点修改+区间查最值。 1 #include 2 #include 3 #include 4 #include 5 6 #define lson l, m, rt > 1;35 if ( L > 1;47 48 int res = 0;49 if ( L m ) res = max( res, Query( L, R, rson ) );51 52 return res;53 }54 55 int main()56 {57 while ( ~scanf( "%d%d", &n, &d ... 阅读全文
posted @ 2013-07-18 14:34 冰鸮 阅读(349) 评论(4) 推荐(0)
摘要:依然不是十分理解……待考虑…… 1 #include 2 #include 3 #include 4 #include 5 6 #define lson l, m, rt > 1; 74 build( lson ); 75 build( rson ); 76 PushUp( rt ); 77 return; 78 } 79 80 node Query( int L, int R, int l, int r, int rt ) 81 { 82 if ( L > 1; 93 94 if ( R m ) return Que... 阅读全文
posted @ 2013-07-15 22:47 冰鸮 阅读(221) 评论(0) 推荐(0)
摘要:Problem H:Boring CountingTime Limit : 6000/3000ms (Java/Other)Memory Limit : 65535/32768K (Java/Other)Problem DescriptionIn this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to answer a list of queries, for each query, p 阅读全文
posted @ 2013-06-20 23:36 冰鸮 阅读(585) 评论(0) 推荐(0)
摘要:线段树:http://www.notonlysuccess.com/index.php/segment-tree-complete/主席树:http://blog.csdn.net/metalseed/article/details/8045038http://seter.is-programmer.com/posts/31907.html划分树:http://wenku.baidu.com/view/8fc6bc365a8102d276a22fa0.htmlhttp://blog.csdn.net/zxy_snow/article/details/6681086 阅读全文
posted @ 2013-06-20 21:54 冰鸮 阅读(339) 评论(0) 推荐(0)