当前位置:网站首页>Based on Zeng Shen's explanation, the line segment tree is studied again one
Based on Zeng Shen's explanation, the line segment tree is studied again one
2022-06-26 10:23:00 【yumuluo】
Concept
A line tree is a binary tree , That is, for a line segment , We will use a binary tree to represent . For example, a length of 4 The line segment , We can express it like this :
Also, the length is n
( Ugly )
Generalization : node i A weight = Her left son + Her right son . because 1-4 The sum of is equal to 1-2 And 2-3 And
According to this idea , Then we can make achievements , Let's set up a structure tree
,tree[i].l and tree[i].r
Respectively represent the left and right subscripts of the line segment represented by this point ,tree[i].sum
Represents the line segment and... Represented by this node .
We know , A binary tree , Her left and right sons are numbered her respectively *2
And her *2+1
According to the nature just now , Get the formula :t r e e [ i ] . s u m = t r e e [ i ∗ 2 ] . s u m + t r e e [ i ∗ 2 + 1 ] . s u m ; tree[i].sum=tree[i*2].sum+tree[i*2+1].sum;tree[i].sum=tree[i∗2].sum+tree[i∗2+1].sum; You can build a segment tree !
The code is as follows :
inline void build(int i,int l,int r){// Recursive tree building
tree[i].l=l;tree[i].r=r;
if(l==r){// If this node is a leaf node
tree[i].sum=input[l];
return ;
}
int mid=(l+r)>>1;
build(i*2,l,mid);// Construct left subtree and right subtree respectively
build(i*2+1,mid+1,r);
tree[i].sum=tree[i*2].sum+tree[i*2+1].sum;// The properties we just found return ;
}
边栏推荐
猜你喜欢
3行3列整形二维数组,求对角之和
How to change the QR code material color of wechat applet
Problems encountered in the application and development of Hongmeng and some roast
Using foreach to loop two-dimensional array
Differences between JVM, Dalvik and art
Detailed explanation of winsorflow quantum installation process
Solution to network request crash in retrofit2.8.1
exec系列函数(execl、execlp、execle、execv、execvp)使用
MySQL第七次作业-更新数据
Allocation de mémoire tas lors de la création d'objets
随机推荐
The basis of C language grammar -- function definition learning
Under the double reduction, the amount of online education has plummeted. Share 12 interesting uses of webrtc
Poj3682 king arthur's birthday celebration (probability)
2. merge two ordered arrays
Introduction to stored procedure testing
Blog post index summary --c #
Blog article index summary -- Software Engineering
Reshape a two-dimensional array with 3 rows and 3 columns to find the sum of the diagonals
Global and Chinese market of aluminum sunshade systems 2022-2028: Research Report on technology, participants, trends, market size and share
904. 水果成篮
String constant pool, class constant pool, and runtime constant pool
存储过程测试入门案例
Internationalization configuration
Blog article index Summary - wechat games
Luogu 1146 coin flip
Extracting public fragments from thymeleaf
Solution to network request crash in retrofit2.8.1
MySQL第六章总结
MySQL learning summary
JVM的符号引用和直接引用是什么