-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.xml
2879 lines (2859 loc) · 271 KB
/
feed.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<generator uri="https://gohugo.io/" version="0.130.0">Hugo</generator>
<title>Weily's blog</title>
<subtitle>I write, therefore I am.</subtitle>
<link href="https://weilycoder.github.io/" rel="alternate" type="text/html" title="html" />
<link href="https://weilycoder.github.io/feed.xml" rel="self" type="application/atom+xml" title="atom" />
<updated>2024-12-30T16:05:56+08:00</updated>
<author>
<name>weily</name>
<email>[email protected]</email>
</author>
<id>https://weilycoder.github.io/</id>
<entry>
<title>Who I am</title>
<link href="https://weilycoder.github.io/post/about/" rel="alternate" type="text/html" hreflang="en" />
<id>https://weilycoder.github.io/post/about/</id>
<published>2024-06-01T00:35:21+08:00</published>
<updated>2024-06-01T00:35:21+08:00</updated>
<content type="html">
<p><a
class="gblog-markdown__link--raw"
href="https://badges.toozhao.com/stats/01HYYXADZH998DH2N5QTGZSZG1"
><img
src="https://badges.toozhao.com/badges/01HYYXADZH998DH2N5QTGZSZG1/green.svg"
alt="Page Views Count"
/></a>
<a
class="gblog-markdown__link--raw"
href="https://codeforces.com/profile/weily"
><img
src="https://cfrating.baoshuo.dev/rating?username=weily&amp;style=flat"
alt="Codeforces Rating of @weily"
/></a>
<a
class="gblog-markdown__link--raw"
href="https://atcoder.jp/users/weily"
><img
src="https://atrating.baoshuo.dev/rating?username=weily&amp;style=flat"
alt="AtCoder Rating of @weily"
/></a>
<a
class="gblog-markdown__link--raw"
href="https://github.com/weilycoder"
><img
src="https://img.shields.io/badge/github-weilycoder-blue?logo=github"
alt="github"
/></a></p>
<blockquote>
<p>朝乾夕惕,功不唐捐。</p>
</blockquote>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="自我介绍"
>
自我介绍
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/about/#自我介绍" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 自我介绍" href="#%e8%87%aa%e6%88%91%e4%bb%8b%e7%bb%8d">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<ul>
<li>👋 Hi, I’m weily</li>
<li>🇨🇳 来自中国的一名高中生</li>
<li>👀 对数学和计算机科学很感兴趣</li>
<li>🌱 现已考上<a
class="gblog-markdown__link"
href="http://sdshiyan.jinan.cn/"
>山东省实验中学</a>推荐生</li>
<li>💞️ 正在学习算法竞赛(Olympiad in Informatics)</li>
<li>📫 How to reach me &hellip;
<ul>
<li>QQ: 1991809701 / 40726462</li>
<li>e-mail: <a
class="gblog-markdown__link"
href="mailto:[email protected]"
>[email protected]</a></li>
<li>luogu: <a
class="gblog-markdown__link"
href="https://www.luogu.com/user/818693"
>wly09</a></li>
</ul>
</li>
<li>⚡ 已成为 <a
class="gblog-markdown__link"
href="https://github.com/OI-wiki/OI-wiki"
>OI-wiki</a> 贡献者!</li>
</ul>
<p>另外,我在<a
class="gblog-markdown__link"
href="https://www.cnblogs.com/weily09"
>博客园</a>也会发布文章。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="声明"
>
声明
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/about/#声明" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 声明" href="#%e5%a3%b0%e6%98%8e">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<ol start="0">
<li>Page Views Count 是我的所有个人介绍页面公用的,而且大部分是我的访问。</li>
<li>博客托管在 <a
class="gblog-markdown__link"
href="https://github.com"
>Github</a> 提供的静态页面服务,网站构建使用 <a
class="gblog-markdown__link"
href="https://gohugo.io/"
>Hugo</a>,主题风格来自 <a
class="gblog-markdown__link"
href="https://hugo-geekblog.geekdocs.de/"
>hugo-geekblog</a>。</li>
<li>博客文章使用 <a
class="gblog-markdown__link"
href="https://creativecommons.org/licenses/by-sa/4.0/"
>CC BY-SA 4.0</a> 协议。</li>
<li>数学公式由 <a
class="gblog-markdown__link"
href="https://www.mathjax.org/"
>MathJax</a> 支持。</li>
<li>学习时可能搬一些 <a
class="gblog-markdown__link"
href="https://oi-wiki.org"
>OI-Wiki</a> 的内容,会注明。</li>
</ol>
</content>
<category scheme="https://weilycoder.github.io/tags/About" term="About" label="About" />
</entry>
<entry>
<title>Floyd 算法</title>
<link href="https://weilycoder.github.io/post/floyd/" rel="alternate" type="text/html" hreflang="en" />
<id>https://weilycoder.github.io/post/floyd/</id>
<author>
<name>weily</name>
</author>
<published>2024-11-05T20:40:07+08:00</published>
<updated>2024-11-05T20:40:07+08:00</updated>
<content type="html">
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="floyd-算法"
>
Floyd 算法
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/floyd/#floyd-算法" class="gblog-post__anchor clip flex align-center" aria-label="Anchor Floyd 算法" href="#floyd-%e7%ae%97%e6%b3%95">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>用于求图中任意两点最短路算法。</p>
<p>Floyd 算法本质是 dp。设 $i\to j$ 最短路为 $f_{i,j}$,初始置 $f_{i,j}$ 为 $i\to j$ 的边权;若 $i\to j$ 没有直接连边,置 $f_{i,j}$ 为 $+\infty$。</p>
<p>枚举 $k$,检查 $i\to k\to j$ 是否比原有的路径长度短,即令</p>
<p>$$
f_{i,j}\gets \min(f_{i,j},f_{i,k}+f_{k,j})
$$</p>
<p>当 $k$ 遍历过所有点,全局最短路就确定了。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="o">++</span><span class="n">k</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="o">++</span><span class="n">j</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">min</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">],</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="n">f</span><span class="p">[</span><span class="n">k</span><span class="p">][</span><span class="n">i</span><span class="p">]);</span></span></span></code></pre></td></tr></table>
</div>
</div>
<p>注意 Floyd 算法的枚举顺序是 $(k,i,j)$,更改枚举顺序会导致算法出错。</p>
<p>值得一提,在<a
class="gblog-markdown__link"
href="https://arxiv.org/pdf/1904.01210"
>这篇论文</a>证明,对于 $(i,k,j)$ 的枚举顺序,算法最多跑 $2$ 遍即可得到正确结果;对于 $(i,j,k)$ 的枚举顺序,算法最多跑 $3$ 遍即可得到正确结果。</p>
<p>不过,对单个变量的枚举顺序显然没有要求,正序倒序乱序显然都可以。</p>
<p>特别地,假设枚举变量 $k$ 已经遍历且仅遍历了 $S$ 中的点,当前答案的意义就是:只经过 $S$ 中的点作为“中继”,任意两点的最短路。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="bitset-优化传递闭包"
>
bitset 优化传递闭包
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/floyd/#bitset-优化传递闭包" class="gblog-post__anchor clip flex align-center" aria-label="Anchor bitset 优化传递闭包" href="#bitset-%e4%bc%98%e5%8c%96%e4%bc%a0%e9%80%92%e9%97%ad%e5%8c%85">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>计算任意两点是否可达。</p>
<p>我们对 dp 数组的含义重新定义,初始若 $i\to j$ 有边,置 $f_{i,j}$ 为 $1$,否者置为 $0$,特别地,$i=j$ 时应置为 $1$。</p>
<p>转移方程为:</p>
<p>$$
f_{i,j}\gets f_{i,j}\cup(f_{i,k}\cap f_{k,j})
$$</p>
<p>这里 $\cup$ 表示按位或;$\cap$ 表示按位与。</p>
<p>考虑用 <code>bitset</code> 优化。</p>
<p>我们用 <code>bitset</code> 数组作为 dp 数组,$f_i$ 是一个长 $N$ 的 <code>bitset</code>。</p>
<p>转移方程显然可以看成:</p>
<p>$$
f_{i,j}=
\begin{cases}
f_{i,j}\cup f_{k,j},&amp;\text{if }f_{i,k}=\text{true} \\
f_{i,j},&amp;\text{otherwise}
\end{cases}
$$</p>
<p>注意到若 $f_{i,k}$ 为真,则需遍历所有 $j$ 对 $f_{i,j}$ 和 $f_{k,j}$ 取或。</p>
<p>这个过程可以用 <code>bitset</code> 优化。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="o">++</span><span class="n">k</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">k</span><span class="p">])</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">|=</span> <span class="n">f</span><span class="p">[</span><span class="n">k</span><span class="p">];</span></span></span></code></pre></td></tr></table>
</div>
</div>
<p>由于 <code>bitset</code> 实现上会压位,时间复杂度是 $O\left(\dfrac{N^3}{w}\right)$,$w$ 是计算机的位数,一般是 $32$ 或 $64$。</p>
<p>开 O2 优化后,编译器可能会进行循环展开,这会使常数变为原来的 $\dfrac18\sim \dfrac14$。因此,Floyd 传递闭包的复杂度尽管是 $O(n^3)$,但常常可以跑过 $3000$ 左右的数据。</p>
</content>
<category scheme="https://weilycoder.github.io/authors/weily" term="weily" label="weily" />
<category scheme="https://weilycoder.github.io/tags/graph" term="graph" label="graph" />
</entry>
<entry>
<title>拉格朗日插值</title>
<link href="https://weilycoder.github.io/post/lag-interp/" rel="alternate" type="text/html" hreflang="en" />
<id>https://weilycoder.github.io/post/lag-interp/</id>
<published>2024-11-05T11:21:12+08:00</published>
<updated>2024-11-05T11:21:12+08:00</updated>
<content type="html">
<blockquote>
<p>内容基本来自 <a
class="gblog-markdown__link"
href="https://oi-wiki.org"
>OI-Wiki</a>。</p>
</blockquote>
<p>插值是通过已知的、离散的数据点,推算未知的新数据点的方法。</p>
<p>最普通地,可以将原数据点用线段连接,构成分段函数,叫做线性插值;C++ 20 开始,标准库实现了 <a
class="gblog-markdown__link--code"
href="https://zh.cppreference.com/w/cpp/numeric/lerp"
><code>std::lerp</code></a> 函数,用于线性插值。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="拉格朗日插值法"
>
拉格朗日插值法
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/lag-interp/#拉格朗日插值法" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 拉格朗日插值法" href="#%e6%8b%89%e6%a0%bc%e6%9c%97%e6%97%a5%e6%8f%92%e5%80%bc%e6%b3%95">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>但是,我们通常对精度有更高的要求;一般的,已知 $n+1$ 个点 $(x_0,y_0),(x_1,y_1),\cdots,(x_n,y_n)$ <strong>通常</strong>可以求出 $n$ 次多项式 $f(x)$ 满足 $f(x_i)=y_i,\forall i=0,1,\cdots,n$。</p>
<p>最暴力地,我们可以使用高斯消元 $O(n^3)$ 计算 $f(x)$,单次插值可以 $O(n)$。</p>
<p>朴素的拉格朗日插值无需计算具体的函数 $f(x)$,单次插值 $O(n^2)$,通过多项式技巧可以做到 $O(n\log^2 n)$。</p>
<p>具体地,记原数据点为 $P_1(x_1,y_1),P_2(x_2,y_2),\cdots,P_n(x_n,y_n)$,它们在 $x$ 轴上的投影为 $P&rsquo;_1(x_1,0),P&rsquo;_2(x_2,0),\cdots,P&rsquo;_n(x_n,0)$。</p>
<p>考虑构造 $n$ 个函数,第 $i$ 个函数 $f_i(x)$ 过 $\begin{cases}P_j ,&amp; j=i \\ P&rsquo;_j ,&amp; \text{otherwise}\end{cases}$。</p>
<p>不妨令 $f_i(x)=a\cdot\prod_{j\ne i}(x-x_j)$,将 $P_i$ 代入,得 $a=\dfrac{y_i}{\prod_{j\ne i}(x_i-x_j)}$,于是</p>
<p>$$
f_i(x)=y_i\cdot\dfrac{\prod_{j\ne i}(x-x_j)}{\prod_{j\ne i}(x_i-x_j)}=y_i\cdot\prod_{j\ne i}\dfrac{x-x_j}{x_i-x_j}
$$</p>
<p>因此所求函数 $f(x)$ 即为:</p>
<p>$$
f(x)=\sum_{i=1}^{n}y_i\cdot\prod_{j\ne i}\dfrac{x-x_j}{x_i-x_j}
$$</p>
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="横坐标为连续整数的插值"
>
横坐标为连续整数的插值
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/lag-interp/#横坐标为连续整数的插值" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 横坐标为连续整数的插值" href="#%e6%a8%aa%e5%9d%90%e6%a0%87%e4%b8%ba%e8%bf%9e%e7%bb%ad%e6%95%b4%e6%95%b0%e7%9a%84%e6%8f%92%e5%80%bc">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>例如,已知 $1\sim n+1$ 的点值,有:</p>
<p>$$
f(x)=\sum\limits_{i=1}^{n+1}(-1)^{n+1-i}y_i\cdot\frac{\prod\limits_{j=1}^{n+1}(x-j)}{(i-1)!(n+1-i)!(x-i)}
$$</p>
<p>预处理后线性。</p>
</content>
<category scheme="https://weilycoder.github.io/tags/math" term="math" label="math" />
</entry>
<entry>
<title>辛普森法求数值积分</title>
<link href="https://weilycoder.github.io/post/simpson/" rel="alternate" type="text/html" hreflang="en" />
<id>https://weilycoder.github.io/post/simpson/</id>
<published>2024-08-10T11:35:20+08:00</published>
<updated>2024-08-10T11:35:20+08:00</updated>
<content type="html">
<blockquote>
<p>部分内容来自 <a
class="gblog-markdown__link"
href="https://oi-wiki.org"
>OI-Wiki</a>。</p>
</blockquote>
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="辛普森公式"
>
辛普森公式
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/simpson/#辛普森公式" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 辛普森公式" href="#%e8%be%9b%e6%99%ae%e6%a3%ae%e5%85%ac%e5%bc%8f">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>首先,我们引入二次函数积分公式。</p>
<p>对于二次函数</p>
<p>$$
f(x)=ax^2+bx+c,
$$</p>
<p>有</p>
<p>$$
\int_{L}^{R} f(x)\text{d}x=\dfrac{R-L}{6}\left[f(L)+f(R)+4f\left(\dfrac{L+R}{2}\right)\right]
$$</p>
<p>这里给出计算过程:</p>
<p>先求出 $f(x)$ 的原函数:</p>
<p>$$
F(x)=\int f(x)\text{d}x=\dfrac{a}{3}x^3+\dfrac{b}{2}x^2+cx+D
$$</p>
<p>由牛顿-莱布尼茨公式,</p>
<p>$$
\begin{aligned}
\int_{L}^{R} f(x)\text{d}x
&amp;=F(R)-F(L)\\
&amp;=\dfrac{a}{3}(R^3-L^3)+\dfrac{b}{2}(R^2-L^2)+c(r-l)\\
&amp;=(R-L)\left(\dfrac{a}{3}(L^2+LR+R^2)+\dfrac{b}{2}(L+R)+c\right)\\
&amp;=\dfrac{R-L}{6}(2L^2+2LR+2R^2+3L+3R+6c)\\
&amp;=\dfrac{R-L}{6}\left\{(aL^2+bL+c)+(aR^2+bR+c)+4\left[a\left(\dfrac{l+r}{2}\right)^2+b\left(\dfrac{l+r}{2}\right)+c\right]\right\}\\
&amp;=\dfrac{R-L}{6}\left[f(L)+f(R)+4f\left(\dfrac{L+R}{2}\right)\right]
\end{aligned}
$$</p>
<p>需要注意这里其实不要求 $a\ne 0$。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="自适应辛普森法"
>
自适应辛普森法
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/simpson/#自适应辛普森法" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 自适应辛普森法" href="#%e8%87%aa%e9%80%82%e5%ba%94%e8%be%9b%e6%99%ae%e6%a3%ae%e6%b3%95">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>考虑使用二次函数拟合被积函数,将被积函数分成若干段,每段内使用辛普森公式求近似值。</p>
<p>但是,如果我们不断细分,无疑会增大算量;如果细分的程度不够,又会影响精度。</p>
<p>一般,我们先将函数分成几段,接下来对每段检查二次函数是否能良好拟合。</p>
<p>具体地,对于函数的某一段 $[l,r]$,如果拟合 $[l,r]$ 的结果跟 $[l,\frac{l+r}{2}]$ 与 $[\frac{l+r}{2},r]$ 的和接近,那么认为拟合良好。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="kt">double</span> <span class="nf">simpson</span><span class="p">(</span><span class="kt">double</span> <span class="n">l</span><span class="p">,</span> <span class="kt">double</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kt">double</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">l</span> <span class="o">+</span> <span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">(</span><span class="n">r</span> <span class="o">-</span> <span class="n">l</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="n">l</span><span class="p">)</span> <span class="o">+</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">f</span><span class="p">(</span><span class="n">mid</span><span class="p">)</span> <span class="o">+</span> <span class="n">f</span><span class="p">(</span><span class="n">r</span><span class="p">))</span> <span class="o">/</span> <span class="mi">6</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">double</span> <span class="nf">asr</span><span class="p">(</span><span class="kt">double</span> <span class="n">l</span><span class="p">,</span> <span class="kt">double</span> <span class="n">r</span><span class="p">,</span> <span class="kt">double</span> <span class="n">eps</span><span class="p">,</span> <span class="kt">double</span> <span class="n">ans</span><span class="p">,</span> <span class="kt">int</span> <span class="n">step</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kt">double</span> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">l</span> <span class="o">+</span> <span class="n">r</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="kt">double</span> <span class="n">fl</span> <span class="o">=</span> <span class="n">simpson</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">mid</span><span class="p">),</span> <span class="n">fr</span> <span class="o">=</span> <span class="n">simpson</span><span class="p">(</span><span class="n">mid</span><span class="p">,</span> <span class="n">r</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="n">abs</span><span class="p">(</span><span class="n">fl</span> <span class="o">+</span> <span class="n">fr</span> <span class="o">-</span> <span class="n">ans</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">15</span> <span class="o">*</span> <span class="n">eps</span> <span class="o">&amp;&amp;</span> <span class="n">step</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="n">fl</span> <span class="o">+</span> <span class="n">fr</span> <span class="o">+</span> <span class="p">(</span><span class="n">fl</span> <span class="o">+</span> <span class="n">fr</span> <span class="o">-</span> <span class="n">ans</span><span class="p">)</span> <span class="o">/</span> <span class="mi">15</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="n">asr</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">mid</span><span class="p">,</span> <span class="n">eps</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">fl</span><span class="p">,</span> <span class="n">step</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span>
</span></span><span class="line"><span class="cl"> <span class="n">asr</span><span class="p">(</span><span class="n">mid</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">eps</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">fr</span><span class="p">,</span> <span class="n">step</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">double</span> <span class="nf">calc</span><span class="p">(</span><span class="kt">double</span> <span class="n">l</span><span class="p">,</span> <span class="kt">double</span> <span class="n">r</span><span class="p">,</span> <span class="kt">double</span> <span class="n">eps</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="n">asr</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">eps</span><span class="p">,</span> <span class="n">simpson</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">),</span> <span class="mi">12</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
<p>其中 <code>15</code> 是一个 Magic Number,在 <a
class="gblog-markdown__link"
href="https://doi.org/10.1145/321526.321537"
>https://doi.org/10.1145/321526.321537</a> 给出了论述。</p>
<p>需要指出,也存在一些病态函数,直接应用自适应辛普森法可能会出错。</p>
</content>
<category scheme="https://weilycoder.github.io/tags/math" term="math" label="math" />
</entry>
<entry>
<title>道高一尺 or 魔高一丈:浅谈交互库编写</title>
<link href="https://weilycoder.github.io/post/interactive/" rel="alternate" type="text/html" hreflang="en" />
<id>https://weilycoder.github.io/post/interactive/</id>
<author>
<name>weily</name>
</author>
<published>2024-08-04T14:22:57+08:00</published>
<updated>2024-08-04T14:22:57+08:00</updated>
<content type="html">
<p>有些时候,为了实现特殊的需求,我们可能需要编写交互库,要求选手链接。</p>
<p>常见的情景有:</p>
<ul>
<li>强制在线:一些题目不强制在线可能会被“乱搞”通过。例如可持久化数据结构有众所周知的离线做法。</li>
<li>加速输入输出:一些题目为了要求严格线性可能需要 $10^8$ 以上的输入量,如此大的数据必须在内存生成并交换。</li>
<li>限制操作:一些思维题不允许选手直接读取数据,而是要求选手做特定询问获取详细内容;或者可能限制操作次数。</li>
<li>人机对抗:另一些思维题要求选手找到最优策略,那么可以要求选手通过接口与交互库对抗。</li>
</ul>
<p>既然交互库要链接选手的程序,就必须做好防范措施,避免选手使用不当操作 $\textcolor{green}{\text{AC}}$。</p>
<p>本人才疏学浅,谈论的内容只是全局的一小部分,欢迎补充。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="hack-输出内容"
>
Hack 输出内容
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/interactive/#hack-输出内容" class="gblog-post__anchor clip flex align-center" aria-label="Anchor Hack 输出内容" href="#hack-%e8%be%93%e5%87%ba%e5%86%85%e5%ae%b9">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>如果不要求选手进行输出,很多交互库可能只是简单输出 <code>Yes</code> 或 <code>No</code> 来告诉 Judger 结果是否正确。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="0x00"
>
0x00
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/interactive/#0x00" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 0x00" href="#0x00">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>一个基本的素养是将除了 <code>main</code> 函数和接口函数以外的函数或变量使用 <code>static</code> 修饰,并且不使用 <code>#define</code> 或 <code>using</code>。防止出现奇怪问题或被选手猜中变量名。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="0x01"
>
0x01
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/interactive/#0x01" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 0x01" href="#0x01">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>这里有一个显然的漏洞。众所周知,函数 <code>exit</code> 可以让程序退出,不必返回主函数,选手自然可以输出 <code>Yes</code> 后直接退出。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">solve</span><span class="p">(...)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="p">...</span>
</span></span><span class="line"><span class="cl"> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;Yes&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="0x02"
>
0x02
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/interactive/#0x02" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 0x02" href="#0x02">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>最简单的方式是不告诉选手交互库评测时行为。</p>
<p>不过,在部分题目中,这可能是无法避免的。</p>
<p>一个简单的做法是利用析构函数,只要定义一个全局变量,程序退出时自然会进行析构。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="k">static</span> <span class="k">struct</span> <span class="nc">E</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kt">int</span> <span class="n">code</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="o">~</span><span class="n">E</span><span class="p">()</span> <span class="p">{</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">code</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span> <span class="n">e</span><span class="p">;</span></span></span></code></pre></td></tr></table>
</div>
</div>
<p>另一个高级的方法是使用 <code>atexit</code> 注册函数,被注册的函数会在程序正常退出时被逆序调用。</p>
<p><code>atexit</code> 定义在 <code>&lt;cstdlib&gt;</code>。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="p">...</span>
</span></span><span class="line"><span class="cl"><span class="k">static</span> <span class="kt">int</span> <span class="n">code</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="p">...</span>
</span></span><span class="line"><span class="cl"> <span class="k">auto</span> <span class="n">p</span> <span class="o">=</span> <span class="p">[]{</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">code</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">};</span>
</span></span><span class="line"><span class="cl"> <span class="n">atexit</span><span class="p">(</span><span class="n">p</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="p">...</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
<p>要注意的是以上两种方法只能在函数正常退出时输出;不过程序非正常退出时一定不会返回 <code>0</code>,会出现 $\textcolor{purple}{\text{RE}}$。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="0x03"
>
0x03
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/interactive/#0x03" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 0x03" href="#0x03">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>不过最近看到一个有趣的函数:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="na">[[noreturn]]</span> <span class="kt">void</span> <span class="n">quick_exit</span><span class="p">(</span> <span class="kt">int</span> <span class="n">exit_code</span> <span class="p">)</span> <span class="k">noexcept</span><span class="p">;</span>
</span></span></code></pre></div><p>这个函数也定义在 <code>&lt;cstdlib&gt;</code>。顾名思义,这个函数可以使程序快速退出,不完全清理资源。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;cstdlib&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">static</span> <span class="k">struct</span> <span class="nc">E</span> <span class="p">{</span> <span class="o">~</span><span class="n">E</span><span class="p">()</span> <span class="p">{</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;clean&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="n">e</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">quick_exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="p">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
<p>尝试运行以上程序,你会发现不会输出任何东西。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="0x04"
>
0x04
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/interactive/#0x04" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 0x04" href="#0x04">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>不过如果你知道 <code>quick_exit</code>,一定会看到 <code>at_quick_exit</code>,后者也定义在 <code>&lt;cstdlib&gt;</code>。</p>
<p><code>at_quick_exit</code> 可以注册快速退出时执行的函数,不会在正常退出时调用。</p>
<p>这段程序可能更加保险。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="p">...</span>
</span></span><span class="line"><span class="cl"><span class="k">static</span> <span class="kt">int</span> <span class="n">code</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="p">...</span>
</span></span><span class="line"><span class="cl"> <span class="k">auto</span> <span class="n">p</span> <span class="o">=</span> <span class="p">[]{</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">code</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">};</span>
</span></span><span class="line"><span class="cl"> <span class="k">auto</span> <span class="n">q</span> <span class="o">=</span> <span class="p">[]{</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;at_quick_exit&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">};</span>
</span></span><span class="line"><span class="cl"> <span class="n">atexit</span><span class="p">(</span><span class="n">p</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="n">at_quick_exit</span><span class="p">(</span><span class="n">q</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="p">...</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
<p>注意快速退出时不一定刷新缓冲区,需要手动刷新。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="0x06"
>
0x06
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/interactive/#0x06" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 0x06" href="#0x06">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>我的朋友提醒了我 <code>close</code> 函数的使用,一般地,我们可以使用 <code>close</code> 函数干掉文件描述符 <code>1</code>,使得交互库无法正常输出。更进一步,我们可以使用 <code>dup2</code> 将 <code>dev/nul</code> 绑定到描述符 <code>1</code>。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="0xff"
>
0xff
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/interactive/#0xff" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 0xff" href="#0xff">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>至此,在输出上直接做手脚可能比较困难,但选手程序和交互库毕竟被编译到同一程序,高手可以选择直接操作内存。</p>
<p>参见 <a
class="gblog-markdown__link"
href="https://mcfxmcfx.blog.uoj.ac/blog/6519"
>部分交互题的通用 hack 方法</a>,By <a
class="gblog-markdown__link"
href="https://uoj.ac/user/profile/mcfxmcfx"
>mcfxmcfx</a>。</p>
<p>有灵感在记录罢。</p>
</content>
<category scheme="https://weilycoder.github.io/authors/weily" term="weily" label="weily" />
<category scheme="https://weilycoder.github.io/tags/misc" term="misc" label="misc" />
</entry>
<entry>
<title>乘法逆元</title>
<link href="https://weilycoder.github.io/post/inv/" rel="alternate" type="text/html" hreflang="en" />
<id>https://weilycoder.github.io/post/inv/</id>
<published>2024-07-18T22:09:36+08:00</published>
<updated>2024-07-18T22:09:36+08:00</updated>
<content type="html">
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="定义"
>
定义
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/inv/#定义" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 定义" href="#%e5%ae%9a%e4%b9%89">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>若 $ax\equiv 1\pmod{p}$,则称 $x$ 是模 $p$ 意义下 $a$ 的乘法逆元,记作 $a^{-1}$。</p>
<p>显然,它相当于模意义下 $a$ 的倒数。不那么严谨地说,模意义下除以 $a$ 就是乘 $a^{-1}$。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="在线单点求值"
>
在线单点求值
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/inv/#在线单点求值" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 在线单点求值" href="#%e5%9c%a8%e7%ba%bf%e5%8d%95%e7%82%b9%e6%b1%82%e5%80%bc">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="扩展欧几里得法"
>
扩展欧几里得法
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/inv/#扩展欧几里得法" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 扩展欧几里得法" href="#%e6%89%a9%e5%b1%95%e6%ac%a7%e5%87%a0%e9%87%8c%e5%be%97%e6%b3%95">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>由于求逆元相当于求 $ax+py=1$ 的一组整数解,所以可以使用扩展欧几里得法。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">exgcd</span><span class="p">(</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">,</span> <span class="kt">int</span><span class="o">&amp;</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span><span class="o">&amp;</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="n">b</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="n">exgcd</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">%</span> <span class="n">b</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="n">y</span> <span class="o">-=</span> <span class="n">a</span> <span class="o">/</span> <span class="n">b</span> <span class="o">*</span> <span class="n">x</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
<p>复杂度为 $O(\log a)$。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="快速幂法"
>
快速幂法
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/inv/#快速幂法" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 快速幂法" href="#%e5%bf%ab%e9%80%9f%e5%b9%82%e6%b3%95">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>由费马小定理($p$ 为质数):</p>
<p>$$
a^{p}\equiv a\pmod{p}
$$</p>
<p>因此 $(a,p)=1$ 时,</p>
<p>$$
a^{-1}\equiv a^{p-2}\pmod{p}
$$</p>
<p>复杂度为 $\Theta(\log p)$。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="离线多点求值"
>
离线多点求值
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/inv/#离线多点求值" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 离线多点求值" href="#%e7%a6%bb%e7%ba%bf%e5%a4%9a%e7%82%b9%e6%b1%82%e5%80%bc">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="1sim-n-的逆元"
>
$1\sim n$ 的逆元
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/inv/#1sim-n-的逆元" class="gblog-post__anchor clip flex align-center" aria-label="Anchor $1\sim n$ 的逆元" href="#1sim-n-%e7%9a%84%e9%80%86%e5%85%83">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>首先,$1^{-1}\equiv 1\pmod p$;</p>
<p>其次,考虑 $i^{-1}$,令 $k=\left\lfloor\dfrac{p}{i}\right\rfloor,j=p\bmod i$,有 $p=ki+j$,则模意义下有 $ki+j\equiv 0\pmod p$;两边同时乘 $i^{-1}j^{-1}$:</p>
<p>$$
\begin{aligned}
kj^{-1}+i^{-1}&amp;\equiv 0&amp;\pmod p\\
i^{-1}&amp;\equiv -kj^{-1}&amp;\pmod p\\
i^{-1}&amp;\equiv -\left\lfloor\dfrac{p}{i}\right\rfloor(p\bmod i)^{-1}&amp;\pmod p\\
\end{aligned}
$$</p>
<p>递推即可,时间复杂度 $\Theta(n)$。</p>
<p>注意到 $0^{-1}$ 未定义,因此对任意 $i\mid p$,$i^{-1}$ 同样未定义。</p>
<p>注意到这种方法同样可以单点求逆元,现在已经证明其复杂度为 $O(\sqrt[3]{n\ln n})$,且 $\Omega(\dfrac{\ln n}{\ln\ln n})$,同时观察到可能是 $O(\log n)$ 的。</p>
<p><img
src="https://cdn.jsdelivr.net/gh/weilycoder/image_hosting@master/pmodn-5723a1e5b899a5d7.svg"
alt=""
/></p>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="任意-n-个数的逆元"
>
任意 $n$ 个数的逆元
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/inv/#任意-n-个数的逆元" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 任意 $n$ 个数的逆元" href="#%e4%bb%bb%e6%84%8f-n-%e4%b8%aa%e6%95%b0%e7%9a%84%e9%80%86%e5%85%83">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>给定序列 $a$,线性求序列中每个数的逆元。</p>
<ul>
<li>
<p>首先,求出 $a$ 的前缀积 $s$;</p>
</li>
<li>
<p>然后,使用快速幂或扩展欧几里得法求 $s_n$ 的逆元 $v_n$;</p>
</li>
<li>
<p>由 $v_{i}=v_{i+1}\cdot a_{i+1}$,可以递推出整个序列 $v$;</p>
</li>
<li>
<p>$a_{i}=s_{i}\cdot v_{i-1}$。</p>
</li>
</ul>
<p>时间复杂度 $\Theta(n+\log p)$。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="预处理后快速求解"
>
预处理后快速求解
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/inv/#预处理后快速求解" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 预处理后快速求解" href="#%e9%a2%84%e5%a4%84%e7%90%86%e5%90%8e%e5%bf%ab%e9%80%9f%e6%b1%82%e8%a7%a3">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>存在一种 $\Theta(p^{2/3})$ 预处理,在线 $\Theta(1)$ 查询的算法。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="farey-序列"
>
Farey 序列
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/inv/#farey-序列" class="gblog-post__anchor clip flex align-center" aria-label="Anchor Farey 序列" href="#farey-%e5%ba%8f%e5%88%97">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>第 $i$ 个 Farey 序列记作 $F_i$,是把分母小于等于 $i$ 的所有最简真分数从小到大排成的序列;特别地,序列的首项是 $\dfrac{0}{1}$,末项为 $\dfrac{1}{1}$。</p>
<p>$$
\begin{array}{lllllllllllll}
F_1=\bigg\{&amp;\dfrac{0}{1},&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;\dfrac{1}{1}&amp;\bigg\}\\
F_2=\bigg\{&amp;\dfrac{0}{1},&amp;&amp;&amp;&amp;&amp;\dfrac{1}{2},&amp;&amp;&amp;&amp;&amp;\dfrac{1}{1}&amp;\bigg\}\\
F_3=\bigg\{&amp;\dfrac{0}{1},&amp;&amp;&amp;\dfrac{1}{3},&amp;&amp;\dfrac{1}{2},&amp;&amp;\dfrac{2}{3},&amp;&amp;&amp;\dfrac{1}{1}&amp;\bigg\}\\
F_4=\bigg\{&amp;\dfrac{0}{1},&amp;&amp;\dfrac{1}{4},&amp;\dfrac{1}{3},&amp;&amp;\dfrac{1}{2},&amp;&amp;\dfrac{2}{3},&amp;\dfrac{3}{4},&amp;&amp;\dfrac{1}{1}&amp;\bigg\}\\
F_5=\bigg\{&amp;\dfrac{0}{1},&amp;\dfrac{1}{5},&amp;\dfrac{1}{4},&amp;\dfrac{1}{3},&amp;\dfrac{2}{5},&amp;\dfrac{1}{2},&amp;\dfrac{3}{5},&amp;\dfrac{2}{3},&amp;\dfrac{3}{4},&amp;\dfrac{4}{5},&amp;\dfrac{1}{1}&amp;\bigg\}
\end{array}
$$</p>
<!-- <div class="center-flex">
<img src="https://cdn.jsdelivr.net/gh/weilycoder/image_hosting@master/202408022026646-72ea53848d5932dc.png" style="margin:auto;"/>
</div> -->
<p>注意到 Farey 序列可以迭代生成,每次只需选择 $F_{n-1}$ 中每对相邻的 $\dfrac{a}{b},\dfrac{c}{d}$,满足 $b+d=n$,将 $\dfrac{a+c}{b+d}$ 放在中间即可。</p>
<p>同时,Farey 序列有性质:$bc-ad=1$。</p>
<p>我看到很多博客提到一个定理:</p>
<blockquote>
<p>对于任意整数 $n\le 2$ 和任意实数 $v\in [0,1]$,总能在 $F_{n−1}$ 找到 $\dfrac{x}{y}$ 满足 $\left|v−\dfrac{x}{y}\right|≤\dfrac{1}{yn}$ 。更强地,这个 $\dfrac{x}{y}$ 一定是 $v$ 序列中最接近 $v$ 的第一个分数。</p>
</blockquote>
<p>实际上,如果引入渐进分数,有另一个不等式:</p>
<blockquote>
<p>对实数 $x$ 与渐进分数 $\dfrac{p_k}{q_k}$ ,有
$$
\dfrac{1}{q_k(q_k+q_{k+1})}&lt;\left|x-\frac{p_k}{q_k}\right|&lt;\dfrac{1}{q_kq_{k+1}}
$$</p>
</blockquote>
<p>这个不等式更紧一些,因为 $q_{k+1}$ 不会比 $n$ 更小。</p>
<p>我们固定 $n$,对于 $a$ 令 $v=\dfrac{a}{p}$ 并找到 $\dfrac{x}{y}$ 满足 $\left|\dfrac{a}{p}-\dfrac{x}{y}\right|\le\dfrac{1}{yn}$。即 $|ay-px|\le\left\lfloor\dfrac{p}{n}\right\rfloor$。也就是说 $ay\equiv u\pmod p$,其中 $|u|\le \left\lfloor\dfrac{p}{n}\right\rfloor$。注意到 $a^{-1}=u^{-1}y\pmod p$,我们只需预处理 $O\left(\left\lfloor\dfrac{p}{n}\right\rfloor\right)$ 个数的逆元。</p>
<p>注意到序列中 $\left\lfloor\dfrac{xn^2}{y}\right\rfloor$ 互不相同,我们开一个长 $n^2$ 的 $0/1$ 数组记录是否有 $\left\lfloor\dfrac{xn^2}{y}\right\rfloor$ 等于 $i$。将序列求前缀和即可 $O(1)$ 查大于或小于 $\dfrac{x}{y}$ 的的第一个数。</p>
</content>
<category scheme="https://weilycoder.github.io/tags/math" term="math" label="math" />
</entry>
<entry>
<title>珂朵莉树</title>
<link href="https://weilycoder.github.io/post/odt/" rel="alternate" type="text/html" hreflang="en" />
<id>https://weilycoder.github.io/post/odt/</id>
<published>2024-06-24T15:45:44+08:00</published>
<updated>2024-06-24T15:45:44+08:00</updated>
<content type="html">
<p>珂朵莉树(Chtholly Tree),又名老司机树 ODT(Old Driver Tree)。起源自 <a
class="gblog-markdown__link"
href="https://codeforces.com/problemset/problem/896/C"
>CF896C</a>。</p>
<p>实际上,这种想法的本质是基于数据随机的「颜色段均摊」,而不是一种数据结构。</p>
<p>使用 <code>set</code> 实现的珂朵莉树的 <code>assign</code>、<code>add</code>、<code>sum</code> 操作复杂度为 $O(n\log\log n)$.</p>
<p>ODT 的核心思想是将值相同的区间合并为一个结点维护,只要有区间赋值的题目都可以用 ODT 骗分。</p>
<p>ODT 在随机数据上表现良好,但是不保证数据随机时,会被构造数据卡到 T 飞。</p>
<blockquote>
<p>内容基本来自 <a
class="gblog-markdown__link"
href="https://oi-wiki.org"
>OI-Wiki</a>。</p>
</blockquote>
<div class="flex align-center gblog-post__anchorwrap">
<h2 id="具体操作"
>
具体操作
</h2>
<a data-clipboard-text="https://weilycoder.github.io/post/odt/#具体操作" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 具体操作" href="#%e5%85%b7%e4%bd%93%e6%93%8d%e4%bd%9c">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="节点声明"
>
节点声明
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/odt/#节点声明" class="gblog-post__anchor clip flex align-center" aria-label="Anchor 节点声明" href="#%e8%8a%82%e7%82%b9%e5%a3%b0%e6%98%8e">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Note_t</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="k">mutable</span> <span class="kt">int</span> <span class="n">v</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="n">Node_t</span><span class="p">(</span><span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">il</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">ir</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="o">&amp;</span><span class="n">iv</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="o">:</span> <span class="n">l</span><span class="p">(</span><span class="n">il</span><span class="p">),</span> <span class="n">r</span><span class="p">(</span><span class="n">ir</span><span class="p">),</span> <span class="n">v</span><span class="p">(</span><span class="n">iv</span><span class="p">)</span> <span class="p">{}</span>
</span></span><span class="line"><span class="cl"> <span class="kt">bool</span> <span class="k">operator</span><span class="o">&lt;</span><span class="p">(</span><span class="k">const</span> <span class="n">Node_t</span> <span class="o">&amp;</span><span class="n">o</span><span class="p">)</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">l</span> <span class="o">&lt;</span> <span class="n">o</span><span class="p">.</span><span class="n">l</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
<p>其中,<code>v</code> 是附加数据。</p>
<blockquote class="gblog-hint note">
<div class="gblog-hint__title flex align-center"><svg class="gblog-icon gblog_info_outline"><use xlink:href="#gblog_info_outline"></use></svg>
<span>Note</span></div>
<div class="gblog-hint__text"><p><code>mutable</code> 用于突破 <code>const</code> 的限制,即使 <code>Note_t</code> 被设为常量,仍能改变 <code>v</code> 的值。</p>
<p>这意味着,我们可以直接修改已经插入 <code>set</code> 的元素的 <code>v</code> 值,而不用将该元素取出后重新加入 <code>set</code>。</p>
</div>
</blockquote>
<p>接下来,我们定义一个 <code>set</code> 存储节点。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="n">set</span><span class="o">&lt;</span><span class="n">Node_t</span><span class="o">&gt;</span> <span class="n">odt</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">typedef</span> <span class="n">set</span><span class="o">&lt;</span><span class="n">Node_t</span><span class="o">&gt;::</span><span class="n">iterator</span> <span class="n">iter</span><span class="p">;</span></span></span></code></pre></td></tr></table>
</div>
</div>
<p><code>iter</code> 用于简化代码,如果题目支持 C++11,也可以使用 <code>auto</code>。</p>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="split"
>
split
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/odt/#split" class="gblog-post__anchor clip flex align-center" aria-label="Anchor split" href="#split">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p><code>split</code> 是核心操作之一,用于将包含 $x$ 的区间 $[l,r]$ 分裂为 $[l,x)$ 和 $[x,r]$,并返回后者的迭代器。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="n">iter</span> <span class="nf">split</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="n">n</span><span class="p">)</span> <span class="k">return</span> <span class="n">odt</span><span class="p">.</span><span class="n">end</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"> <span class="k">auto</span> <span class="n">it</span> <span class="o">=</span> <span class="o">--</span><span class="n">odt</span><span class="p">.</span><span class="n">upper_bound</span><span class="p">(</span><span class="n">Node_t</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="n">it</span><span class="o">-&gt;</span><span class="n">l</span> <span class="o">==</span> <span class="n">x</span><span class="p">)</span> <span class="k">return</span> <span class="n">it</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="kt">int</span> <span class="n">l</span> <span class="o">=</span> <span class="n">it</span><span class="o">-&gt;</span><span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">it</span><span class="o">-&gt;</span><span class="n">r</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="n">it</span><span class="o">-&gt;</span><span class="n">v</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="n">odt</span><span class="p">.</span><span class="n">erase</span><span class="p">(</span><span class="n">it</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="n">odt</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">Node_t</span><span class="p">{</span><span class="n">l</span><span class="p">,</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">v</span><span class="p">});</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="n">odt</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">Node_t</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">v</span><span class="p">).</span><span class="n">first</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="assign"
>
assign
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/odt/#assign" class="gblog-post__anchor clip flex align-center" aria-label="Anchor assign" href="#assign">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p><code>assign</code> 是另一个核心操作,用于对区间赋值,可以降低节点数量,保证了 ODT 的时间复杂度。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">assign</span><span class="p">(</span><span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">,</span> <span class="kt">int</span> <span class="n">v</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">auto</span> <span class="n">itr</span> <span class="o">=</span> <span class="n">split</span><span class="p">(</span><span class="n">r</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="n">itl</span> <span class="o">=</span> <span class="n">split</span><span class="p">(</span><span class="n">l</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="n">odt</span><span class="p">.</span><span class="n">erase</span><span class="p">(</span><span class="n">itl</span><span class="p">,</span> <span class="n">itr</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="n">odt</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">Node_t</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">v</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
<blockquote class="gblog-hint important">
<div class="gblog-hint__title flex align-center"><svg class="gblog-icon gblog_error_outline"><use xlink:href="#gblog_error_outline"></use></svg>
<span>Important</span></div>
<div class="gblog-hint__text"><p>ODT 必须先 <code>split</code> 右端点,再 <code>split</code> 左端点,这和迭代器的失效有关。</p>
<p>不这样做,代码会随机 $\textcolor{purple}{\text{RE}}$。</p>
</div>
</blockquote>
<div class="flex align-center gblog-post__anchorwrap">
<h3 id="other"
>
other
</h3>
<a data-clipboard-text="https://weilycoder.github.io/post/odt/#other" class="gblog-post__anchor clip flex align-center" aria-label="Anchor other" href="#other">
<svg class="gblog-icon gblog_link"><use xlink:href="#gblog_link"></use></svg>
</a>
</div>
<p>参考代码如下:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-Cpp" data-lang="Cpp"><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">assign</span><span class="p">(</span><span class="kt">int</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">r</span><span class="p">,</span> <span class="kt">int</span> <span class="n">v</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">auto</span> <span class="n">itr</span> <span class="o">=</span> <span class="n">split</span><span class="p">(</span><span class="n">r</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="n">itl</span> <span class="o">=</span> <span class="n">split</span><span class="p">(</span><span class="n">l</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="k">for</span> <span class="p">(;</span> <span class="n">itl</span> <span class="o">!=</span> <span class="n">itr</span><span class="p">;</span> <span class="o">++</span><span class="n">itl</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="c1">// Perform Operations here
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
</content>
<category scheme="https://weilycoder.github.io/tags/ds" term="ds" label="ds" />
</entry>
<entry>
<title>小清新线段树</title>
<link href="https://weilycoder.github.io/post/fresh_seg/" rel="alternate" type="text/html" hreflang="en" />
<id>https://weilycoder.github.io/post/fresh_seg/</id>
<author>
<name>weily</name>
</author>
<published>2024-06-23T17:22:36+08:00</published>
<updated>2024-06-23T17:22:36+08:00</updated>
<content type="html">