线段树入门
<span data-docs-delta="[[20, "1、什么是线段树", "inline-dir:\"ltr\""], [20, "\n", "24:\"uP1k\"|32:2|direction:\"ltr\""], [20, "线段树是:", "0:\"rgb(255%2C%200%2C%200)\"|inline-dir:\"ltr\""], [20, "利用分治思想处理对一段序列进行大量区间操作(区间修改、区间查询)的数据结构", "0:\"rgb(255%2C%200%2C%200)\"|10:1|inline-dir:\"ltr\""], [20, "\n", "0:\"rgb(255%2C%200%2C%200)\"|24:\"S93S\"|33:1|direction:\"ltr\"|list-id:\"uT7T\"|ordered:\"ckj-decimal\""], [20, "线段树可以在0(nlogn)的时间复杂度内,完成区间修改/区间查询操作。", "0:\"rgb(255%2C%200%2C%200)\"|inline-dir:\"ltr\""], [20, "\n", "0:\"rgb(255%2C%200%2C%200)\"|24:\"TgIX\"|33:1|direction:\"ltr\"|list-id:\"uT7T\"|ordered:\"ckj-decimal\""]]" data-copy-origin="https://shimo.im">
1、什么是线段树
- 线段树是:利用分治思想处理对一段序列进行大量区间操作(区间修改、区间查询)的数据结构
<ol start="2">
<li class="" style="margin-bottom:0pt;margin-top:0pt;font-size:11pt;color:#494949;line-height:1.7;list-style-type:lower-alpha;">
线段树可以在0(nlogn)的时间复杂度内,完成区间修改/区间查询操作。
</li>
</ol>
<span style="color:#494949;"><span style="font-size:14.6667px;"><span data-docs-delta="[[20, "2、线段树的拆分原理", "inline-dir:\"ltr\""], [20, "\n", "24:\"qbNh\"|32:2|direction:\"ltr\""], [20, "将[1, n]分解成若干特定的子区间;", "inline-dir:\"ltr\""], [20, "\n", "24:\"7BT9\"|33:1|direction:\"ltr\"|list-id:\"NQjk\"|ordered:\"decimal\""], [20, "将每个区间[L, R]都分解为少量特定的子区间", "inline-dir:\"ltr\""], [20, "\n", "24:\"KZND\"|33:1|direction:\"ltr\"|list-id:\"NQjk\"|ordered:\"decimal\""], [20, "通过对子区间的修改或统计,来实现快速对[L,R]区间的修改、统计。", "inline-dir:\"ltr\""], [20, "\n", "24:\"aFHQ\"|33:1|direction:\"ltr\"|list-id:\"NQjk\"|ordered:\"decimal\""], [30, [{"A1":[40, [[20, "算法", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"Mn7O\"|7:1|direction:\"ltr\""]], "25:\"sUcZ\"|7:1"], "A2":[40, [[20, "前缀和", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"WBOm\"|7:1|direction:\"ltr\""]], "25:\"E8wb\"|7:1"], "A3":[40, [[20, "树状数组", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"TJH2\"|7:1|direction:\"ltr\""]], "25:\"o3KE\"|7:1"], "A4":[40, [[20, "线段树", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"gS6a\"|7:1|direction:\"ltr\""]], "25:\"T6P8\"|7:1"], "B1":[40, [[20, "区间求和", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"6DuN\"|7:1|direction:\"ltr\""]], "25:\"g8E2\"|7:1"], "B2":[40, [[20, "√", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"fHcn\"|7:1|direction:\"ltr\""]], "25:\"OHE7\"|7:1"], "B3":[40, [[20, "√", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"XYUc\"|7:1|direction:\"ltr\""]], "25:\"Ybgr\"|7:1"], "B4":[40, [[20, "√", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"ov4m\"|7:1|direction:\"ltr\""]], "25:\"Wdyw\"|7:1"], "C1":[40, [[20, "区间最值", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"Ie8V\"|7:1|direction:\"ltr\""]], "25:\"AvmG\"|7:1"], "C2":[40, [[20, "×", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"IrAR\"|7:1|direction:\"ltr\""]], "25:\"IERr\"|7:1"], "C3":[40, [[20, "√", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"0ohB\"|7:1|direction:\"ltr\""]], "25:\"Gwsj\"|7:1"], "C4":[40, [[20, "√", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"okk3\"|7:1|direction:\"ltr\""]], "25:\"N54J\"|7:1"], "D1":[40, [[20, "单点修改", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"48YT\"|7:1|direction:\"ltr\""]], "25:\"EGjx\"|7:1"], "D2":[40, [[20, "×", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"ezLL\"|7:1|direction:\"ltr\""]], "25:\"Quok\"|7:1"], "D3":[40, [[20, "√", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"kHis\"|7:1|direction:\"ltr\""]], "25:\"3Eoo\"|7:1"], "D4":[40, [[20, "√", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"hqV2\"|7:1|direction:\"ltr\""]], "25:\"1E0q\"|7:1"], "E1":[40, [[20, "区间修改", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"MVkG\"|7:1|direction:\"ltr\""]], "25:\"Km1S\"|7:1"], "E2":[40, [[20, "×", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"Lhtc\"|7:1|direction:\"ltr\""]], "25:\"k1EH\"|7:1"], "E3":[40, [[20, "×", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"urbn\"|7:1|direction:\"ltr\""]], "25:\"8gge\"|7:1"], "E4":[40, [[20, "√", "26:\"20775936\"|inline-dir:\"ltr\""], [20, "\n", "24:\"dAzH\"|7:1|direction:\"ltr\""]], "25:\"WA9n\"|7:1"]}, [[10, 4, "26:\"20775936\""]], [[10, 1, "26:\"20775936\"|3:124"], [10, 4, "26:\"20775936\"|3:123"]]], "25:\"lJcWuB\"|readOnly:false"]]" data-copy-origin="https://shimo.im">
<h2 class="ql-long-20775936">
<div data-header="2" ql-global-para="true" line="qbNh" class="ql-direction-ltr ql-long-20775936" data-foldable="true" data-default-linespacing="100" style="line-height:100%;font-size:14pt;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">2、线段树的拆分原理</span>
</div>
</h2>
<ol start="2">
<li class="" style="margin-bottom:0pt;margin-top:0pt;font-size:11pt;color:#494949;line-height:1.7;list-style-type:lower-alpha;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">将[1, n]分解成若干特定的子区间;</span>
</li>
<li class="" style="margin-bottom:0pt;margin-top:0pt;font-size:11pt;color:#494949;line-height:1.7;list-style-type:lower-alpha;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">将每个区间[L, R]都分解为少量特定的子区间</span>
</li>
<li class="" style="margin-bottom:0pt;margin-top:0pt;font-size:11pt;color:#494949;line-height:1.7;list-style-type:lower-alpha;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">通过对子区间的修改或统计,来实现快速对[L,R]区间的修改、统计。</span>
</li>
</ol>
<table style="border-spacing:0px;">
<tbody>
<tr height="30" style="height:30px;">
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="sUcZ" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="124">
<p align="center" line="Mn7O" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">算法</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="g8E2" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="6DuN" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">区间求和</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="AvmG" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="Ie8V" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">区间最值</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="EGjx" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="48YT" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">单点修改</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="Km1S" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="MVkG" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">区间修改</span>
<br />
</td>
</tr>
<tr height="30" style="height:30px;">
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="E8wb" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="124">
<p align="center" line="WBOm" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">前缀和</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="OHE7" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="fHcn" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">√</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="IERr" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="IrAR" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">×</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="Quok" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="ezLL" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">×</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="k1EH" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="Lhtc" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">×</span>
<br />
</td>
</tr>
<tr height="30" style="height:30px;">
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="o3KE" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="124">
<p align="center" line="TJH2" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">树状数组</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="Ybgr" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="XYUc" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">√</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="Gwsj" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="0ohB" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">√</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="3Eoo" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="kHis" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">√</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="8gge" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="urbn" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">×</span>
<br />
</td>
</tr>
<tr height="30" style="height:30px;">
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="T6P8" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="124">
<p align="center" line="gS6a" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">线段树</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="Wdyw" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="ov4m" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">√</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="N54J" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="okk3" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">√</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="1E0q" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="hqV2" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">√</span>
<br />
</td>
<td spellcheck="false" autocorrect="off" autocomplete="off" class="ql-sheet-cell" rowspan="1" colspan="1" guid="WA9n" style="min-height:30px;border-width:1px;border-style:solid;vertical-align:top;padding:4px 0px;" width="123">
<p align="center" line="dAzH" class="ql-align-center ql-direction-ltr" style="line-height:100%;margin-bottom:0pt;margin-top:0pt;text-align:center;font-size:11pt;color:#494949;">
<span class="ql-author-20775936" inline-dir="ltr" ql-global="true">√</span>
<br />
</td>
</tr>
</tbody>
</table>
</span></span></span>
1
2
3
4
1 2 3 4
1 2 3 4 5 6 7
画图模拟查询
query(1, 10) query(4, 6)
query(1, 5) query(4, 8)
query(4, 5) query(5, 6)
</span>
//建树
void build(int k, int l, int r){
//如果是叶子节点
if(l==r){
ma[k]=a[i];
return;
}
int mid=l+r>>1;
build(k*2, l, mid); //构造左子树
build(k*2+1.mid+1, r); //构造右子树
//利用字节点更新父节点
ma[k]=max(ma[k*2], ma[k*2+1]);
}
//修改:将p点的值修改为v
viod change(int k, int l, int r, int p, int v){
if(r<p||l>p) return;//当前区间完全不包含需要修改的点
//叶结点,直接修改
if(l==p&&r==p){
a[k]=v;
return;
}
int mid=l+r>>1;
if(p<=mid) change(k*2, mid, p, v)
else change(k*2+1, mid+1, r, p, v);
//回溯更新对应结点的值
a[k]=max(a[k*2], a[k*2+1]);
}
</span></span></span>
//在下标为k的位置(对应区间[l, r])
//查询范围是[x, y]
int query(int k, int l, int r, int x, int y){
//如果当前区间完全在查询范围内
if(x<=1&&y>=r) return a[k];
int mid=l+r>>1;
int res=0;
//左一半有覆盖
if(x<=mid) res=query(k*2, l, mid, x, y);
//右一半有部分覆盖
if(y>=mid+1) res=max(res, query(k*2+1, mid+1, r, x, y));
return res;
}
</span>
- 状态
- 已结束
- 规则
- ACM/ICPC
- 题目
- 4
- 开始于
- 2024-4-20 17:00
- 结束于
- 2024-6-19 10:00
- 持续时间
- 1433 小时
- 主持人
- 参赛人数
- 6