随笔分类 -  线段树

摘要:线段树点修改+区间合并对于如何查询连续某块的端点和长度还不太熟……但是属于经典常见操作。 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 6 using namespace std; 7 8 #define lc rt << 1 9 #define rc rt << 1 | 1 10 #define lson l, m, rt << 1 11 #define rson m + 1, r, 阅读全文
posted @ 2013-05-22 09:50 冰鸮 阅读(184) 评论(0) 推荐(0)
摘要:裸线段树区间合并,一开始没注意题目细节结果搞麻烦了……给你四种操作,每种操作按它的要求输出结果。1.Reset Reset all memory units free.Reset 释放所有的内存2.New x Allocate a memory block consisted of x continuous free memory units with the least start numberNew x 申请一个包含x个连续内存单元的内存块,起始地址编号最小3.Free x Release the memory block which includes unit xFree x 释放包含. 阅读全文
posted @ 2013-05-21 22:55 冰鸮 阅读(372) 评论(0) 推荐(0)
摘要:裸线段树区间合并,题目本身不难,就是细节处理比较麻烦。因为涉及到异或运算,所以连续0和连续1的个数都要记录一下。操作的懒惰标记我只用了一个flag,注意flag更新时的细节,我分了三种情况:flag == -1 或者 当前操作为0或1:更新时直接赋值。因为0, 1操作都可以直接覆盖前面的操作。flag == 0或1,当前操作为2(xor):flag ^= 1。前面标记过0或1,当前操作为异或,那么改变flag标记flag == 2,当前操作为2(xor): flag = -1。前面只出现过异或运算,没出现过0,1运算。因为异或两次相当于没异或,所以清除标记即可。 1 #include < 阅读全文
posted @ 2013-05-20 21:03 冰鸮 阅读(268) 评论(0) 推荐(0)
摘要:挺不错的一道线段树,薛神给的礼物,WA了整整一天才过,各种细节出错OTL……1.纵坐标扩大两倍保存,不然样例都跑不过。即(0, 2)与(3, 4)之间可以有一条横线,但是如果坐标不扩大,这条横线是过不去的。2.按x值从小到大排序,插入线段树,每次将Line[id]更新到线段树中之前,先查询编号为Line[id]向左可以看到哪几条线段(即可以看到哪几条编号比它小的线段),用vector保存,注意去重,不然最后结果会多。(vector忘了clear这里错了好几次。。。)3.id[MAXN]用来标记该段最顶端的线段的编号,-1代表该段没被覆盖或者该段不是被同一条线段覆盖4.四重循环暴力枚举,暂时也没 阅读全文
posted @ 2013-05-14 18:28 冰鸮 阅读(233) 评论(0) 推荐(0)
摘要:因为0-15二进制下最大是1111,所以可以每个区间记录一下各个位上1的个数,这样不管是对于操作也好还是求和也好都简便了许多。PS1.感谢薛神提点。PS2.注意细节 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 5 #define lson l, m, rt << 1 6 #define rson m + 1, r, rt << 1 | 1 7 8 const int MAXN = 1000002; 9 10 int cnt[ MAXN << 阅读全文
posted @ 2013-05-10 12:50 冰鸮 阅读(265) 评论(2) 推荐(0)
摘要:裸线段树,目前WA中。如果用离散化的话,就跟poj 2528 Mayor’s posters一样中间需要插点,之前样例一直跑不对就是这个原因。但这个做法依然不对,我这种做法相当于默认为位运算的运算顺序不影响结果,实际上对于位运算来说,混合运算不同的运算顺序不一定得到相同的结果。虽然花了很长时间很大功夫也没做出来,但是知道自己错在哪里也是一种收获。 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 6 #define lson 阅读全文
posted @ 2013-05-09 23:23 冰鸮 阅读(382) 评论(6) 推荐(0)
摘要:待编辑需一步思路转化 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define lson l, m, rt << 1 6 #define rson m + 1, r, rt << 1 | 1 7 8 const int MAXN = 200001; 9 10 int pos[MAXN], val[MAXN];11 int sum[MAXN << 2];12 int ans[MAXN];13 int N, id;14 15 void b 阅读全文
posted @ 2013-05-09 14:02 冰鸮 阅读(155) 评论(0) 推荐(0)
摘要:线段树矩形周长并 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 6 #define lson l, m, rt << 1 7 #define rson m + 1, r, rt << 1 | 1 8 9 using namespace std; 10 11 struct Seg_Tree 12 { 13 int l, r, h, s; 14 Seg_Tree() {} 15 Seg_Tree( i 阅读全文
posted @ 2013-05-09 13:52 冰鸮 阅读(191) 评论(0) 推荐(0)
摘要:求区间内最长连续上升子序列线段树典型区间合并 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 6 #define lson l, m, rt << 1 7 #define rson m + 1, r, rt << 1 | 1 8 9 using namespace std; 10 11 const int MAXN = 100010; 12 13 int N, Q; 14 int Lval[ MAX 阅读全文
posted @ 2013-05-09 00:35 冰鸮 阅读(162) 评论(0) 推荐(0)
摘要:线段树 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 6 #define lson l, m, rt << 1 7 #define rson m + 1, r, rt << 1 | 1 8 9 using namespace std; 10 11 const int MAXN = 1000010; 12 13 char str1[MAXN]; 14 char str2[MAXN]; 15 bool 阅读全文
posted @ 2013-05-06 22:43 冰鸮 阅读(276) 评论(0) 推荐(0)
摘要:线段树 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 6 #define lson l, m, rt << 1 7 #define rson m + 1, r, rt << 1 | 1 8 9 using namespace std; 10 11 const int MAXN = 100010; 12 13 int N, Q; 14 int color[ MAXN << 2 ]; // 阅读全文
posted @ 2013-05-06 22:39 冰鸮 阅读(238) 评论(0) 推荐(0)
摘要:题意:给你一棵树,求树中某节点子树中能力值大于它且忠诚度最高的那个。在第一次看到这个问题是,我有一个疑问:解决这个问题有两个关键点:1.树形结构到线性结构的转换原因:员工关系整棵树是一棵结构不确定的树,员工编号不一定连续,对于查询其符合条件的下属有很大困难(只能暴力)。而编号之后可以将子树映射到编号连续的一段区间,这时就可以用线段树快速查询最值。做法:用邻接表保存树,从根开始DFS,记录每棵子树的起始端点和终止端点。2.对能力值从大到小进行排序,按能力值从大到小的顺序加入线段树,能力值相同时,编号小的在前面。原因:因为每次要查找能力值大于该人且忠诚度最高的那个下属,所以当该点插入线段树时,线段 阅读全文
posted @ 2013-05-05 15:59 冰鸮 阅读(355) 评论(0) 推荐(0)
摘要:题目链接:http://poj.org/problem?id=2374DP+线段树View Code 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 6 #define lson l, m, rt << 1 7 #define rson m + 1, r, rt << 1 | 1 8 9 using namespace std; 10 11 const int MAXN = 100000; 12 c 阅读全文
posted @ 2013-04-24 15:30 冰鸮 阅读(291) 评论(0) 推荐(0)
摘要:题目链接:线段树,点修改。 1 #include <cstdio> 2 #include <cctype> 3 #define lson l, m, rt << 1 4 #define rson m + 1, r, ( rt << 1 ) | 1 5 6 const int MAXN = 100010; 7 const int INF = 2147483645; 8 9 int tree[ MAXN << 2 ]; 10 11 int min( int a, int b ) 12 { 13 return a < b ? a : 阅读全文
posted @ 2012-12-04 20:52 冰鸮 阅读(272) 评论(0) 推荐(0)
摘要:题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754线段树节点更新练习第四棵线段树……TUT……初始化的时候忘了把cnt设为0, RE了几次……=3= 1 #include <cstdio> 2 #include <cstring> 3 4 const int MAXN = 200000 + 10; 5 6 struct Node 7 { 8 int r, l; 9 int max;10 Node *l_child, *r_child;11 };12 13 Node Tree[MAXN + MAXN];14 int .. 阅读全文
posted @ 2012-10-27 14:25 冰鸮 阅读(157) 评论(0) 推荐(0)
摘要:题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166线段树节点更新练习第三棵线段树……TUT…… 1 #include <cstdio> 2 #include <cstring> 3 4 const int MAXN = 50000 + 10; 5 6 struct Node 7 { 8 int l, r; 9 int sum;10 Node *l_child, *r_child;11 };12 13 int N;14 int cnt;15 Node Tree[ MAXN + MAXN ];16 17 void Bu.. 阅读全文
posted @ 2012-10-27 13:20 冰鸮 阅读(166) 评论(0) 推荐(0)
摘要:题目链接:http://poj.org/problem?id=3468题意:给N个数,有两组操作:(1)对某个区间[a, b]中所有的数都增加c(2)求某个区间[a, b]个数的和共操作Q次,每次只输出操作(2)的结果第二棵线段树……TUT……需要用到延迟标记,每次更新不用到达叶子节点,只需要在标记上进行累加,当询问的时候再将标记向下传递。 1 #include <cstdio> 2 #include <cstdlib> 3 4 const int MAXN = 100000 + 5; 5 6 struct TNode 7 { 8 int l, r; 9 ... 阅读全文
posted @ 2012-10-23 12:18 冰鸮 阅读(187) 评论(0) 推荐(0)
摘要:题目链接:http://poj.org/problem?id=3264题目描述:跟N个数和Q个询问,求询问区间[a, b]中最大值和最小值的差。我的第一个线段树…… 1 /* 2 Interval Tree 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 8 const int MAXN = 50000 + 2; 9 const int INF = 2147483645; 10 11 struct Node 12 { 13 int l, r; 14 int maxi,. 阅读全文
posted @ 2012-10-22 22:26 冰鸮 阅读(143) 评论(0) 推荐(0)