随笔分类 -  数据结构 - 线段树

摘要:显然先用单调栈求出一个位置向左/右延申的最大长度(即这些区间中当前位置是最大值位置) 然后我们发现我们可以离线,然后按照最大值位置依次添加线段,每次用线段树查一个区间和. 然后我们想查满足最大值位置在 $[l,r]$ 之间,$[l,r]$ 内区间和. 这个显然满足可减性(即最大值位置在 $[1,r] 阅读全文
posted @ 2020-02-29 17:41 EM-LGH 阅读(221) 评论(0) 推荐(0)
摘要:A Prefix Sum Primes 显然,除了 2 以外的质数都是奇数,所以最优的排布方式应该是 21222222.... 然后 2 不够的时候再放 1 code: #include <bits/stdc++.h> #define N 200009 #define setIO(s) freope 阅读全文
posted @ 2020-02-29 15:22 EM-LGH 阅读(142) 评论(0) 推荐(0)
摘要:用 LCT 维护颜色相同连通块,然后在线段树上查一下逆序对个数就可以了. code: #include <cstdio> #include <algorithm> #include <cstring> #include <string> #define N 100005 #define ll lon 阅读全文
posted @ 2020-02-28 16:46 EM-LGH 阅读(226) 评论(0) 推荐(0)
摘要:code: #include <vector> #include <cstdio> #include <cstring> #include <map> #include <set> #include <algorithm> #define N 300005 #define MAX 320005 #d 阅读全文
posted @ 2020-02-15 17:14 EM-LGH 阅读(229) 评论(2) 推荐(0)
摘要:思路自然的码农题. 显然分类讨论一下编号在 $[l,r]$ 的点与 $p$ 的子树关系. 如果都在 $p$ 的子树内就是个区间 $lca$. 否则,就二分第一个满足 $p$ 的祖先且子树内部没有 $[l,r]$ 之间的点. 二分验证的话要用主席树. code: #include <cstring> 阅读全文
posted @ 2020-02-13 15:42 EM-LGH 阅读(158) 评论(0) 推荐(0)
摘要:将问题转化为统计以 $i$ 结尾的 $AA$ 串个数. 我们将后缀树建出来,然后按照启发式合并的方式每次合并两个 $endpos$ 集合. 那么就有:$[x-mx,x-1] \rightarrow x$ 与 $x \rightarrow [x+1,x+mx]$ 的贡献. 统计第一种贡献的话在线段树上 阅读全文
posted @ 2020-02-12 16:58 EM-LGH 阅读(167) 评论(0) 推荐(0)
摘要:第一次写这个题是好长时间以前了,然后没调出来. 本来以为是思路错了,结果今天看题解发现思路没错,但是好多代码细节需要注意. code: #include <cstdio> #include <vector> #include <map> #include <cstring> #include <al 阅读全文
posted @ 2020-02-09 15:27 EM-LGH 阅读(178) 评论(0) 推荐(0)
摘要:A - Sasha and a Bit of Relax code: #include <cstdio> #include <map> #include <cstring> #include <algorithm> #define N 300006 #define ll long long #def 阅读全文
posted @ 2020-02-07 21:25 EM-LGH 阅读(156) 评论(0) 推荐(0)
摘要:显然如果卖出的话肯定要在同一天卖出. 那么我们只需维护 $max(y_{i}\prod x_{i})$ 即可. 乘法维护不了,取一个对数就好了. code: #include <cstdio> #include <cmath> #include <cstring> #include <algorit 阅读全文
posted @ 2020-02-05 14:37 EM-LGH 阅读(145) 评论(0) 推荐(0)
摘要:复习一下线性基 ~ code: #include <cmath> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #define N 500008 #defin 阅读全文
posted @ 2020-01-10 10:53 EM-LGH 阅读(148) 评论(0) 推荐(0)
摘要:这个思路还是非常巧妙的. 困难在于我们需要同时维护以 $x$ 为分治中心,延伸出颜色相同/不同的最大值. 不同的话直接将权和相加,相同的话还需要减掉重复部分,这就比较难办. 但是我们发现,当以 $x$ 为分治中心时,$x$ 每一个儿子为根的子树的延伸颜色都是相同的. 所以我们可以将每一个点的所有儿子 阅读全文
posted @ 2019-12-31 15:05 EM-LGH 阅读(159) 评论(0) 推荐(0)
摘要:这个期望显然可以表示成总价值/总方案数. 然后我们用线段树依次维护 $\sum val[i]$,$\sum val[i]\times i$,$\sum val[i]\times i^2$ 即可. code: #include <cmath> #include <cstdio> #include <s 阅读全文
posted @ 2019-12-30 15:36 EM-LGH 阅读(169) 评论(0) 推荐(0)
摘要:一个区间在线段树中会被分成 $O(\log n)$ 块,对于每一块都开一个 $vector$ 记录一下. 然后在删除操作结束后如果影响到的区间变为全 $0$,就扫一遍对应的 $vector$. #include <cstdio> #include <vector> #include <string> 阅读全文
posted @ 2019-12-30 11:11 EM-LGH 阅读(152) 评论(0) 推荐(0)
摘要:code: #include <cstdio> #include <string> #include <algorithm> #define N 200007 #define ll long long using namespace std; namespace IO { void setIO(st 阅读全文
posted @ 2019-12-28 15:30 EM-LGH 阅读(115) 评论(0) 推荐(0)
摘要:用线段树模拟一下就好了~ code: #include <cstdio> #include <algorithm> #define lson ls[x] #define rson rs[x] #define N 10000006 #define ll long long #define setIO( 阅读全文
posted @ 2019-12-24 13:54 EM-LGH 阅读(187) 评论(0) 推荐(0)
摘要:非常好的一道思维题. code: #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson x<<1 #define rson x<<1|1 #define N 500010 #d 阅读全文
posted @ 2019-12-23 15:43 EM-LGH 阅读(124) 评论(0) 推荐(0)
摘要:code: #include <string> #include <cstring> #include <cstdio> #include <algorithm> #define N 50003 #define lson now<<1 #define rson now<<1|1 #define in 阅读全文
posted @ 2019-12-21 09:33 EM-LGH 阅读(151) 评论(0) 推荐(0)
摘要:然而这只是 70pts 的部分分,考场上没想到满分怎么做(现在也不会) code: #include <cstdio> #include <string> #include <stack> #include <queue> #include <cstring> #include <algorithm 阅读全文
posted @ 2019-12-21 08:38 EM-LGH 阅读(199) 评论(0) 推荐(0)
摘要:Description 影魔,奈文摩尔,据说有着一个诗人的灵魂。事实上,他吞噬的诗人灵魂早已成千上万。千百年来,他收集了各式各样 的灵魂,包括诗人、牧师、帝王、乞丐、奴隶、罪人,当然,还有英雄。每一个灵魂,都有着自己的战斗力,而影魔,靠 这些战斗力提升自己的攻击。奈文摩尔有 n 个灵魂,他们在影魔宽 阅读全文
posted @ 2019-12-18 18:10 EM-LGH 阅读(187) 评论(0) 推荐(0)
摘要:code: #include <bits/stdc++.h> #define N 100060 #define M 1000000 #define lson x<<1 #define rson x<<1|1 #define ll long long #define setIO(s) freopen( 阅读全文
posted @ 2019-12-18 10:35 EM-LGH 阅读(187) 评论(0) 推荐(0)