123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
- <meta name="theme-color" content="#222">
- <meta name="generator" content="Hexo 4.2.0">
- <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
- <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
- <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
- <link rel="mask-icon" href="/images/logo.svg" color="#222">
- <link rel="stylesheet" href="/css/main.css">
- <link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
- <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
- <script src="/lib/pace/pace.min.js"></script>
- <script id="hexo-configurations">
- var NexT = window.NexT || {};
- var CONFIG = {"hostname":"schtonn.github.io","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
- </script>
- <meta name="description" content="前置知识 数组,结构体,二叉树 引入 有时候我们会遇到一些大规模的区间查找和区间修改问题,比如让你维护一个 10510^5105 长度的数列,要求操作有区间求和、区间加(区间每个数加上一个值),让你在一秒内完成 10510^5105 次操作。 暴力是肯定不行的,数据范围太大,操作太多,会超时。 所以我们就有一种专门解决大范围区间修改查询的数据结构:线段树。">
- <meta property="og:type" content="article">
- <meta property="og:title" content="线段树">
- <meta property="og:url" content="https://schtonn.github.io/posts/segment-tree/index.html">
- <meta property="og:site_name" content="Alex's Blog">
- <meta property="og:description" content="前置知识 数组,结构体,二叉树 引入 有时候我们会遇到一些大规模的区间查找和区间修改问题,比如让你维护一个 10510^5105 长度的数列,要求操作有区间求和、区间加(区间每个数加上一个值),让你在一秒内完成 10510^5105 次操作。 暴力是肯定不行的,数据范围太大,操作太多,会超时。 所以我们就有一种专门解决大范围区间修改查询的数据结构:线段树。">
- <meta property="og:locale" content="en_US">
- <meta property="og:image" content="https://schtonn.github.io/images/segment-1.png">
- <meta property="article:published_time" content="2020-03-02T03:37:36.000Z">
- <meta property="article:modified_time" content="2020-03-29T07:59:45.949Z">
- <meta property="article:author" content="Alex">
- <meta property="article:tag" content="graph">
- <meta name="twitter:card" content="summary">
- <meta name="twitter:image" content="https://schtonn.github.io/images/segment-1.png">
- <link rel="canonical" href="https://schtonn.github.io/posts/segment-tree/">
- <script id="page-configurations">
- // https://hexo.io/docs/variables.html
- CONFIG.page = {
- sidebar: "",
- isHome : false,
- isPost : true,
- lang : 'en'
- };
- </script>
- <title>线段树 | Alex's Blog</title>
-
- <noscript>
- <style>
- .use-motion .brand,
- .use-motion .menu-item,
- .sidebar-inner,
- .use-motion .post-block,
- .use-motion .pagination,
- .use-motion .comments,
- .use-motion .post-header,
- .use-motion .post-body,
- .use-motion .collection-header { opacity: initial; }
- .use-motion .site-title,
- .use-motion .site-subtitle {
- opacity: initial;
- top: initial;
- }
- .use-motion .logo-line-before i { left: initial; }
- .use-motion .logo-line-after i { right: initial; }
- </style>
- </noscript>
- </head>
- <body itemscope itemtype="http://schema.org/WebPage">
- <div class="container use-motion">
- <div class="headband"></div>
- <header class="header" itemscope itemtype="http://schema.org/WPHeader">
- <div class="header-inner"><div class="site-brand-container">
- <div class="site-nav-toggle">
- <div class="toggle" aria-label="Toggle navigation bar">
- <span class="toggle-line toggle-line-first"></span>
- <span class="toggle-line toggle-line-middle"></span>
- <span class="toggle-line toggle-line-last"></span>
- </div>
- </div>
- <div class="site-meta">
- <a href="/" class="brand" rel="start">
- <span class="logo-line-before"><i></i></span>
- <h1 class="site-title">Alex's Blog</h1>
- <span class="logo-line-after"><i></i></span>
- </a>
- <p class="site-subtitle" itemprop="description">Middle school OIer</p>
- </div>
- <div class="site-nav-right">
- <div class="toggle popup-trigger">
- </div>
- </div>
- </div>
- <nav class="site-nav">
- <ul id="menu" class="menu">
- <li class="menu-item menu-item-home">
- <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>
- </li>
- <li class="menu-item menu-item-tags">
- <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>Tags</a>
- </li>
- <li class="menu-item menu-item-archives">
- <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>
- </li>
- <li class="menu-item menu-item-games">
- <a href="/games/" rel="section"><i class="fa fa-fw fa-gamepad"></i>Games</a>
- </li>
- <li class="menu-item menu-item-sans">
- <a href="/sans/" rel="section"><i class="fa fa-fw fa-heart"></i>sans</a>
- </li>
- </ul>
- </nav>
- </div>
- </header>
-
- <div class="back-to-top">
- <i class="fa fa-arrow-up"></i>
- <span>0%</span>
- </div>
- <div class="reading-progress-bar"></div>
- <main class="main">
- <div class="main-inner">
- <div class="content-wrap">
-
- <div class="content post posts-expand">
-
-
-
-
- <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
- <link itemprop="mainEntityOfPage" href="https://schtonn.github.io/posts/segment-tree/">
- <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
- <meta itemprop="image" content="/images/avatar.gif">
- <meta itemprop="name" content="Alex">
- <meta itemprop="description" content="The blog of a middle school OIer">
- </span>
- <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
- <meta itemprop="name" content="Alex's Blog">
- </span>
- <header class="post-header">
- <h1 class="post-title" itemprop="name headline">
- 线段树
- </h1>
- <div class="post-meta">
- <span class="post-meta-item">
- <span class="post-meta-item-icon">
- <i class="fa fa-calendar-o"></i>
- </span>
- <span class="post-meta-item-text">Posted on</span>
- <time title="Created: 2020-Mar-02 11:37:36" itemprop="dateCreated datePublished" datetime="2020-03-02T11:37:36+08:00">2020-Mar-02</time>
- </span>
- <span class="post-meta-item">
- <span class="post-meta-item-icon">
- <i class="fa fa-calendar-check-o"></i>
- </span>
- <span class="post-meta-item-text">Edited on</span>
- <time title="Modified: 2020-Mar-29 15:59:45" itemprop="dateModified" datetime="2020-03-29T15:59:45+08:00">2020-Mar-29</time>
- </span>
-
- <span id="/posts/segment-tree/" class="post-meta-item leancloud_visitors" data-flag-title="线段树" title="Views">
- <span class="post-meta-item-icon">
- <i class="fa fa-eye"></i>
- </span>
- <span class="post-meta-item-text">Views: </span>
- <span class="leancloud-visitors-count"></span>
- </span>
-
- <span class="post-meta-item">
-
- <span class="post-meta-item-icon">
- <i class="fa fa-comment-o"></i>
- </span>
- <span class="post-meta-item-text">Valine: </span>
-
- <a title="valine" href="/posts/segment-tree/#valine-comments" itemprop="discussionUrl">
- <span class="post-comments-count valine-comment-count" data-xid="/posts/segment-tree/" itemprop="commentCount"></span>
- </a>
- </span>
-
-
- </div>
- </header>
-
-
-
- <div class="post-body" itemprop="articleBody">
-
- <h3 id="前置知识"><a class="markdownIt-Anchor" href="#前置知识"></a> 前置知识</h3>
- <p>数组,结构体,二叉树</p>
- <h3 id="引入"><a class="markdownIt-Anchor" href="#引入"></a> 引入</h3>
- <p>有时候我们会遇到一些大规模的区间查找和区间修改问题,比如让你维护一个 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>1</mn><msup><mn>0</mn><mn>5</mn></msup></mrow><annotation encoding="application/x-tex">10^5</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">5</span></span></span></span></span></span></span></span></span></span></span> 长度的数列,要求操作有区间求和、区间加(区间每个数加上一个值),让你在一秒内完成 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>1</mn><msup><mn>0</mn><mn>5</mn></msup></mrow><annotation encoding="application/x-tex">10^5</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">5</span></span></span></span></span></span></span></span></span></span></span> 次操作。<br />
- 暴力是肯定不行的,数据范围太大,操作太多,会超时。<br />
- 所以我们就有一种专门解决大范围区间修改查询的数据结构:线段树。</p>
- <a id="more"></a>
- <h3 id="线段树"><a class="markdownIt-Anchor" href="#线段树"></a> 线段树</h3>
- <p>线段树本质上是把整个数列拆分了,用一个一个区间来表示。</p>
- <p>比如有 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>n</mi></mrow><annotation encoding="application/x-tex">n</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">n</span></span></span></span> 个节点,根节点代表整个数列的区间 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>1</mn><mo separator="true">,</mo><mi>n</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[1,n]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathdefault">n</span><span class="mclose">]</span></span></span></span>,根节点的两个子节点代表根区间二分成两部分,也就是 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>1</mn><mo separator="true">,</mo><mi mathvariant="normal">mid</mi><mo></mo><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[1,\operatorname{mid}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mop"><span class="mord mathrm">m</span><span class="mord mathrm">i</span><span class="mord mathrm">d</span></span><span class="mclose">]</span></span></span></span> 和 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mi mathvariant="normal">mid</mi><mo></mo><mo separator="true">,</mo><mi>n</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[\operatorname{mid},n]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mop"><span class="mord mathrm">m</span><span class="mord mathrm">i</span><span class="mord mathrm">d</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathdefault">n</span><span class="mclose">]</span></span></span></span>。<br />
- 而子节点也是一棵线段树。<br />
- 一直往下延伸,叶子结点就代表着单一位置 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mi>a</mi><mo separator="true">,</mo><mi>a</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[a,a]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord mathdefault">a</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathdefault">a</span><span class="mclose">]</span></span></span></span>。<br />
- 下图是一个 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>n</mi><mo>=</mo><mn>7</mn></mrow><annotation encoding="application/x-tex">n=7</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">n</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">7</span></span></span></span> 的示例:</p>
- <p><img data-src="/images/segment-1.png" alt="segment-1" /></p>
- <p>每一个节点都存储着它所代表的区间的信息,比如这个区间的和。</p>
- <h3 id="查找"><a class="markdownIt-Anchor" href="#查找"></a> 查找</h3>
- <h4 id="解释"><a class="markdownIt-Anchor" href="#解释"></a> 解释</h4>
- <p>如查找 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mi>l</mi><mo separator="true">,</mo><mi>r</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[l,r]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord mathdefault" style="margin-right:0.01968em;">l</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathdefault" style="margin-right:0.02778em;">r</span><span class="mclose">]</span></span></span></span>,则执行以下步骤:</p>
- <ol>
- <li>首先在根节点查找 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mi>l</mi><mo separator="true">,</mo><mi>r</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[l,r]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord mathdefault" style="margin-right:0.01968em;">l</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathdefault" style="margin-right:0.02778em;">r</span><span class="mclose">]</span></span></span></span></li>
- <li>判断当前所在区间是否在当前查找的区间内部,若在内部则直接返回当前区间数据。</li>
- <li>若当前区间的<strong>右子节点区间的左边界</strong>在<strong>当前查找的区间的左边界</strong>的右侧,说明当前查找区间完全在右子节点一侧,则返回在右子节点查找 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mi>l</mi><mo separator="true">,</mo><mi>r</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[l,r]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord mathdefault" style="margin-right:0.01968em;">l</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathdefault" style="margin-right:0.02778em;">r</span><span class="mclose">]</span></span></span></span> 的结果。</li>
- <li>否则,若当前区间的<strong>左子节点区间的右边界</strong>在<strong>当前查找区间的右边界</strong>的左侧,说明当前查找区间完全在左子节点一侧,则返回在左子节点查找 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mi>l</mi><mo separator="true">,</mo><mi>r</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[l,r]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord mathdefault" style="margin-right:0.01968em;">l</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathdefault" style="margin-right:0.02778em;">r</span><span class="mclose">]</span></span></span></span> 的结果。</li>
- <li>否则,说明当前查找区间横跨左右子节点,那么返回:在左子节点查找<strong>当前查找区间的左边界-左子节点区间的右边界</strong>与在右子节点查找<strong>右子节点的左边界-当前查找区间的右边界</strong>结果的和。</li>
- </ol>
- <h4 id="举例"><a class="markdownIt-Anchor" href="#举例"></a> 举例</h4>
- <p>设若查找区间 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>3</mn><mo separator="true">,</mo><mn>6</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[3,6]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">3</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">6</span><span class="mclose">]</span></span></span></span>,首先从根节点开始:<br />
- -当前区间 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>1</mn><mo separator="true">,</mo><mn>7</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[1,7]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">7</span><span class="mclose">]</span></span></span></span> 不在 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>3</mn><mo separator="true">,</mo><mn>6</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[3,6]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">3</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">6</span><span class="mclose">]</span></span></span></span> 内部,经过判断 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>3</mn><mo separator="true">,</mo><mn>6</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[3,6]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">3</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">6</span><span class="mclose">]</span></span></span></span> 横跨左右子节点,分别在左右子节点查找查找:<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>3</mn><mo separator="true">,</mo><mn>3</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[3,3]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">3</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">3</span><span class="mclose">]</span></span></span></span> 和 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>4</mn><mo separator="true">,</mo><mn>6</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[4,6]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">4</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">6</span><span class="mclose">]</span></span></span></span>。<br />
- -于是在 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>1</mn><mo separator="true">,</mo><mn>3</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[1,3]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">3</span><span class="mclose">]</span></span></span></span> 内查找 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>3</mn><mo separator="true">,</mo><mn>3</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[3,3]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">3</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">3</span><span class="mclose">]</span></span></span></span>,经过两次下放到右子树最终返回。<br />
- -接着在 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>4</mn><mo separator="true">,</mo><mn>7</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[4,7]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">4</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">7</span><span class="mclose">]</span></span></span></span> 内查找 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>4</mn><mo separator="true">,</mo><mn>6</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[4,6]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">4</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">6</span><span class="mclose">]</span></span></span></span>,<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>4</mn><mo separator="true">,</mo><mn>6</mn></mrow><annotation encoding="application/x-tex">4,6</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8388800000000001em;vertical-align:-0.19444em;"></span><span class="mord">4</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">6</span></span></span></span> 横跨左右子树,继续查找:<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>4</mn><mo>−</mo><mn>5</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[4-5]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">4</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">5</span><span class="mclose">]</span></span></span></span> 和 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>6</mn><mo>−</mo><mn>6</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[6-6]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">6</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">6</span><span class="mclose">]</span></span></span></span>。<br />
- -进入 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>4</mn><mo separator="true">,</mo><mn>5</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[4,5]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">4</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">5</span><span class="mclose">]</span></span></span></span>,在区间内部,直接返回。由 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>6</mn><mo separator="true">,</mo><mn>7</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[6,7]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">6</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">7</span><span class="mclose">]</span></span></span></span> 进入 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>6</mn><mo separator="true">,</mo><mn>6</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[6,6]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">6</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">6</span><span class="mclose">]</span></span></span></span> 后返回,查找完毕。</p>
- <h3 id="修改"><a class="markdownIt-Anchor" href="#修改"></a> 修改</h3>
- <p>我们会发现,修改时如果像查找一样做,那么有一些细小的叶子修改就改不到,如刚才举例的 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>4</mn><mo separator="true">,</mo><mn>5</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[4,5]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">4</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">5</span><span class="mclose">]</span></span></span></span>,如果只修改 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>4</mn><mo separator="true">,</mo><mn>5</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[4,5]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">4</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">5</span><span class="mclose">]</span></span></span></span> 区间,那么单独查询 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>4</mn><mo separator="true">,</mo><mn>4</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[4,4]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">4</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">4</span><span class="mclose">]</span></span></span></span> 和 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">[</mo><mn>5</mn><mo separator="true">,</mo><mn>5</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[5,5]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">5</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">5</span><span class="mclose">]</span></span></span></span> 的时候就会出错。要是每一个修改都下放到叶子节点,那这个算法就和暴力一样了。所以我们需要一些巧妙地解决办法。</p>
- <h4 id="lazy"><a class="markdownIt-Anchor" href="#lazy"></a> lazy</h4>
- <p>我们在树的节点上加一个标记:<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mtext mathvariant="monospace">lazy</mtext></mrow><annotation encoding="application/x-tex">\texttt{lazy}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.83333em;vertical-align:-0.22222em;"></span><span class="mord text"><span class="mord texttt">lazy</span></span></span></span></span>。</p>
- <p>所谓 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mtext mathvariant="monospace">lazy</mtext></mrow><annotation encoding="application/x-tex">\texttt{lazy}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.83333em;vertical-align:-0.22222em;"></span><span class="mord text"><span class="mord texttt">lazy</span></span></span></span></span>,就是要懒,就是要在事情迫不得已要做的时候把它做了,所以我们每次那些细小的叶子修改就不做他,直接记录在 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mtext mathvariant="monospace">lazy</mtext></mrow><annotation encoding="application/x-tex">\texttt{lazy}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.83333em;vertical-align:-0.22222em;"></span><span class="mord text"><span class="mord texttt">lazy</span></span></span></span></span> 标记上,等到要查询的时候,再从标记上下放到子结点上,查询到哪里就下放到哪里。</p>
- <h4 id="注意"><a class="markdownIt-Anchor" href="#注意"></a> 注意</h4>
- <p>修改后的结点,需要同时把所有祖先结点全部更新。</p>
- <h3 id="优势"><a class="markdownIt-Anchor" href="#优势"></a> 优势</h3>
- <p>暴力算法的复杂度是 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mi>m</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">O(nm)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.02778em;">O</span><span class="mopen">(</span><span class="mord mathdefault">n</span><span class="mord mathdefault">m</span><span class="mclose">)</span></span></span></span> 的,而线段树的复杂度是 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mi>log</mi><mo></mo><mi>m</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">O(n\log m)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.02778em;">O</span><span class="mopen">(</span><span class="mord mathdefault">n</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mop">lo<span style="margin-right:0.01389em;">g</span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathdefault">m</span><span class="mclose">)</span></span></span></span> 的,因为每次树深入一层,区间长度都会减半。</p>
- <h3 id="代码luogu-p3372"><a class="markdownIt-Anchor" href="#代码luogu-p3372"></a> 代码(<a href="https://www.luogu.com.cn/problem/P3372" target="_blank" rel="noopener">luogu P3372</a>)</h3>
- <figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"iostream"</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 1000010</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> lson id<<1 <span class="comment">//此处偷了懒,因为完全二叉树的性质可以推出,左子树编号是根节点的一倍,右子树是根节点一倍加一,</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rson id<<1|1 <span class="comment">//使用位运算提高速度。</span></span></span><br><span class="line"><span class="keyword">int</span> a[N],n,m,op,x,y,k;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span> {</span></span><br><span class="line"> <span class="keyword">int</span> l,r,sum,lazy;</span><br><span class="line">}t[<span class="number">2</span>*N];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">(<span class="keyword">int</span> id)</span></span>{<span class="comment">//更新函数</span></span><br><span class="line"> t[id].sum=t[lson].sum+t[lson].lazy*(t[lson].r-t[lson].l+<span class="number">1</span>)+</span><br><span class="line"> t[rson].sum+t[rson].lazy*(t[rson].r-t[rson].l+<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">pushdown</span><span class="params">(<span class="keyword">int</span> id)</span></span>{<span class="comment">//下推函数</span></span><br><span class="line"> <span class="keyword">if</span>(t[id].lazy){</span><br><span class="line"> t[lson].lazy+=t[id].lazy;</span><br><span class="line"> t[rson].lazy+=t[id].lazy;</span><br><span class="line"> t[id].sum+=t[id].lazy*(t[id].r-t[id].l+<span class="number">1</span>);</span><br><span class="line"> t[id].lazy=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">buildtree</span><span class="params">(<span class="keyword">int</span> id,<span class="keyword">int</span> l,<span class="keyword">int</span> r)</span></span>{<span class="comment">//建树</span></span><br><span class="line"> t[id].l=l;</span><br><span class="line"> t[id].r=r;</span><br><span class="line"> t[id].lazy=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(l==r){</span><br><span class="line"> t[id].sum=a[l];</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> mid=(l+r)>><span class="number">1</span>;</span><br><span class="line"> buildtree(lson,l,mid);</span><br><span class="line"> buildtree(rson,mid+<span class="number">1</span>,r);</span><br><span class="line"> update(id);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">change</span><span class="params">(<span class="keyword">int</span> id,<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> c)</span></span>{<span class="comment">//修改</span></span><br><span class="line"> <span class="keyword">if</span>(t[id].l>=l&&t[id].r<=r){</span><br><span class="line"> t[id].lazy+=c;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> pushdown(id);</span><br><span class="line"> <span class="keyword">if</span>(t[lson].r>=r)change(lson,l,r,c);</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(t[rson].l<=l)change(rson,l,r,c);</span><br><span class="line"> <span class="keyword">else</span>{</span><br><span class="line"> change(lson,l,t[lson].r,c);</span><br><span class="line"> change(rson,t[rson].l,r,c);</span><br><span class="line"> }</span><br><span class="line"> update(id);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">query</span><span class="params">(<span class="keyword">int</span> id,<span class="keyword">int</span> l,<span class="keyword">int</span> r)</span></span>{<span class="comment">//求和</span></span><br><span class="line"> <span class="keyword">if</span>(t[id].l>=l&&t[id].r<=r){</span><br><span class="line"> <span class="keyword">return</span> t[id].sum+t[id].lazy*(t[id].r-t[id].l+<span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> pushdown(id);</span><br><span class="line"> <span class="keyword">if</span>(t[lson].r>=r)<span class="keyword">return</span> query(lson,l,r);</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(t[rson].l<=l)<span class="keyword">return</span> query(rson,l,r);</span><br><span class="line"> <span class="keyword">else</span>{</span><br><span class="line"> <span class="keyword">return</span> query(lson,l,t[lson].r)+query(rson,t[rson].l,r);</span><br><span class="line"> }</span><br><span class="line"> update(id);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="built_in">cin</span>>>n>>m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> <span class="built_in">cin</span>>>a[i];</span><br><span class="line"> }</span><br><span class="line"> buildtree(<span class="number">1</span>,<span class="number">1</span>,n);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=m;i++){</span><br><span class="line"> <span class="built_in">cin</span>>>op;</span><br><span class="line"> <span class="keyword">if</span>(op==<span class="number">1</span>){</span><br><span class="line"> <span class="built_in">cin</span>>>x>>y>>k;</span><br><span class="line"> change(<span class="number">1</span>,x,y,k);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span>{</span><br><span class="line"> <span class="built_in">cin</span>>>x>>y;</span><br><span class="line"> <span class="built_in">cout</span><<query(<span class="number">1</span>,x,y)<<<span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
- </div>
-
-
-
-
- <div>
- <ul class="post-copyright">
- <li class="post-copyright-author">
- <strong>Post author: </strong>Alex
- </li>
- <li class="post-copyright-link">
- <strong>Post link: </strong>
- <a href="https://schtonn.github.io/posts/segment-tree/" title="线段树">https://schtonn.github.io/posts/segment-tree/</a>
- </li>
- <li class="post-copyright-license">
- <strong>Copyright Notice: </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> unless stating additionally.
- </li>
- </ul>
- </div>
- <footer class="post-footer">
-
- <div class="post-tags">
- <a href="/tags/graph/" rel="tag"><i class="fa fa-tag"></i> graph</a>
- </div>
-
-
- <div class="post-nav">
- <div class="post-nav-item">
- <a href="/posts/fibonacci/" rel="prev" title="斐波那契数列-O(1)">
- <i class="fa fa-chevron-left"></i> 斐波那契数列-O(1)
- </a></div>
- <div class="post-nav-item">
- <a href="/posts/computer/" rel="next" title="How do computers work?">
- How do computers work? <i class="fa fa-chevron-right"></i>
- </a></div>
- </div>
- </footer>
-
- </article>
-
-
-
- </div>
-
- <div class="comments" id="valine-comments"></div>
- <script>
- window.addEventListener('tabs:register', () => {
- let { activeClass } = CONFIG.comments;
- if (CONFIG.comments.storage) {
- activeClass = localStorage.getItem('comments_active') || activeClass;
- }
- if (activeClass) {
- let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
- if (activeTab) {
- activeTab.click();
- }
- }
- });
- if (CONFIG.comments.storage) {
- window.addEventListener('tabs:click', event => {
- if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
- let commentClass = event.target.classList[1];
- localStorage.setItem('comments_active', commentClass);
- });
- }
- </script>
- </div>
-
-
- <div class="toggle sidebar-toggle">
- <span class="toggle-line toggle-line-first"></span>
- <span class="toggle-line toggle-line-middle"></span>
- <span class="toggle-line toggle-line-last"></span>
- </div>
- <aside class="sidebar">
- <div class="sidebar-inner">
- <ul class="sidebar-nav motion-element">
- <li class="sidebar-nav-toc">
- Table of Contents
- </li>
- <li class="sidebar-nav-overview">
- Overview
- </li>
- </ul>
- <!--noindex-->
- <div class="post-toc-wrap sidebar-panel">
- <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#前置知识"><span class="nav-number">1.</span> <span class="nav-text"> 前置知识</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#引入"><span class="nav-number">2.</span> <span class="nav-text"> 引入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#线段树"><span class="nav-number">3.</span> <span class="nav-text"> 线段树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查找"><span class="nav-number">4.</span> <span class="nav-text"> 查找</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#解释"><span class="nav-number">4.1.</span> <span class="nav-text"> 解释</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#举例"><span class="nav-number">4.2.</span> <span class="nav-text"> 举例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#修改"><span class="nav-number">5.</span> <span class="nav-text"> 修改</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#lazy"><span class="nav-number">5.1.</span> <span class="nav-text"> lazy</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#注意"><span class="nav-number">5.2.</span> <span class="nav-text"> 注意</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#优势"><span class="nav-number">6.</span> <span class="nav-text"> 优势</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#代码luogu-p3372"><span class="nav-number">7.</span> <span class="nav-text"> 代码(luogu P3372)</span></a></li></ol></div>
- </div>
- <!--/noindex-->
- <div class="site-overview-wrap sidebar-panel">
- <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
- <p class="site-author-name" itemprop="name">Alex</p>
- <div class="site-description" itemprop="description">The blog of a middle school OIer</div>
- </div>
- <div class="site-state-wrap motion-element">
- <nav class="site-state">
- <div class="site-state-item site-state-posts">
- <a href="/archives">
- <span class="site-state-item-count">10</span>
- <span class="site-state-item-name">posts</span>
- </a>
- </div>
- <div class="site-state-item site-state-tags">
- <a href="/tags/">
- <span class="site-state-item-count">4</span>
- <span class="site-state-item-name">tags</span></a>
- </div>
- </nav>
- </div>
- <div class="links-of-author motion-element">
- <span class="links-of-author-item">
- <a href="https://github.com/schtonn" title="GitHub → https://github.com/schtonn" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
- </span>
- <span class="links-of-author-item">
- <a href="mailto:m18519511495@163.com" title="E-Mail → mailto:m18519511495@163.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
- </span>
- </div>
- <div class="links-of-blogroll motion-element">
- <div class="links-of-blogroll-title">
- <i class="fa fa-fw fa-link"></i>
- Links
- </div>
- <ul class="links-of-blogroll-list">
- <li class="links-of-blogroll-item">
- <a href="https://yonghong.github.io/" title="https://yonghong.github.io" rel="noopener" target="_blank">Yonghong</a>
- </li>
- <li class="links-of-blogroll-item">
- <a href="https://source.unsplash.com/random/1600x900" title="https://source.unsplash.com/random/1600x900" rel="noopener" target="_blank">Background</a>
- </li>
- </ul>
- </div>
- </div>
-
- <div id="treefrog" style="text-align: center;margin-top: 18px;">
- <object type="application/x-shockwave-flash" style="outline:none;" data="/flash/treefrog.swf?up_bodyColor=444444&up_pattern=0&up_flyColor=777777&up_tongueColor=555555&up_patternColor=000000&up_releaseFly=0&up_frogName=Froggie&up_backgroundImage=http://&up_bellySize=.5&up_footColor=444444&up_eyeColor=444444&up_backgroundColor=222222&" width="300" height="600"><param name="movie" value="http://cdn.abowman.com/widgets/treefrog/treefrog.swf?up_bodyColor=444444&up_pattern=0&up_flyColor=777777&up_tongueColor=555555&up_patternColor=000000&up_releaseFly=0&up_frogName=Froggie&up_backgroundImage=http://&up_bellySize=.5&up_footColor=444444&up_eyeColor=444444&up_backgroundColor=222222&"></param><param name="AllowScriptAccess" value="always"></param><param name="wmode" value="opaque"></param><param name="scale" value="noscale"/><param name="salign" value="tl"/></object>
- </div>
-
- </div>
- </aside>
- <div id="sidebar-dimmer"></div>
- </div>
- </main>
- <footer class="footer">
- <div class="footer-inner">
-
-
- <div class="copyright">
-
- ©
- <span itemprop="copyrightYear">2020</span>
- <span class="with-love">
- <i class="fa fa-user"></i>
- </span>
- <span class="author" itemprop="copyrightHolder">Alex Xiang</span>
- </div>
-
- </div>
- </footer>
- </div>
-
- <script src="/lib/anime.min.js"></script>
- <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
- <script src="/lib/velocity/velocity.min.js"></script>
- <script src="/lib/velocity/velocity.ui.min.js"></script>
- <script src="/js/utils.js"></script>
- <script src="/js/motion.js"></script>
- <script src="/js/schemes/muse.js"></script>
- <script src="/js/next-boot.js"></script>
-
-
-
-
- <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/katex@0/dist/katex.min.css">
-
- <script>
- NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
- NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
- var GUEST = ['nick', 'mail', 'link'];
- var guest = 'nick,mail,link';
- guest = guest.split(',').filter(item => {
- return GUEST.includes(item);
- });
- new Valine({
- el : '#valine-comments',
- verify : true,
- notify : true,
- appId : 'BmologYYnRqCv0SLHDeDdA17-gzGzoHsz',
- appKey : 'w9mVebFMdCmY6Nh9vfcBGaGt',
- placeholder: "Say something.",
- avatar : 'mm',
- meta : guest,
- pageSize : '10' || 10,
- visitor : true,
- lang : 'en' || 'zh-cn',
- path : location.pathname,
- recordIP : true,
- serverURLs : ''
- });
- }, window.Valine);
- });
- </script>
- </body>
- </html>
|