树的结构 数据结构_段树| 数据结构

树的结构 数据结构

What is a segment tree?

什么是段树?

A segment tree is a full binary tree where each node represents an interval. A node may store one or more data members of an interval which can be queried later.

段树是完整的二叉树,其中每个节点代表一个间隔。 节点可以存储一个或多个间隔的数据成员,以后可以查询该成员。

Why do we need Segment tree?

为什么我们需要细分树?

Many problems require that we give results based on query over a range or segment of available data. This can be the tedious and slow process, especially if the number of queries is large.

许多问题要求我们根据对可用数据范围或数据段的查询得出结果。 这可能是一个繁琐而缓慢的过程,尤其是在查询数量很大的情况下。

A segment tree lets us process such queries efficiently in logarithmic order of time.

段树使我们能够以对数时间顺序有效地处理此类查询。

How do we make a Segment tree?

我们如何制作细分树?

Let the data be in array arr[]

让数据位于数组arr []中

  1. The root of our tree will represent the entire interval of data we are interested in, i.e, arr[0...n-1].

    树的根将代表我们感兴趣的整个数据间隔,即arr [0 ... n-1] 。

  2. Each leaf of the tree will represent a range consisting of just a single element. Thus the leaves represent arr[0], arr[1], arr[2] ... arr[n-1].

    树的每片叶子将代表一个仅包含一个元素的范围。 因此,叶子代表arr [0] , arr [1] , arr [2] ... arr [n-1] 。

  3. The internal nodes will represent the merged result of their child nodes.

    内部节点将代表其子节点的合并结果。

  4. Each of the children nodes could represent approximately half of the range represented by their parent.

    每个子节点可代表其父节点所代表范围的大约一半。

Segment Tree generally contains three types of method: Build, Query, and Update.

细分树通常包含三种类型的方法:生成,查询和更新。

Let’s take an example:

让我们举个例子:

Problem: Range minimum query

问题:范围最小查询

You are given N numbers and Q queries. There are two types of queries.

系统会为您提供N个数字和Q个查询。 有两种类型的查询。

  1. Find the minimum number in range [l,r].

    在[l,r]范围内找到最小值。

  2. Update the element at ith position of array to val.

    将数组 i 位置的元素更新为val 。

Basic Approach:

基本方法:

We can find minimum element in O(N) and update the element in O(1). But if N or Q (query) is a very large number, it will be very slow.

我们可以在O(N)中找到最小元素,并在O(1)中更新元素。 但是,如果N或Q(查询)是一个非常大的数字,它将非常慢。

But it can be solved in logarithmic time with segment trees.

但这可以用段树在对数时间内解决。

  • The root of the tree is at index 1

    树的根在索引1处

  • The children then will be at 2*i and 2*i+1 index.

    然后,子级将位于2 * i和2 * i + 1索引处。

#include<iostream>
using namespace std;
int tree[400005]; // segment tree structure
int arr[100005];  // input tree
//Build
void build_tree(int node, int a, int b){
//node represent the current node number
// a,b represent the current node range
//for leaf a == b
if(a==b){
//for single element minimum will be the element itself
tree[node] = arr[a];
return;
}
int mid = (a + b) >> 1; // middle element
tree[node] =
build_tree(node*2,a,mid)+    // call for left half
build_tree(node*2+1,mid+1,b);//call for right half
}
//Query
int query_tree(int node, int a, int b, int i, int j){
//i, j represents the range to be queried
if(a > b || a > j || b < i){ return INT_MAX;} // out of range
if(a>= i && b<=j){
return tree[node];  //segment within range
}
int mid = a+ b >> 1;
int q1 = query_tree(node*2,a,mid,i,j);    //left child query
int q2 = query_tree(node*2+1,mid+1,b,i,j); // right child query
return q1>q2?q2:q1;
}
void update_tree(int node, int a,int b, int i, int val){
if(a>b||a>i||b<i){
return;//Out of range
}
if(a==b){
tree[node] = val;
return;
}
int mid = (a+b) >> 1;
tree[node] = update_tree(node*2,a,mid,i,val) +  // updating left child
update_tree(node*2+1,mid+1,b,val); //updating right child
}

翻译自: https://www.includehelp.com/data-structure-tutorial/segment-trees.aspx

树的结构 数据结构

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544029.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

iOS开发中 常用枚举和常用的一些运算符(易错总结)

1、色值的随机值&#xff1a;#define kColorValue arc4random_uniform(256)/255.0 // arc4random_uniform(256)/255.0; 求出0.0~1.0之间的数字view.backgroundColor [UIColor colorWithRed:kColorValue green: kColorValue blue: kColorValue alpha: 0.5]; 2、定时器的使用&…

明明加了唯一索引,为什么还是产生重复数据?

前段时间我踩过一个坑&#xff1a;在mysql8的一张innodb引擎的表中&#xff0c;加了唯一索引&#xff0c;但最后发现数据竟然还是重复了。到底怎么回事呢&#xff1f;本文通过一次踩坑经历&#xff0c;聊聊唯一索引&#xff0c;一些有意思的知识点。1.还原问题现场前段时间&…

python字符串 切片_用于切片字符串的Python程序

python字符串 切片Given a string and number of characters (N), we have to slice and print the starting N characters from the given string using python program. 给定一个字符串和字符数( N )&#xff0c;我们必须使用python程序从给定的字符串中切片并打印开始的N个字…

nmap入门之主机发现

2019独角兽企业重金招聘Python工程师标准>>> #主机发现&#xff08;HOST DISCOVERY&#xff09; ##仅列出IP&#xff0c;不扫描 nmap -sL 192.168.70.0/24 > nmap_result.txt 2>&1##仅ping扫描&#xff0c;不扫描端口 nmap -sn 192.168.70.0/24##不ping扫…

面试官:为什么ConcurrentHashMap要放弃分段锁?

今天我们来讨论一下一个比较经典的面试题就是 ConcurrentHashMap 为什么放弃使用了分段锁&#xff0c;这个面试题阿粉相信很多人肯定觉得有点头疼&#xff0c;因为很少有人在开发中去研究这块的内容&#xff0c;今天阿粉就来给大家讲一下这个 ConcurrentHashMap 为什么在 JDK8 …

ruby .each_Ruby中带有示例的Array.each方法

ruby .eachRuby Array.each方法 (Ruby Array.each method) Array.each method can be easily termed as a method which helps you to iterate over the Array. This method first processes the first element then goes on the second and the process keeps on going on unt…

面试突击72:输入URL之后会执行什么流程?

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在浏览器中输入 URL 之后&#xff0c;它会执行以下几个流程&#xff1a;执行 DNS 域名解析&#xff1b;封装 HTTP 请…

二层交换网络_网络中的第2层交换

二层交换网络二层交换简介 (Introduction to Layer 2 Switching) As you know hubs are not intelligent devices. Whenever a hub receives a frame, it broadcasts the frame in all ports. Also, the hub represents a single collision domain i.e. when any 2 hosts send …

最小化托盘示例工程

http://files.cnblogs.com/files/kekec2/BuyTicket.rar.gif转载于:https://www.cnblogs.com/kekec2/p/4914572.html

面试必备:TCP 经典 15 连问!

TCP协议是大厂面试必问的知识点。整理了15道非常经典的TCP面试题&#xff0c;希望大家都找到理想的offer呀1. 讲下TCP三次握手流程开始客户端和服务器都处于CLOSED状态&#xff0c;然后服务端开始监听某个端口&#xff0c;进入LISTEN状态第一次握手(SYN1, seqx)&#xff0c;发…

range函数python_range()函数以及Python中的示例

range函数pythonPython range()函数 (Python range() function) The range() is a built-in function in Python which returns the sequence of values. It is used where we need to perform a specific action for a limited number of times. In general, if we write rang…

ISP QoS Lab

ISP QoS Lab1-PQ优先级队列&#xff08;PQ&#xff0c;Priority Queue&#xff09;中&#xff0c;有高、中、普通、低优先级四个队列。数据包根据事先的定义放在不同的队列中&#xff0c;路由器按照高、中、普通、低顺序服务&#xff0c;只有高优先级的队列为空后才为中优先级的…

面渣逆袭:JVM经典五十问,这下面试稳了!

引言1.什么是JVM?JVM——Java虚拟机&#xff0c;它是Java实现平台无关性的基石。Java程序运行的时候&#xff0c;编译器将Java文件编译成平台无关的Java字节码文件&#xff08;.class&#xff09;,接下来对应平台JVM对字节码文件进行解释&#xff0c;翻译成对应平台匹配的机器…

操作系统大内核和微内核_操作系统中的内核类型

操作系统大内核和微内核As we have already studied about the Kernels, we know that the Kernel is a program which is the main component of the Operating System. Now let us study about the types of Kernels. 正如我们已经研究了内核一样 &#xff0c;我们知道内核是…

POJ 3268:Silver Cow Party 求单点的来回最短路径

Silver Cow PartyTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 15989 Accepted: 7303Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A t…

【论文解读】Learning based fast H.264 to H.265 transcoding

时间&#xff1a; 2015 年 级别&#xff1a;APSIPA 机构&#xff1a; 上海电力大学 摘要 新提出的视频编码标准HEVC (High Efficiency video coding)以其比H.264/AVC更好的编码效率&#xff0c;被工业界和学术界广泛接受和采用。在HEVC实现了约40%的编码效率提升的同时&#…

面试必备:聊聊sql优化的15个小技巧

sql优化是一个大家都比较关注的热门话题&#xff0c;无论你在面试&#xff0c;还是工作中&#xff0c;都很有可能会遇到。如果某天你负责的某个线上接口&#xff0c;出现了性能问题&#xff0c;需要做优化。那么你首先想到的很有可能是优化sql语句&#xff0c;因为它的改造成本…

Scala程序将字符串转换为整数

In Scala, there is a huge library to support different operations on a string. One such operation is to convert string to int in Scala. 在Scala中&#xff0c;有一个庞大的库来支持对字符串的不同操作。 一种这样的操作是在Scala中将字符串转换为int。 A string can…

面试突击73:IoC 和 DI 有什么区别?

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;IoC 和 DI 都是 Spring 框架中的重要概念&#xff0c;就像玫瑰花与爱情一样&#xff0c;IoC 和 DI 通常情况下也是成…

MyBatis整合Spring的实现(13)

2019独角兽企业重金招聘Python工程师标准>>> 本章中分析insert元素的解析。 1 配置文件 <insert id"insert" parameterType"cn.vansky.schedule.time.menu.bo.Menu"><!--WARNING - mbggeneratedThis element is automatically generat…