
数据结构——线段树&树状数组&分块
无
Sstee1XD
这个作者很懒,什么都没留下…
展开
-
洛谷P3605 [USACO17JAN]Promotion Counting P(离散化 + 树状数组 或 线段树合并)
题目链接 题意:给你一棵以111为根节点的树和每个点的点权,问在每个子树中,有多少个子节点的点权小于根节点。 题解: 首先对值进行离散后,之后有两种方法可以解决。 树状数组 求有几个比一个值小的值,想到了树状数组。对树从树根进行dfsdfsdfs,每次dfsdfsdfs到一个点的时候,先减去当前树状数组中比这个节点值小的个数,最后dfsdfsdfs完之后再加回去。答案 = - 祖先 + (祖先 + 子节点),所以得出了答案。 #include <bits/stdc++.h> using na原创 2020-11-18 13:13:48 · 267 阅读 · 0 评论 -
CodeForces - 600E Lomsat gelral(线段树合并)
题目链接 线段树的合并是从这里学的 题意 :给你一棵以111为根节点的树,树上每个点有一个颜色,用111~nnn的编号表示,问在以每个点为根节点的子树中,出现次数最多(可能有多种颜色出现次数一样)的颜色的编号和是多少。 题解:处理子树问题,并且大的子树的值可以由它的左右子树的值确定,所以想到了线段树的合并。空间复杂度在玄学的证明下大概是O(nlogn)O(nlogn)O(nlogn),但是好像会带个2的常数,所以要开2nlogn2nlogn2nlogn倍。1e51e51e5大概要开323232倍。 #inc原创 2020-11-17 20:30:43 · 268 阅读 · 0 评论 -
洛谷P1494 小Z的袜子(莫队板子)
题目链接 题意:求区间内任取两点,颜色相同的概率。 题解:对于一个[l,r][l, r][l,r]的区间,我们设一个num数组,num_i代表当前区间第i个颜色的数量,那么我们最终需要求出的原创 2020-11-12 09:06:22 · 260 阅读 · 0 评论 -
分块——以poj3468和洛谷P4168为例
当我们遇到简单的区间问题时,一般会想到树状数组或者线段树。但是当区间信息不符合区间可加可减性时,我们就要另寻他路了。这时我们可以选择用分块来解决问题。 分块要将你要处理的区间划分成长度大致相等的几块。为什么是大致相等呢?因为最后一个剩下的区间长度是难以控制的,剩下来的长度我们就让它自己成为一个区间,大概是这个样子。 然后我们记录下每个区间的左右极限,以及每个点所在的区间,比如点1在第一个区间,点5在第二个区间,之后在进行区间处理的时候,访问它们所在的区间。 读入数据后要先预处理 如果它们在一个区间内,例如原创 2020-10-30 22:12:42 · 342 阅读 · 0 评论 -
hdu - 3015 Disharmony Trees(树状数组 + 离散化)
题目连接 题意:给你n棵树的坐标和高度,分别按两个值升序排序后,得到每棵树的排名,分别为uiu_iui和viv_ivi,求∑i=1n∑j=i+1nabs(ui−uj)∗min(vi,vj)\sum_{i = 1}^n \sum_{j = i + 1}^n abs(u_i - u_j) * min(v_i, v_j)∑i=1n∑j=i+1nabs(ui−uj)∗min(vi,vj)。 题解:因为是区间求和,想到了树状数组。若想要一次性求一个区间,那么在这个区间中min(vi,vj)min(v_原创 2020-10-20 17:01:30 · 1737 阅读 · 6 评论 -
poj 2528 线段树的离散化
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placin原创 2020-07-21 09:13:57 · 246 阅读 · 0 评论 -
hdu 4578 -线段树的多种操作(要相信珂学)
Transformation Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations. Operation 1: Add c to each number between ax and ay inclusive. In oth原创 2020-07-05 18:17:03 · 271 阅读 · 0 评论 -
hdu1698-线段树模板
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on the ho原创 2020-07-04 21:07:14 · 332 阅读 · 0 评论