<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>NotionNext BLOG</title>
        <link>https://tangly1024.com/</link>
        <description>这是一个由NotionNext生成的站点</description>
        <lastBuildDate>Thu, 12 Sep 2024 11:06:42 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>zh-CN</language>
        <copyright>All rights reserved 2024, NotionNext</copyright>
        <item>
            <title><![CDATA[板子]]></title>
            <link>https://tangly1024.com/article/板子</link>
            <guid>https://tangly1024.com/article/板子</guid>
            <pubDate>Sun, 01 Sep 2030 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-aa3c8382ad8c44dc95108394e6734a33"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-ef627f7c05094eb9b736446ab68b8e0b" data-id="ef627f7c05094eb9b736446ab68b8e0b"><span><div id="ef627f7c05094eb9b736446ab68b8e0b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#ef627f7c05094eb9b736446ab68b8e0b" title="快速幂"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">快速幂</span></span></h2><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-bbdfcec8218e460587fca3aee676f1b8" data-id="bbdfcec8218e460587fca3aee676f1b8"><span><div id="bbdfcec8218e460587fca3aee676f1b8" class="notion-header-anchor"></div><a class="notion-hash-link" href="#bbdfcec8218e460587fca3aee676f1b8" title="矩阵快速幂"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">矩阵快速幂</span></span></h2><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-f62d6393cf7f4325b5f322f940c0ae7c" data-id="f62d6393cf7f4325b5f322f940c0ae7c"><span><div id="f62d6393cf7f4325b5f322f940c0ae7c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#f62d6393cf7f4325b5f322f940c0ae7c" title="欧几里得求最大公约数"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">欧几里得求最大公约数</span></span></h2><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-093d608ea6754385bd9d5dd5c0c99cd8" data-id="093d608ea6754385bd9d5dd5c0c99cd8"><span><div id="093d608ea6754385bd9d5dd5c0c99cd8" class="notion-header-anchor"></div><a class="notion-hash-link" href="#093d608ea6754385bd9d5dd5c0c99cd8" title="拓展欧几里得"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">拓展欧几里得</span></span></h2><div class="notion-text notion-block-6fae150a7db441159482e5b2a7bd2ffc">扩展欧几里得算法可以在求得 <!-- -->，<!-- --> 的最大公约数的同时，找到整数 <!-- -->，<!-- -->（其中一个可能是负数），使它们满足 <!-- --> 。如果 <!-- --> 是负数，可以把问题转化成 <!-- -->，然后令 <!-- -->。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-c2c63f9db862498fb463cdb029d99da9" data-id="c2c63f9db862498fb463cdb029d99da9"><span><div id="c2c63f9db862498fb463cdb029d99da9" class="notion-header-anchor"></div><a class="notion-hash-link" href="#c2c63f9db862498fb463cdb029d99da9" title="拓扑排序"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">拓扑排序</span></span></h2><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-e222b07e484545d9811bf86e58736636" data-id="e222b07e484545d9811bf86e58736636"><span><div id="e222b07e484545d9811bf86e58736636" class="notion-header-anchor"></div><a class="notion-hash-link" href="#e222b07e484545d9811bf86e58736636" title="树状数组"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">树状数组</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34e7543fb77f48a1b6d8dbe9e01bcbbf" data-id="34e7543fb77f48a1b6d8dbe9e01bcbbf"><span><div id="34e7543fb77f48a1b6d8dbe9e01bcbbf" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34e7543fb77f48a1b6d8dbe9e01bcbbf" title="单点修改与区间查询"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">单点修改与区间查询</span></span></h3><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-7ddee20e393d4747a7e2145c5809dad2" data-id="7ddee20e393d4747a7e2145c5809dad2"><span><div id="7ddee20e393d4747a7e2145c5809dad2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#7ddee20e393d4747a7e2145c5809dad2" title="树状数组区间修改与单点查询"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">树状数组区间修改与单点查询</span></span></h3><div class="notion-text notion-block-1dd876c35f63435eb0ced752a37f5acd">我们用树状数组保存差分即可实现区间修改。</div><div class="notion-text notion-block-c5488dd587e743a49c7d3108902d2eda">另一个写法是树状数组存相邻个两数之间的差值，在此不做演示。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-9ae2b5e7f37843288c6979940b041b4d" data-id="9ae2b5e7f37843288c6979940b041b4d"><span><div id="9ae2b5e7f37843288c6979940b041b4d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#9ae2b5e7f37843288c6979940b041b4d" title="二维树状数组"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">二维树状数组</span></span></h3><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-6408dffa0440494c9ff6a7547a3b0982" data-id="6408dffa0440494c9ff6a7547a3b0982"><span><div id="6408dffa0440494c9ff6a7547a3b0982" class="notion-header-anchor"></div><a class="notion-hash-link" href="#6408dffa0440494c9ff6a7547a3b0982" title="二维树状数组差分"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">二维树状数组差分</span></span></h3><div class="notion-text notion-block-b3f49530ac3648508bf92e33bd069874">由二维差分的知识可知，我们需要维护四个数组。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-744747d0c70b48c8945af5477b92ebce" data-id="744747d0c70b48c8945af5477b92ebce"><span><div id="744747d0c70b48c8945af5477b92ebce" class="notion-header-anchor"></div><a class="notion-hash-link" href="#744747d0c70b48c8945af5477b92ebce" title="逆序对"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">逆序对</span></span></h2><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-748c6076ce26447aa575ed550626896a" data-id="748c6076ce26447aa575ed550626896a"><span><div id="748c6076ce26447aa575ed550626896a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#748c6076ce26447aa575ed550626896a" title="线段树"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">线段树</span></span></h2><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-4fd8d0b0e2d246f29aef84052a9cf081" data-id="4fd8d0b0e2d246f29aef84052a9cf081"><span><div id="4fd8d0b0e2d246f29aef84052a9cf081" class="notion-header-anchor"></div><a class="notion-hash-link" href="#4fd8d0b0e2d246f29aef84052a9cf081" title="tire树"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">tire树</span></span></h2></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[线段树]]></title>
            <link>https://tangly1024.com/article/线段树</link>
            <guid>https://tangly1024.com/article/线段树</guid>
            <pubDate>Fri, 26 Jul 2024 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-38d17bcf9da04602b57448ba1eb6fd01"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-4817d82d93af4b78a66192880ed02b1a" data-id="4817d82d93af4b78a66192880ed02b1a"><span><div id="4817d82d93af4b78a66192880ed02b1a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#4817d82d93af4b78a66192880ed02b1a" title="简介"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">简介</span></span></h2><div class="notion-text notion-block-2e56017c92224c47bb10155afcc494de">线段树是算法竞赛中常用的用来维护 <b>区间信息</b> 的数据结构。</div><div class="notion-text notion-block-2d12a15cc7fc43de8ce8e25cca3e9f76">线段树可以在 <!-- --> 的时间复杂度内实现单点修改、区间修改、区间查询（区间求和，求区间最大值，求区间最小值）等操作。</div><div class="notion-text notion-block-32c752b3cd9a4553851c4dadedf3b399">线段树将每个长度不为1的区间划分成左右两个区间递归求解，把整个线段划分为一个树形结构，通过合并左右两区间信息来求得该区间的信息。这种数据结构可以方便的进行大部分的区间操作。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-e99d47b8256245d6a30fccaedce9b43a" data-id="e99d47b8256245d6a30fccaedce9b43a"><span><div id="e99d47b8256245d6a30fccaedce9b43a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#e99d47b8256245d6a30fccaedce9b43a" title="过程"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">过程</span></span></h3><div class="notion-text notion-block-43fd80b745534dd4adc5933673655084">有个大小为 <!-- --> 的数组 <!-- --> ，要将其转化为线段树，有以下做法：设线段树的根节点编号为 <!-- --> ，用数组 <!-- --> 来保存我们的线段树，<!-- --> 用来保存线段树上编号为 <!-- --> 的节点的值，这里每个节点所维护的值就是这个节点所表示的区间总和。如图所示:</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-7b3ae3e7a42449c5a7fd45b2691f293d"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2Fb7cedaa4-65bd-4568-b05f-71b21f47d181%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-07-26_113045.png?table=block&amp;id=7b3ae3e7-a424-49c5-a7fd-45b2691f293d&amp;t=7b3ae3e7-a424-49c5-a7fd-45b2691f293d" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-371c5d002f6e4606be68e0ae1c5d6ddc">实现代码：</div><div class="notion-text notion-block-e2a64d7c5c844091ab5a21188a5ec2ff">关于线段树的空间:如果采用堆式存储（ <!-- --> 是 <!-- --> 的左儿子，<!-- --> 是 <!-- --> 的右儿子），若有几个叶
子结点，则 <!-- --> 数组的范围最大为 <!-- --> 。</div><div class="notion-text notion-block-c650933c126b458281a5611addce9d14"><b>分析</b>：容易知道线段树的深度是 <!-- --> 的，则在堆式储存情况下叶子节点（包括无用的叶子节点）数量为 <!-- --> 个，又由于其为一棵完全二叉树，则其总节点个数 <!-- --> 。当然如果你懒得计算的话可以直接把数组长度设为 <!-- -->，因为 <!-- --> 的最大值在 <!-- --> 时取到，此时节点数为 <!-- --> 。</div><div class="notion-text notion-block-cf403980ca76468ead7712039e7ef2f2">而堆式存储存在无用的叶子节点，可以考虑使用内存池管理线段树节点，每当需要新建节点时从池中获取。自底向上考虑，必有每两个底层节点合并为一个上层节点，因此可以类似哈夫曼树地证明，如果有 <!-- --> 个叶子节点，这样的线段树总共有 <!-- --> 个节点。其空间效率优于堆式存储，并且是可能的最优情况。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3edc85ea36e442e6b390710d8cc9deb2" data-id="3edc85ea36e442e6b390710d8cc9deb2"><span><div id="3edc85ea36e442e6b390710d8cc9deb2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3edc85ea36e442e6b390710d8cc9deb2" title="区间修改懒惰标记"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">区间修改懒惰标记</span></span></h3><div class="notion-text notion-block-67cfc89f06dd4263956fec0c0262a247">如果要求修改区间 <!-- --> ，把所有包含在区间 <!-- --> 中的节点都遍历一次、修改一次，时间复杂度无
法承受。我们这里要引入一个叫做<b>懒惰标记</b>的东西。</div><div class="notion-text notion-block-ba531a22bb9f4453b139a47908b54b13"><b>懒惰标记</b>，简单来说，就是通过延迟对节点信息的更改，从而减少可能不必要的操作次数。每次执行修改时，我们通过打标记的方法表明该节点对应的区间在某一次操作中被更改，但不更新该节点的子节点的信息。实质性的修改则在下一次访问带有标记的节点时才进行。仍然以最开始的图为例，我们将执行若干次给区间内的数加上一个值的操作。我们现在给每个节点增加一个t，表示该节点带的标记值。
最开始时的情况是这样的：</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-7e99d7f40f8443798a6e19e1699ba27c"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F83c2df55-d2b6-48d9-97d0-4cdbd5a18385%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-07-26_113139.png?table=block&amp;id=7e99d7f4-0f84-4379-8a6e-19e1699ba27c&amp;t=7e99d7f4-0f84-4379-8a6e-19e1699ba27c" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-b56a144361024dc9b0a0f276bace0d14">现在我们准备给[3,5]上的每个数都加上 5。根据前面区间查询的经验，我们很快找到了两个极大区间 <!-- --> 和 <!-- --> 。我们直接在这两个节点上进行修改，并给它们打上标记。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-67f4ca0969a44d43af027a8f42644956"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F12f44086-6b29-4f44-a50e-875d296a8258%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-07-26_113145.png?table=block&amp;id=67f4ca09-69a4-4d43-af02-7a8f42644956&amp;t=67f4ca09-69a4-4d43-af02-7a8f42644956" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-1e25d46f39f54b7c970c4b1899f3a38c">我们发现，3号节点的信息虽然被修改了（因为该区间管辖两个数，所以 <!-- --> 加上的数是 <!-- -->），但它的两个子节点却还没更新，仍然保留着修改之前的信息。虽然修改目前还没进行，但当我们要查询这两个子节点的信息时，我们会利用标记修改这两个子节点的信息，使查询的结果依旧准确。</div><div class="notion-text notion-block-b4f905fcad0a44ecbd41a376c2cc8544">接下来我们查询一下 <!-- --> 区间上各数字的和。</div><div class="notion-text notion-block-af9faeb368684e4d8bdcea873a7cacd8">我们通过递归找到 <!-- --> 区间，发现该区间并非我们的目标区间，且该区间上还存在标记。这时候
就到标记下放的时间了。我们将该区间的两个子区间的信息更新，并清除该区间上的标记。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-03402158c3f04744b89749ad9c0ac273"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2Fa5038a2e-c76a-43bf-8db8-365739175dd8%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-07-26_113416.png?table=block&amp;id=03402158-c3f0-4744-b897-49ad9c0ac273&amp;t=03402158-c3f0-4744-b897-49ad9c0ac273" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-d9108b7401ac4e2b88d11970b731201d">现在 6、7两个节点的值变成了最新的值，查询的结果也是准确的。</div><div class="notion-text notion-block-ec1f8b140ccb4e65a112c223a0b0bb50">代码实现：</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-17ed28287eb442a9af84b6ed093e7128" data-id="17ed28287eb442a9af84b6ed093e7128"><span><div id="17ed28287eb442a9af84b6ed093e7128" class="notion-header-anchor"></div><a class="notion-hash-link" href="#17ed28287eb442a9af84b6ed093e7128" title="区间查询"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">区间查询</span></span></h3><div class="notion-text notion-block-6064935ebe9d47bfaba77777a5ce0919">一般地，如果要查询的区间是 <!-- -->，则可以将其拆成最多为 <!-- --> 个 极大 的区间，合并这些区
间即可求出 <!-- --> 的答案。</div><div class="notion-text notion-block-b0173909fe9e40729697074d52d0cc1c">实现代码：</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-da761f9f59db4d3db806901400fe58e6" data-id="da761f9f59db4d3db806901400fe58e6"><span><div id="da761f9f59db4d3db806901400fe58e6" class="notion-header-anchor"></div><a class="notion-hash-link" href="#da761f9f59db4d3db806901400fe58e6" title="动态开点线段树 "><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">动态开点线段树 </span></span></h3><div class="notion-text notion-block-7c915b358d5e4553970f5d40d8acfef3">前面讲到堆式储存的情况下，需要给线段树开 <!-- --> 大小的数组。为了节省空间，我们可以不一次性建好树，而是在最初只建立一个根结点代表整个区间。当我们需要访问某个子区间时，才建立代表这个区间的子结点。这样我们不再使用 <!-- --> 和 <!-- --> 代表 <!-- --> 结点的儿子，而是用 <!-- --> 和 <!-- --> 记录儿子的编号。总之，动态开点线段树的核心思想就是：<b>结点只有在有需要的时候才被创建。</b></div><div class="notion-text notion-block-fea50031beb64df9b355ce1cad906b1d">单次操作的时间复杂度是不变的，为 <!-- -->。由于每次操作都有可能创建并访问全新的一系列结点，因此 m 次单点操作后结点的数量规模是 <!-- --> 。最多也只需要 <!-- --> 个结点，没有浪费。</div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-a96cc9f28812445dbece08887bb3e79f" data-id="a96cc9f28812445dbece08887bb3e79f"><span><div id="a96cc9f28812445dbece08887bb3e79f" class="notion-header-anchor"></div><a class="notion-hash-link" href="#a96cc9f28812445dbece08887bb3e79f" title="单点修改"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">单点修改</span></span></h4><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-eb2a6aa4831d4c1eb9b765610849a1be" data-id="eb2a6aa4831d4c1eb9b765610849a1be"><span><div id="eb2a6aa4831d4c1eb9b765610849a1be" class="notion-header-anchor"></div><a class="notion-hash-link" href="#eb2a6aa4831d4c1eb9b765610849a1be" title="区间查询"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">区间查询</span></span></h4><div class="notion-text notion-block-c8da3f29ebab4cde9a2bcc7b0cda34f4">区间修改也是一样的，不过下放标记时要注意如果缺少孩子，就直接创建一个新的孩子。或者使用标记永久化技巧。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-626203f50be54f4e84ad10db7984b23b" data-id="626203f50be54f4e84ad10db7984b23b"><span><div id="626203f50be54f4e84ad10db7984b23b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#626203f50be54f4e84ad10db7984b23b" title="一些优化"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">一些优化</span></span></h3><div class="notion-text notion-block-52e08d9cf27b4fe089e388517e59a1d6">这里总结几个线段树的优化：</div><ul class="notion-list notion-list-disc notion-block-46aa11de95fe4f91bd1c1a4ca489f01a"><li>在叶子节点处无需下放懒惰标记，所以懒惰标记可以不下传到叶子节点。</li></ul><ul class="notion-list notion-list-disc notion-block-d541dc7c36e2467da664c01fb90c3e60"><li>下放懒惰标记可以写一个专门的函数 <code class="notion-inline-code">pushdown</code>，从儿子节点更新当前节点也可以写一个专门的函数 <code class="notion-inline-code">maintain</code>（或者对称地用 <code class="notion-inline-code">pushup</code>），降低代码编写难度。</li></ul><ul class="notion-list notion-list-disc notion-block-6c56e5492be240bbafef48051e65e11b"><li>标记永久化：如果确定懒惰标记不会在中途被加到溢出（即超过了该类型数据所能表示的最大范围），那么就可以将标记永久化。标记永久化可以避免下传懒惰标记，只需在进行询问时把标记的影响加到答案当中，从而降低程序常数。具体如何处理与题目特性相关，需结合题目来写。这也是树套树和可持久化数据结构中会用到的一种技巧。</li></ul><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-23e9ed67a0a549f2b73ce84fb4df112e" data-id="23e9ed67a0a549f2b73ce84fb4df112e"><span><div id="23e9ed67a0a549f2b73ce84fb4df112e" class="notion-header-anchor"></div><a class="notion-hash-link" href="#23e9ed67a0a549f2b73ce84fb4df112e" title="模板例题"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">模板例题</span></span></h2><div class="notion-row"><a target="_blank" rel="noopener noreferrer" class="notion-bookmark notion-block-b1f6cc50d74442398aafe7e1e2086b18" href="https://www.luogu.com.cn/problem/P3372"><div><div class="notion-bookmark-title">【模板】线段树 1 - 洛谷</div><div class="notion-bookmark-link"><div class="notion-bookmark-link-icon"><img src="https://www.luogu.com.cn/favicon.ico?t=b1f6cc50-d744-4239-8aaf-e7e1e2086b18" alt="【模板】线段树 1 - 洛谷" loading="lazy" decoding="async"/></div><div class="notion-bookmark-link-text">https://www.luogu.com.cn/problem/P3372</div></div></div></a></div><div class="notion-row"><a target="_blank" rel="noopener noreferrer" class="notion-bookmark notion-block-cce67a33cb8e47c8bd3a2210b7646edd" href="https://www.luogu.com.cn/problem/P3373"><div><div class="notion-bookmark-title">【模板】线段树 2 - 洛谷</div><div class="notion-bookmark-link"><div class="notion-bookmark-link-icon"><img src="https://www.luogu.com.cn/favicon.ico?t=cce67a33-cb8e-47c8-bd3a-2210b7646edd" alt="【模板】线段树 2 - 洛谷" loading="lazy" decoding="async"/></div><div class="notion-bookmark-link-text">https://www.luogu.com.cn/problem/P3373</div></div></div></a></div></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[并查集]]></title>
            <link>https://tangly1024.com/article/并查集</link>
            <guid>https://tangly1024.com/article/并查集</guid>
            <pubDate>Mon, 11 Mar 2024 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-6587b322230c4f0c9e9cef472fb7a3d8"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-4707ad655b9546a797724d783170eef2" data-id="4707ad655b9546a797724d783170eef2"><span><div id="4707ad655b9546a797724d783170eef2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#4707ad655b9546a797724d783170eef2" title="什么是并查集"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">什么是并查集</span></span></h2><div class="notion-text notion-block-a280d00d98e144588e87d0b43b2c34c9">并查集是一种树型的数据结构，用于处理一些不相交集合的合并及查询问题。</div><div class="notion-text notion-block-4a1713004b8d44e28eb9d5025e16ca7c">并查集的思想是用一个数组表示了整片森林（parent），树的根节点唯一标识了一个集合，我们只要找到了某个元素的的树根，就能确定它在哪个集合里。</div><div class="notion-text notion-block-c190796a5fcb4e00a71c49e60f6eb7ce">并查集支持两种操作：</div><ul class="notion-list notion-list-disc notion-block-609a88572e2d4e1d9419325a4f4db68d"><li><b>合并</b>（Union）：把两个不相交的集合合并为一个集合。</li></ul><ul class="notion-list notion-list-disc notion-block-6918a49d23694136b86abf2486a930fb"><li><b>查询</b>（Find）：查询两个元素是否在同一个集合中。</li></ul><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-88bfa4149c57493380710c6ad80c3719" data-id="88bfa4149c57493380710c6ad80c3719"><span><div id="88bfa4149c57493380710c6ad80c3719" class="notion-header-anchor"></div><a class="notion-hash-link" href="#88bfa4149c57493380710c6ad80c3719" title="适用条件"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">适用条件</span></span></h2><div class="notion-text notion-block-7b15eeada6fd4b5c9c792b72edf4dd3d">并查集用在一些有<!-- -->个元素的集合应用问题中，我们通常是在开始时让每个元素构成一个单元素的集合，然后按一定顺序将属于同一组的元素所在的集合合并，其间要反复查找一个元素在哪个集合中。这个过程看似并不复杂，但数据量极大，若用其他的数据结构来描述的话，往往在空间上过大，计算机无法承受，也无法在短时间内计算出结果，所以只能用并查集来处理。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-d9bec33d8b7b4bb0a04ab7de6b1dcd7c" data-id="d9bec33d8b7b4bb0a04ab7de6b1dcd7c"><span><div id="d9bec33d8b7b4bb0a04ab7de6b1dcd7c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#d9bec33d8b7b4bb0a04ab7de6b1dcd7c" title="初始化"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">初始化</span></span></h2><div class="notion-text notion-block-3b18b6a13d464bb9bae6f5160a3cc4d0">每一个祖先都是自己。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-60db424ec63247beb414531627e12b1a" data-id="60db424ec63247beb414531627e12b1a"><span><div id="60db424ec63247beb414531627e12b1a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#60db424ec63247beb414531627e12b1a" title="查询操作"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">查询操作</span></span></h2><div class="notion-text notion-block-6f993ebbedee4ca98d1e7e574f1dfebf">我们用递归的写法实现对代表元素的查询：一层一层的访问父节点，直至根节点（根节点的标志就是父节点是本身）。要判断两个元素是否属于同一个集合，只需要看它们的根节点是否相同即可。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-d84c5d47cbe34c079b42ebd36e2cd900" data-id="d84c5d47cbe34c079b42ebd36e2cd900"><span><div id="d84c5d47cbe34c079b42ebd36e2cd900" class="notion-header-anchor"></div><a class="notion-hash-link" href="#d84c5d47cbe34c079b42ebd36e2cd900" title="合并操作"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">合并操作</span></span></h2><div class="notion-text notion-block-95c6a45231994ac9819519da657cd675">先找到两个集合的根节点，然后将前者的父节点设为后者即可。当然也可以将后者的父节点设为前者。</div><div class="notion-text notion-block-85bb5634ad184f55bc9b4d81d0ba410d">下面我们举个例子来具体说明这些操作:</div><div class="notion-text notion-block-599282fb9d3540d19535d64fc7c0dc64">如图一，开始时我们有一些散乱的无连接的点集，我们将其初始化为7个集合，即每个点的根节点均为自己本身。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-5d3e7780bdf2418cb5b70e41c37096c4"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2Ffbf55efc-f627-41e5-aeb7-e6cc6e98db54%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-11_202359.png?table=block&amp;id=5d3e7780-bdf2-418c-b5b7-0e41c37096c4&amp;t=5d3e7780-bdf2-418c-b5b7-0e41c37096c4" alt="                                    图一" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">                                    图一</figcaption></div></figure><div class="notion-text notion-block-c005b8e69c4c4c6db46c23df829917e7">接着我查询1，2号是否在同一个集合下，发现不是。接着我们想要将1，2号合并，于是将2号的父节点设为1号；查询4，6号是否在同一个集合下，发现不是，将4，6号合并，将6号的父节点设为4号，如图二。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-0310a49ca42149e396643be20647f644"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F78f6973f-ee4a-4eeb-8a81-ced1de399080%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-11_202632.png?table=block&amp;id=0310a49c-a421-49e3-9664-3be20647f644&amp;t=0310a49c-a421-49e3-9664-3be20647f644" alt="                                    图二" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">                                    图二</figcaption></div></figure><div class="notion-text notion-block-4d11e0ebd7da436eb652169b804ae963">接着我分别查询3，5号以及3，7号是否在同一个集合下，发现不是，于是将3，5号、3，7号合并，将5号，7号的父节点设为3号，如图三。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-c48d7fc330b54e87a8e5f675220918ff"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2Ff1a60cd9-c007-4c62-992c-8dc443dd2643%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-11_202748.png?table=block&amp;id=c48d7fc3-30b5-4e87-a8e5-f675220918ff&amp;t=c48d7fc3-30b5-4e87-a8e5-f675220918ff" alt="                                    图三" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">                                    图三</figcaption></div></figure><div class="notion-text notion-block-339bc5cb9b6a443f89957d71f0bbf599">同理，将3号的父节点设为1号，如图四。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-9bb649f9dc3d4b50b3d3f48839044c27"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2Fdc3a7dc9-7ab3-471e-b7cc-cb476f56293e%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-11_202822.png?table=block&amp;id=9bb649f9-dc3d-4b50-b3d3-f48839044c27&amp;t=9bb649f9-dc3d-4b50-b3d3-f48839044c27" alt="                                    图四" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">                                    图四</figcaption></div></figure><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-a49684761ed14a059ddc036160c050e9" data-id="a49684761ed14a059ddc036160c050e9"><span><div id="a49684761ed14a059ddc036160c050e9" class="notion-header-anchor"></div><a class="notion-hash-link" href="#a49684761ed14a059ddc036160c050e9" title="路径压缩"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">路径压缩</span></span></h2><div class="notion-text notion-block-4468a8adeea24aceb983402c31ab27bd">随着元素的不断加入，集合会变得越来越大，链子也会越来越长，这会对我们的查询操作产生影响，查询的效率降低。我们可以在每次查询的时候将每个经过的结点的父节点设为根节点，下次在查询时就可以节省很多时间。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-9f131d946e8c467fb2b2add2aaf5a440"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F31ce9964-7ab7-4da7-852e-d8448f0c8e38%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-12_135747.png?table=block&amp;id=9f131d94-6e8c-467f-b2b2-add2aaf5a440&amp;t=9f131d94-6e8c-467f-b2b2-add2aaf5a440" alt="                                     图五" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">                                     图五</figcaption></div></figure><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-8623634c4116430bad61692851ae7208" data-id="8623634c4116430bad61692851ae7208"><span><div id="8623634c4116430bad61692851ae7208" class="notion-header-anchor"></div><a class="notion-hash-link" href="#8623634c4116430bad61692851ae7208" title="按秩合并"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">按秩合并</span></span></h2><div class="notion-text notion-block-952062684ea845e5b74e82be86c2d13e">针对合并函数merge()，往往有两种合并方式，即把<!-- -->节点的父节点设为<!-- -->和把<!-- -->节点的父节点设为<!-- -->，很显然，两种合并方式的结果并不同，合并后整体的查询效率也不同。如图六，绿色和黄色对应两棵树，图中的第一种合并方式下只有8号的查询需要多一步，而第二种方式下除了8号的所有元素查询都要增加一步。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-d47f7243d59c4cffa865e77f696aed66"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F9a36c38b-db3c-4172-94f2-c555e0fda056%2FUntitled.png?table=block&amp;id=d47f7243-d59c-4cff-a865-e77f696aed66&amp;t=d47f7243-d59c-4cff-a865-e77f696aed66" alt="                                     图六" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">                                     图六</figcaption></div></figure><div class="notion-text notion-block-fcdfeae7a67642b8a4e32695fa6b3a5d">因此，我们需要定义一个判断条件：秩。秩可以是很多因素，比如树高，节点个数，等等。不管选择什么作为秩，合并时都最好要遵循启发式合并的原则，即将小的集合合并到大的集合中。下面以秩为树高来举例。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-0f4b315b99e243fdb798f6ee22b9687e" data-id="0f4b315b99e243fdb798f6ee22b9687e"><span><div id="0f4b315b99e243fdb798f6ee22b9687e" class="notion-header-anchor"></div><a class="notion-hash-link" href="#0f4b315b99e243fdb798f6ee22b9687e" title="初始化"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">初始化</span></span></h3><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-f7a6b3da54424926b448e20ee1859b62" data-id="f7a6b3da54424926b448e20ee1859b62"><span><div id="f7a6b3da54424926b448e20ee1859b62" class="notion-header-anchor"></div><a class="notion-hash-link" href="#f7a6b3da54424926b448e20ee1859b62" title="按秩合并"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">按秩合并</span></span></h3></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[trie树]]></title>
            <link>https://tangly1024.com/article/trie树</link>
            <guid>https://tangly1024.com/article/trie树</guid>
            <pubDate>Tue, 06 Aug 2024 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-290261b58f9644f689f5cd14a0e357d5"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-e2527977ce4d44138276701556b4896b" data-id="e2527977ce4d44138276701556b4896b"><span><div id="e2527977ce4d44138276701556b4896b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#e2527977ce4d44138276701556b4896b" title="定义"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">定义</span></span></h2><div class="notion-text notion-block-cd51ec23f8d0423bab9c8ed44fa5ddd2">字典树，英文名 trie。顾名思义，就是一个像字典一样的树。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-1d0ee64d8c3f4d1cad6e0e701d9e6289"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2Fac7fde3f-0548-415e-a8c8-56bc6f1e040e%2Ftrie1.png?table=block&amp;id=1d0ee64d-8c3f-4d1c-ad6e-0e701d9e6289&amp;t=1d0ee64d-8c3f-4d1c-ad6e-0e701d9e6289" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-f3651ba1a9fd4ae595031b52bcecf5ad">可以发现，这棵字典树用边来代表字母，而从根结点到树上某一结点的路径就代表了一个字符串。</div><div class="notion-text notion-block-238edb7eace3469fac40261deca0fb0e">举个例子，<!-- --> 表示的就是字符串 <code class="notion-inline-code">caa</code>。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-6beeada2cb614493a56a1c31122b9368" data-id="6beeada2cb614493a56a1c31122b9368"><span><div id="6beeada2cb614493a56a1c31122b9368" class="notion-header-anchor"></div><a class="notion-hash-link" href="#6beeada2cb614493a56a1c31122b9368" title="实现"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">实现</span></span></h2><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-0f5a1c5ed3fc46e89bf7a812703dc2ee" data-id="0f5a1c5ed3fc46e89bf7a812703dc2ee"><span><div id="0f5a1c5ed3fc46e89bf7a812703dc2ee" class="notion-header-anchor"></div><a class="notion-hash-link" href="#0f5a1c5ed3fc46e89bf7a812703dc2ee" title="应用"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">应用</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-03b606bb5cb14c75a1450ba33aafef14" data-id="03b606bb5cb14c75a1450ba33aafef14"><span><div id="03b606bb5cb14c75a1450ba33aafef14" class="notion-header-anchor"></div><a class="notion-hash-link" href="#03b606bb5cb14c75a1450ba33aafef14" title="检索字符串"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">检索字符串</span></span></h3><div class="notion-text notion-block-8d6474f7f39f42eda7b54f6211a997f4">字典树最基础的应用——查找一个字符串是否在 “字典” 中出现过。</div><div class="notion-row"><a target="_blank" rel="noopener noreferrer" class="notion-bookmark notion-block-7caa908200d34d079ed0ff1297ceadd7" href="https://www.luogu.com.cn/problem/P2580"><div><div class="notion-bookmark-title">于是他错误的点名开始了 - 洛谷</div><div class="notion-bookmark-link"><div class="notion-bookmark-link-icon"><img src="https://www.luogu.com.cn/favicon.ico?t=7caa9082-00d3-4d07-9ed0-ff1297ceadd7" alt="于是他错误的点名开始了 - 洛谷" loading="lazy" decoding="async"/></div><div class="notion-bookmark-link-text">https://www.luogu.com.cn/problem/P2580</div></div></div></a></div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-546c2415dfff4be4a7eb321f83a6ab3a" data-id="546c2415dfff4be4a7eb321f83a6ab3a"><span><div id="546c2415dfff4be4a7eb321f83a6ab3a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#546c2415dfff4be4a7eb321f83a6ab3a" title="维护异或极值"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>维护异或极值</b></span></span></h3><div class="notion-text notion-block-ad8f1b0735064100bee61f145133feab">01-trie 是指字符集为 <!-- --> 的 trie。01-trie 可以用来维护一些数字的异或和，支持修改(删除，重新插入)，和全局加一 (即让其所维护所有数值递增1，本质上是一种特殊的修改操作)。</div><div class="notion-text notion-block-3275bd219df64714aa7fe59368d2a95f">如果要维护异或和，需要按值从低位到高位建立 trie。</div><div class="notion-row"><a target="_blank" rel="noopener noreferrer" class="notion-bookmark notion-block-aec119e13cfc48519e2e520d694bfe0f" href="https://www.luogu.com.cn/problem/P10471"><div><div class="notion-bookmark-title">最大异或对 The XOR Largest Pair - 洛谷</div><div class="notion-bookmark-link"><div class="notion-bookmark-link-icon"><img src="https://www.luogu.com.cn/favicon.ico?t=aec119e1-3cfc-4851-9e2e-520d694bfe0f" alt="最大异或对 The XOR Largest Pair - 洛谷" loading="lazy" decoding="async"/></div><div class="notion-bookmark-link-text">https://www.luogu.com.cn/problem/P10471</div></div></div></a></div></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[01 trie树]]></title>
            <link>https://tangly1024.com/article/trie树</link>
            <guid>https://tangly1024.com/article/trie树</guid>
            <pubDate>Thu, 25 Apr 2024 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-fd4b0318779146c1b4f3634e5d07b6d4"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-0ed7b072ae3d47a9ae18b8166b3ecaa7" data-id="0ed7b072ae3d47a9ae18b8166b3ecaa7"><span><div id="0ed7b072ae3d47a9ae18b8166b3ecaa7" class="notion-header-anchor"></div><a class="notion-hash-link" href="#0ed7b072ae3d47a9ae18b8166b3ecaa7" title="01 trie树"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">01 trie树</span></span></h2><div class="notion-text notion-block-e8a1d2f76d09411d850183c1cd1ec4fd">类似于字典树的做法，将每一个数化为二进制数，看作01串，插入到字典树中。如图1：<div class="notion-text-children"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-e2aaeafc2121453590b02ef5cd632541"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F523e90a2-42eb-4904-ab91-da3211513675%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-02-18_192126.png?table=block&amp;id=e2aaeafc-2121-4535-90b0-2ef5cd632541&amp;t=e2aaeafc-2121-4535-90b0-2ef5cd632541" alt="                                 图1" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">                                 图1</figcaption></div></figure></div></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-cf59cd02aff0499c8a4b30ce99c6c2bf" data-id="cf59cd02aff0499c8a4b30ce99c6c2bf"><span><div id="cf59cd02aff0499c8a4b30ce99c6c2bf" class="notion-header-anchor"></div><a class="notion-hash-link" href="#cf59cd02aff0499c8a4b30ce99c6c2bf" title="典型例题"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">典型例题</span></span></h2><div class="notion-text notion-block-57b8256b59994a5985ec16a703bdc926">给一个长为<!-- -->的数列，要求一个 <!-- -->，<!-- --> ，使得 <!-- -->^<!-- --> 最大。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-657e01e3b3e84c7f94afa7d735bc0841" data-id="657e01e3b3e84c7f94afa7d735bc0841"><span><div id="657e01e3b3e84c7f94afa7d735bc0841" class="notion-header-anchor"></div><a class="notion-hash-link" href="#657e01e3b3e84c7f94afa7d735bc0841" title="异或的一些性质"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">异或的一些性质</span></span></h3><ul class="notion-list notion-list-disc notion-block-b920e34133b24e6f9079f55628b543fa"><li>0^1=1</li></ul><ul class="notion-list notion-list-disc notion-block-96ce9304fcff41739ea96040d5950cf3"><li>1^1=0</li></ul><ul class="notion-list notion-list-disc notion-block-3379a50f6a7340a5a35ae29cab827fb0"><li>0^0=0</li></ul><ul class="notion-list notion-list-disc notion-block-6e2ffdbb0eb249aa9294763912f5b9d6"><li>p^p=0</li></ul><ul class="notion-list notion-list-disc notion-block-515c4cff54954cb789fb032d724639ba"><li>p^0=1</li></ul><ul class="notion-list notion-list-disc notion-block-0f6af914c8d745729c2bab4a4d8b546b"><li>自反性：a^b=c，b^c=a</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-4e3f4649f0c6428fbd1c60e196e625c2" data-id="4e3f4649f0c6428fbd1c60e196e625c2"><span><div id="4e3f4649f0c6428fbd1c60e196e625c2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#4e3f4649f0c6428fbd1c60e196e625c2" title="问题解答"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">问题解答</span></span></h3><div class="notion-text notion-block-ee37a33cfc1344d9b6e63fddcdbc111c">首先建好01trie树，以图1为例，然后对于每一个数，在树上跑一遍贪心，即贪心选择与这个数当前位不同的那个节点。</div><div class="notion-text notion-block-cd3cb9d24e2f49448e456abe6ba548e3">也就是说，尽可能地保证越高的位的异或和为1，（因为高位的一个1比后面的位全为1都要大）</div><div class="notion-text notion-block-5db7571d4aec4634a8d79d5f2555618e">比如对于7，在下面这个插入了0，2，7的树上跑。
开始在0号节点，然后看最高位，7的最高位是1，所以为了保证当前位异或和为1，应走0的方向，也就是走到1节点。
然后看次高位，7的次高位为1，同理，走0的方向到3号节点，依次类推，得出与7异或的最大值为7。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-a76d8ba1766b45e6b26ac965c9b0ff83" data-id="a76d8ba1766b45e6b26ac965c9b0ff83"><span><div id="a76d8ba1766b45e6b26ac965c9b0ff83" class="notion-header-anchor"></div><a class="notion-hash-link" href="#a76d8ba1766b45e6b26ac965c9b0ff83" title="两道拓展例题"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">两道拓展例题</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-7d56f2fcf69f4d7eabaf976f7cd20cc0" data-id="7d56f2fcf69f4d7eabaf976f7cd20cc0"><span><div id="7d56f2fcf69f4d7eabaf976f7cd20cc0" class="notion-header-anchor"></div><a class="notion-hash-link" href="#7d56f2fcf69f4d7eabaf976f7cd20cc0" title="1.第k大异或和"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1.第k大异或和</span></span></h3><div class="notion-text notion-block-81e9399a1e1d4f6380d2a60950fc3132">题意：还是给出长为<!-- -->的序列，这次不求最大值了，求第<!-- -->大的<!-- -->^<!-- -->。</div><div class="notion-text notion-block-efff75987efd4b88aae8ee709592f13d">做法：考虑二分答案，对于每个二分到的值<!-- -->，遍历数组跑一遍01trie树，看有多少组异或和是大于<!-- -->的，然后根据大于<!-- -->的异或和的组数向上向下二分。</div><div class="notion-text notion-block-6bb09aae521a43b29a79f020b9e173db">如何计算大于<!-- -->的组数？</div><div class="notion-text notion-block-ce72ea3595184c43bb4acaf7e4ea1e9f">遍历数组，对于每一个数<!-- -->,跑一遍01trie，记录比<!-- -->大的异或和的组数<!-- -->。对于<!-- -->的每一位，若为1，则走树上与<!-- -->当前位相反的位置，若为0，则走树上与<!-- -->当前位相同的位置，并将与<!-- -->当前位相反的那个位置的子树大小加到<!-- -->中。</div><div class="notion-text notion-block-bdeda75453d14f2b8796055630df36a1">由于每一组大于<!-- -->的二元组都会被计算两次，所以最终的<!-- -->还要除以二。</div><div class="notion-text notion-block-79d4813e45d24211a18b7e2b29bfa043">复杂度<!-- -->。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-826c3ff820384a89bc106f0386f0b7a5" data-id="826c3ff820384a89bc106f0386f0b7a5"><span><div id="826c3ff820384a89bc106f0386f0b7a5" class="notion-header-anchor"></div><a class="notion-hash-link" href="#826c3ff820384a89bc106f0386f0b7a5" title="2.树上的最大异或路径和"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2.树上的最大异或路径和</span></span></h3><div class="notion-text notion-block-444b573d9c054b2bb67f4903e30c1ce6">题意：给定一棵<!-- -->个点的带权树，寻找树中找两个结点，求最长的异或路径。异或路径指的是指两个结点之间唯一路径上的所有边权的异或。</div><div class="notion-text notion-block-70154067ce684d1889af330a09126e2e">做法：观察得到，从<!-- -->~<!-- -->的路径的异或值为<!-- -->到根的异或和^<!-- -->到根的异或和。</div><div class="notion-text notion-block-6e103cc717754451aa92dea78b8dd603">因此将例题中的<!-- -->转化成节点<!-- -->到根的异或和的值，就可以照着例题的方法做了。</div><div class="notion-text notion-block-8d9e7945fd4a4c3e9fe585e3d1cdfc9c">复杂度<!-- -->。</div></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL使用教程]]></title>
            <link>https://tangly1024.com/article/SQL使用教程</link>
            <guid>https://tangly1024.com/article/SQL使用教程</guid>
            <pubDate>Wed, 20 Mar 2024 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-318ec4fc614e4e4ab002b5f3bc5418ef"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><div class="notion-text notion-block-d8087e46a9ee43cfb38264d6a2b29b9e"><b>SQL</b> (Structured Query Language:结构化查询语言) 是用于管理关系数据库管理系统（RDBMS）。 SQL 的范围包括数据插入、查询、更新和删除，数据库模式创建和修改，以及数据访问控制。</div><div class="notion-text notion-block-f9197ef3470049fdbf97c5ec5cd4cb59">注意：<b>MySQL 在Windows和MacOS系统下不区分大小写</b>，但在Linux 系统下默认区分大小写。但是为了方便使用，我们一般会将关键字全部大写。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-d66106a1ffd94e8ea227815a935d59ea" data-id="d66106a1ffd94e8ea227815a935d59ea"><span><div id="d66106a1ffd94e8ea227815a935d59ea" class="notion-header-anchor"></div><a class="notion-hash-link" href="#d66106a1ffd94e8ea227815a935d59ea" title="创建"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">创建</span></span></h2><div class="notion-text notion-block-6706cadf9b0c418aaa6d741e2b369c18">创建数据库：<code class="notion-inline-code">CREATE DATABASE 数据库名称;</code></div><div class="notion-text notion-block-f2f1a079d5a04e65bdcb19aef3a5ace1">创建数据库下的表格：<code class="notion-inline-code">CREATE TABLE 数据表名称(列名1 数据类型,列名2 数据类型,...,列名n,数据类型);</code></div><div class="notion-text notion-block-8ba76f72a0d748f88eedfdf2d50ad9b6">创建数据库：<code class="notion-inline-code">CREATE DATABASE 数据库名称;</code> 创建数据库下的shu<code class="notion-inline-code">CREATE TABLE 数据表名称;</code></div><div class="notion-text notion-block-edb1a653928344759502aeaa92d8c908">USE library; 这一命令表示使用library这一数据库，适用于有多个数据库的情况。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-86ad2af0bddf4bfc8c2be9b9c0d6bf66"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F2dfb5738-15c2-4601-9e70-ddb37d63d75c%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-20_195127.png?table=block&amp;id=86ad2af0-bddf-4bfc-8c2b-e9b9c0d6bf66&amp;t=86ad2af0-bddf-4bfc-8c2b-e9b9c0d6bf66" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-71c69b8318e6463d85ab35ee6160a764">下面是一些常见的数据类型：</div><table class="notion-simple-table notion-block-f3dcddf0bd55449c86fecda061073ff5"><tbody><tr class="notion-simple-table-row notion-block-249586f328524d2a868912f5c1f66830"><td class="" style="width:184px"><div class="notion-simple-table-cell">数据类型</div></td><td class="" style="width:524px"><div class="notion-simple-table-cell">说明</div></td></tr><tr class="notion-simple-table-row notion-block-c45e852fe850435b8cc5597d2d596a50"><td class="" style="width:184px"><div class="notion-simple-table-cell">varchar(最长255)</div></td><td class="" style="width:524px"><div class="notion-simple-table-cell">可变长度的字符串，varchar(10)，10表示最大可分配空间，会根据传过来的数据动态分配。节省空间，但是需要动态分配空间，速度慢。</div></td></tr><tr class="notion-simple-table-row notion-block-28ce7286b3624c5695f67366f156b390"><td class="" style="width:184px"><div class="notion-simple-table-cell">char(最长255)</div></td><td class="" style="width:524px"><div class="notion-simple-table-cell">定长字符串，可能会导致空间的浪费</div></td></tr><tr class="notion-simple-table-row notion-block-618c6574ac0543d9969877e88d959d83"><td class="" style="width:184px"><div class="notion-simple-table-cell">int(最长11)</div></td><td class="" style="width:524px"><div class="notion-simple-table-cell">数字中的长整型</div></td></tr><tr class="notion-simple-table-row notion-block-e2fc526e839841729b5089f63a771ad8"><td class="" style="width:184px"><div class="notion-simple-table-cell">bigint</div></td><td class="" style="width:524px"><div class="notion-simple-table-cell">数字中的长整型</div></td></tr><tr class="notion-simple-table-row notion-block-8b9d42008ee14244952948aa5fd0e959"><td class="" style="width:184px"><div class="notion-simple-table-cell">float</div></td><td class="" style="width:524px"><div class="notion-simple-table-cell">单精度浮点型数据</div></td></tr><tr class="notion-simple-table-row notion-block-951f95ed1b9d46548b0d84b6fce7cc58"><td class="" style="width:184px"><div class="notion-simple-table-cell">double</div></td><td class="" style="width:524px"><div class="notion-simple-table-cell">双精度浮点型数据</div></td></tr><tr class="notion-simple-table-row notion-block-7dc91577f9724b8599912d56db54e3b3"><td class="" style="width:184px"><div class="notion-simple-table-cell">date</div></td><td class="" style="width:524px"><div class="notion-simple-table-cell">短日期类型</div></td></tr><tr class="notion-simple-table-row notion-block-13cdf3233f9a4596b73f4149ab2c0feb"><td class="" style="width:184px"><div class="notion-simple-table-cell">datetime</div></td><td class="" style="width:524px"><div class="notion-simple-table-cell">长日期类型</div></td></tr></tbody></table><div class="notion-text notion-block-50a2990e054e447abc1d6b9891c6615a">对与创建列名的时候，可以设置一些相应的默认格式。比如：</div><table class="notion-simple-table notion-block-8ec117324431423b8ca37a83e44e2266"><tbody><tr class="notion-simple-table-row notion-block-985dfacf3ad54990906a908d0a563336"><td class="" style="width:345px"><div class="notion-simple-table-cell">格式</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">说明</div></td></tr><tr class="notion-simple-table-row notion-block-ace1a716de054672aec116252103796e"><td class="" style="width:345px"><div class="notion-simple-table-cell">NOT NULL</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">这一列不能为空</div></td></tr><tr class="notion-simple-table-row notion-block-30487262e55d444d9e13e2875bfa565b"><td class="" style="width:345px"><div class="notion-simple-table-cell">NULL</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">这一列可以为空</div></td></tr><tr class="notion-simple-table-row notion-block-1d14798298374c56be68274f45793131"><td class="" style="width:345px"><div class="notion-simple-table-cell">PRIMARY KEY</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">主键</div></td></tr><tr class="notion-simple-table-row notion-block-b5a7c80c6fcd42b4ac08a5d40f61b690"><td class="" style="width:345px"><div class="notion-simple-table-cell">AUTO_INCREMENT</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">自动增加数字</div></td></tr><tr class="notion-simple-table-row notion-block-bfc46361f35a42948b836b5448567e60"><td class="" style="width:345px"><div class="notion-simple-table-cell">UNIQUE</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">不允许重复</div></td></tr></tbody></table><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-465d9e0cee6d4c81b108f936adf40926" data-id="465d9e0cee6d4c81b108f936adf40926"><span><div id="465d9e0cee6d4c81b108f936adf40926" class="notion-header-anchor"></div><a class="notion-hash-link" href="#465d9e0cee6d4c81b108f936adf40926" title="插入"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">插入</span></span></h2><div class="notion-text notion-block-eb06ce64a14e4253b2ab39496a6f8386"><code class="notion-inline-code">INSERT INTO 数据库名.表格名 (列名1,列名2,…,列名n) VALUES (数值1,数值2,…,数值n)</code></div><div class="notion-text notion-block-db5d3dfb6d0444f6a567cb3e62a78f80">这样我们就插入了一些数据</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3cba60b3e90b4c0594cee17fdd4e9dec"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F8c68d827-ad72-4cb0-937e-1353373f6b93%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-20_202158.png?table=block&amp;id=3cba60b3-e90b-4c05-94ce-e17fdd4e9dec&amp;t=3cba60b3-e90b-4c05-94ce-e17fdd4e9dec" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-e5916f0b17bd4c5f87c07562a344be0d">其中，DEFAULT表示默认格式。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-07f400013853465e97303b71d873001b" data-id="07f400013853465e97303b71d873001b"><span><div id="07f400013853465e97303b71d873001b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#07f400013853465e97303b71d873001b" title="更新"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">更新</span></span></h2><div class="notion-text notion-block-ea63a4c2ca9f41d9aef5db95f5f19ab0">我们也可以再增加一个列名：<code class="notion-inline-code">ALTER TABLE 数据库名.表格名 ADD 列名 数据类型 默认条件</code></div><div class="notion-text notion-block-74944cc58fbc40e997b4d14320f49918">更新具体数据：<code class="notion-inline-code">UPDATE 数据库名.表格名 SET 值 WHERE 条件</code></div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-797ff2e7d9ce40e4b4e4981741b642bb"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F44fe9730-0fed-4ddf-90b4-6ca82602a317%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-20_203240.png?table=block&amp;id=797ff2e7-d9ce-40e4-b4e4-981741b642bb&amp;t=797ff2e7-d9ce-40e4-b4e4-981741b642bb" alt="notion image" loading="lazy" decoding="async"/></div></figure><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-9260e1234b9345b194de5efa09cbfcb0" data-id="9260e1234b9345b194de5efa09cbfcb0"><span><div id="9260e1234b9345b194de5efa09cbfcb0" class="notion-header-anchor"></div><a class="notion-hash-link" href="#9260e1234b9345b194de5efa09cbfcb0" title="删除"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">删除</span></span></h2><div class="notion-text notion-block-c6ae175be49a4944a4b5ae152cd73a05">删除某条数据：<code class="notion-inline-code">DELETE FROM 数据库名.表格名 WHERE 条件</code></div><div class="notion-text notion-block-775b4ed9b4e2409bbac9b350bde3b5b8">删除表格：<code class="notion-inline-code">DROP TABLE 数据库名.表格名</code></div><div class="notion-text notion-block-6b6ebdf01d924f0aad91cc508b27b244">删除数据库：<code class="notion-inline-code">DROP DATABASE 数据库名</code></div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-37fda9f83658465fb873ba69a2d76788"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F03718f97-0849-4569-8c97-be4b81cb604f%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-20_203622.png?table=block&amp;id=37fda9f8-3658-465f-b873-ba69a2d76788&amp;t=37fda9f8-3658-465f-b873-ba69a2d76788" alt="notion image" loading="lazy" decoding="async"/></div></figure><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-34ebb78d0c504ce98597dffdcf9326b0" data-id="34ebb78d0c504ce98597dffdcf9326b0"><span><div id="34ebb78d0c504ce98597dffdcf9326b0" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34ebb78d0c504ce98597dffdcf9326b0" title="查找"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">查找</span></span></h2><div class="notion-text notion-block-bd3fbe6d549441f58754468b81b0b41c">查找全部内容：<code class="notion-inline-code">SELECT * FROM 数据库名.表格名</code></div><div class="notion-text notion-block-2f8aed4c93144a36b9de1fcb90415834">查找某一列内容：<code class="notion-inline-code">SELECT 列名1，列名2 FROM 数据库名.表格名</code></div><div class="notion-text notion-block-1b326dd536a9402dac1bb13ff983d040">查找不同的内容（即去除重复内容）：<code class="notion-inline-code">SELECT DISTINCT 列名1 FROM 数据库名.表格名</code></div><div class="notion-text notion-block-3c7dd9fe5cf149879aeaf5be7a7f0ed1">查看时排序（默认ASC，即ascending，升序；DESC，descending，降序）：
<code class="notion-inline-code">SELECT * FROM 数据库名.表格名 ORDER BY 列名 ASC/DESC</code> </div><div class="notion-text notion-block-fafe1545397146d0857e2620be5fdc48">要过滤掉某些信息：<code class="notion-inline-code">SELECT * FROM 数据库名.表格名 WHERE 条件 ORDER BY 列名 ASC/DESC</code> </div><table class="notion-simple-table notion-block-c7d8ec41c85e4cf3bbcd3fa8bc539bc0"><tbody><tr class="notion-simple-table-row notion-block-90318cbc6bab4eda88f61a1c2fae2141"><td class="" style="width:345px"><div class="notion-simple-table-cell">运算符</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">说明</div></td></tr><tr class="notion-simple-table-row notion-block-ee0635722bb743dfb9c3df6e2bce8ba8"><td class="" style="width:345px"><div class="notion-simple-table-cell">=</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">等于</div></td></tr><tr class="notion-simple-table-row notion-block-4486a5b2f87a417fae3ec8aaa69ba702"><td class="" style="width:345px"><div class="notion-simple-table-cell">! = 或 &lt;&gt;</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">不等于</div></td></tr><tr class="notion-simple-table-row notion-block-f396187bfe2246c686624f7834d2dc16"><td class="" style="width:345px"><div class="notion-simple-table-cell">&gt;</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">大于</div></td></tr><tr class="notion-simple-table-row notion-block-a7d73c876c4c4d89b4b78360792b0aca"><td class="" style="width:345px"><div class="notion-simple-table-cell">&lt;</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">小于</div></td></tr><tr class="notion-simple-table-row notion-block-c428c143486a481b8ce8646fcbaf6566"><td class="" style="width:345px"><div class="notion-simple-table-cell">&gt; =</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">大于等于</div></td></tr><tr class="notion-simple-table-row notion-block-10747353c9f54ccd91cf60c4cc554e50"><td class="" style="width:345px"><div class="notion-simple-table-cell">&lt; =</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">小于等于</div></td></tr><tr class="notion-simple-table-row notion-block-ed76919b7faf42778a77c40082671aae"><td class="" style="width:345px"><div class="notion-simple-table-cell">BETWEEN</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">介于两者之间</div></td></tr><tr class="notion-simple-table-row notion-block-bcccb9f00df342fcaabf3b3bfc185607"><td class="" style="width:345px"><div class="notion-simple-table-cell">IN</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">在一组值内</div></td></tr><tr class="notion-simple-table-row notion-block-627078e571a94d12ae0fc139cd4f1957"><td class="" style="width:345px"><div class="notion-simple-table-cell">LIKE</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">相似匹配</div></td></tr><tr class="notion-simple-table-row notion-block-00ce91ddef834bda829d97d52b85d1d1"><td class="" style="width:345px"><div class="notion-simple-table-cell">AND</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">与</div></td></tr><tr class="notion-simple-table-row notion-block-3902962f42b740f1bf9e237f2076634b"><td class="" style="width:345px"><div class="notion-simple-table-cell">OR</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">或</div></td></tr><tr class="notion-simple-table-row notion-block-3995d639121a4087b3b8b94102b6c8ae"><td class="" style="width:345px"><div class="notion-simple-table-cell">NOT 或 !</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">非</div></td></tr></tbody></table><div class="notion-text notion-block-6196500ab9a2460c853ba6a2cf249bb9">比如想知道这些书分别在哪几天入库：</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-4a7b5e421e1f413e90bb9a8b2e4648d1"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F200f3fdf-d783-4b2c-bc04-a4bb4d829f10%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-20_204233.png?table=block&amp;id=4a7b5e42-1e1f-413e-90bb-9a8b2e4648d1&amp;t=4a7b5e42-1e1f-413e-90bb-9a8b2e4648d1" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-57c7008f7b724035afae8350cfd96a77">去重后：</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-d7d600094d914e0ca081be33f5f2ea6a"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2F36be82bf-ceb7-4906-9838-8ae2a9cb81fa%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-20_204303.png?table=block&amp;id=d7d60009-4d91-4e0c-a081-be33f5f2ea6a&amp;t=d7d60009-4d91-4e0c-a081-be33f5f2ea6a" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-1c604c0e999c40689836ead1ae66ccca"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F5382554c-01ac-4db9-a43f-3aa41cfbaa83%2Fa2d0bc98-cd8d-4926-a20c-706e1e459fd4%2F%25E5%25B1%258F%25E5%25B9%2595%25E6%2588%25AA%25E5%259B%25BE_2024-03-20_205333.png?table=block&amp;id=1c604c0e-999c-4068-9836-ead1ae66ccca&amp;t=1c604c0e-999c-4068-9836-ead1ae66ccca" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-072243f31613421ea927b47b633be9d3">在 SQL 中，通配符与 SQL LIKE 操作符一起使用，下面具体说说LIKE的用法：</div><table class="notion-simple-table notion-block-9255e7060fc1408badba9cbf256bbd97"><tbody><tr class="notion-simple-table-row notion-block-03a37b40e9f045a993a450e4c11e12ed"><td class="" style="width:345px"><div class="notion-simple-table-cell">通配符</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">描述</div></td></tr><tr class="notion-simple-table-row notion-block-0a26b5f3c9e245fdbe60d7a3f98eecf1"><td class="" style="width:345px"><div class="notion-simple-table-cell">%</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">替代 0 个或多个字符</div></td></tr><tr class="notion-simple-table-row notion-block-62d128f7b7874f8fade74f24c6f1710c"><td class="" style="width:345px"><div class="notion-simple-table-cell">_</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">替代一个字符</div></td></tr><tr class="notion-simple-table-row notion-block-3615c9b920764f42b1491129cc9434df"><td class="" style="width:345px"><div class="notion-simple-table-cell">[<em>charlist</em>]</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">字符列中的任何单一字符</div></td></tr><tr class="notion-simple-table-row notion-block-ce3ec45196244fb59eb1c57cd6ef4166"><td class="" style="width:345px"><div class="notion-simple-table-cell">[^<em>charlist</em>]<!-- -->或<!-- -->[!<em>charlist</em>]</div></td><td class="" style="width:345px"><div class="notion-simple-table-cell">不在字符列中的任何单一字符</div></td></tr></tbody></table></main></div>]]></content:encoded>
        </item>
    </channel>
</rss>