-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.html
982 lines (962 loc) · 29.5 KB
/
main.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>reveal.js – The HTML Presentation Framework</title>
<meta name="description" content="A framework for easily creating beautiful presentations using HTML">
<meta name="author" content="Hakim El Hattab">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<section>
<h1>Data Structure Basic</h1>
</section>
<section>
<h1>Who am I?</h1>
<ul>
<li>eddy1021,eddy,艾迪,欸底,...</li>
<li>PECaveros</li>
</ul>
</section>
<section>
<h2>What you will learn?</h2>
<ul>
<li>堆疊 stack</li>
<li>佇列 queue</li>
<li>雙端佇列 deque</li>
<li>優先佇列 priority queue</li>
</ul>
</section>
<section>
<h2>And also</h2>
<ul>
<li>並查集 disjoint set</li>
<li>線段樹 segment tree</li>
<li>樹堆 treap</li>
<li>...</li>
</ul>
</section>
<section>
<h1>Let's fight!</h1>
</section>
</section>
<!-- stack -->
<section>
<section>
<h1>堆疊 Stack</h1>
</section>
<section>
<h2>What is stack?</h2>
<ul>
<li>後進先出, Last In First Out</li>
<li>支援push, pop</li>
</ul>
</section>
<section>
<img src="images/stack.png">
</section>
<section>
<h2>How to do so?</h2>
<pre><code class="cpp" data-trim contenteditable>
#include <stack>
#include <stdio.h>
using namespace std;
int main(){
stack<int> s;
s.push( 514 ); s.push( 514514 );
printf( "%d\n" , (int)s.size() ); // 2
printf( "%d\n" , s.top() ); // 514514
s.pop();
printf( "%d\n" , s.top() ); // 514
s.pop();
printf( "%s\n" , s.empty() ? "empty" : "not empty" );
// empty
}
</code></pre>
</section>
<section>
<h1>常見題型</h1>
<ul>
<li>一臉堆疊樣</li>
<li>維護凸包</li>
<li>維護單調性</li>
</ul>
</section>
<section>
<p>寬 $N$ 的直方圖,第 $i$ 單位寬的部分高 $h_i$,問面積最大的矩形?</p>
<img src="images/poj2559.png">
</section>
<section>
<h2>枚舉左右界</h2>
<p>最佳矩形高度爲 $\min\{ h_L, h_{L+1}, \ldots, h_R \}$</p>
</section>
<section>
<h2>最佳左右界</h2>
<p>往外擴到底!</p>
<img src="images/poj2559-1.png">
</section>
<section>
<h2>單調性</h2>
<p>若存在 $j$ < $i$ 且 $h_j$ > $h_i$,則 $i$ 以後 $h_j$ 無法成爲區間極值</p>
</section>
<section>
<h2>計算最佳左右界</h2>
<ol>
<li>考慮 $h_i$</li>
<li>堆疊頂端 $h_j \ge h_i$,則pop </li>
<li>若堆疊爲空,最佳左(右)界爲1,否則爲 $j+1$(堆疊頂端爲 $h_j$)</li>
<li>push $h_i$</li>
</ol>
</section>
<section>
<h2>計算最佳左右界</h2>
<img src="images/poj2559-2.png">
</section>
<section>
<pre><code class="cpp" data-trim contenteditable>
stack<int> S;
for( int i = 1 ; i <= n ; i ++ ){
while( S.size() && h[ S.top() ] >= h[ i ] ) S.pop();
L[ i ] = 1 + ( S.size() ? S.top() : 0 );
S.push( i );
}
while( S.size() ) S.pop();
for( int i = n ; i >= 1 ; i -- ){
while( S.size() && h[ S.top() ] >= h[ i ] ) S.pop();
R[ i ] = ( S.size() ? S.top() : n + 1 ) - 1;
S.push( i );
}
</code></pre>
</section>
</section>
<!-- queue -->
<section>
<section>
<h1>佇列 Queue</h1>
</section>
<section>
<h2>What is queue?</h2>
<ul>
<li>先進先出, First In First Out</li>
<li>支援push, pop</li>
</ul>
</section>
<section>
<img src="images/queue.png">
</section>
<section>
<h2>How to do so?</h2>
<pre><code class="cpp" data-trim contenteditable>
#include <queue>
#include <stdio.h>
using namespace std;
int main(){
queue<int> q;
q.push( 514 ); q.push( 514514 );
printf( "%d\n" , (int)q.size() ); // 2
printf( "%d\n" , q.front() ); // 514
q.pop();
printf( "%d\n" , q.front() ); // 514514
q.pop();
printf( "%s\n" , q.empty() ? "empty" : "not empty" );
// empty
}
</code></pre>
</section>
<section>
<h1>常見題型</h1>
<ul>
<li>一臉佇列樣</li>
<li>Breadth First Search(BFS)!</li>
<li> <strike>中國人排隊問題</strike> </li>
</ul>
</section>
<section>
<p>$R \times C$的地圖上,有障礙物、人(恰一個)、起火點(若干個),問人是否能在不受火災侵襲而逃出迷宮?</p>
<img src="images/uva11624.png">
</section>
<section>
<img src="images/uva11624-1.png">
</section>
<section>
<h2>火很多QQ</h2>
<p>要 BFS 很多次,複雜度 $O(R\times C \times R \times C)$</p>
</section>
<section>
<h1>全部火一起BFS!</h1>
<p>複雜度 $O(R\times C )$</p>
</section>
<section>
<pre><code class="cpp" data-trim contenteditable>
while( q.size() ){
PII tp = q.front(); q.pop();
int nr = tp.first;
int nc = tp.second;
for( int i = 0 ; i < 4 ; i ++ ){
int nxtr = nr + dr[ i ];
int nxtc = nc + dc[ i ];
if( !in( nxtr , nxtc ) )
ans = min( ans , dst[ nr ][ nc ] + 1 );
else if( m[ nxtr ][ nxtc ] != '#' &&
dst[ nr ][ nc ] + 1 < dst[ nxtr ][ nxtc ] ){
dst[ nxtr ][ nxtc ] = dst[ nr ][ nc ] + 1;
q.push( { nxtr , nxtc } );
}
}
}
</code></pre>
</section>
</section>
<!-- deque -->
<section>
<section>
<h1>雙端佇列 Deque</h1>
</section>
<section>
<h2>What is deque?</h2>
<ul>
<li>都進都出, Both In Both Out</li>
<li>支援兩端push, pop</li>
</ul>
</section>
<section>
<img src="images/deque.png">
</section>
<section>
<h2>How to do so?</h2>
<pre><code class="cpp" data-trim contenteditable>
#include <deque>
#include <stdio.h>
using namespace std;
int main(){
deque<int> dq;
dq.push_back( 514 ); // 514
dq.push_back( 514514 ); // 514 514514
dq.push_front( 514514514 ); // 514514514 514 514514
printf( "%d\n" , (int)dq.size() ); // 3
printf( "%d\n" , dq.front() ); // 514514514
printf( "%d\n" , dq.back() ); // 514514
dq.pop_front();
printf( "%d\n" , dq[ 1 ] ); // 514514
}
</code></pre>
<h3>STL 中 deque 支援random access!</h3>
</section>
<section>
<h1>常見題型</h1>
<ul>
<li>一臉得可樣</li>
<li>維護單調性與區間長度</li>
<li>DP斜率優化</li>
</ul>
</section>
<section>
<p>給長度 $N$ 的序列 $a$ 及正整數 $K$。求出所有 $b_i = max\{ a_{i-K+1}, a_{i-K+2}, \cdots, a_i \}$</p>
</section>
<section>
<p>當 i < j 且 $a_i$ < $a_j$,則 $a_i$ 就再也不可能是答案</p>
<p>同時維護長度!</p>
</section>
<section>
<pre><code class="cpp" data-trim contenteditable>
deque<int> dq;
for( int i = 1 ; i <= n ; i ++ ){
while( dq.size() && dq.front()<=i-k ) dq.pop_front();
while( dq.size() && a[dq.back()]<=a[i] ) dq.pop_back();
dq.push_back( i );
b[ i ] = a[ dq.front() ];
}
</code></pre>
</section>
</section>
<!-- priority queue -->
<section>
<section>
<h1>優先佇列</h1>
<h1>Priority Queue</h1>
</section>
<section>
<h2>What is priority queue?</h2>
<ul>
<li>VIP制</li>
<li>優先權越高,越早出來</li>
<li>支援push, pop</li>
</ul>
</section>
<section>
<img src="images/heap.png">
</section>
<section>
<h2>How to do so?</h2>
<pre><code class="cpp" data-trim contenteditable>
#include <queue>
#include <stdio.h>
using namespace std;
typedef priority_queue<int> Heap;
int main(){
Heap h;
h.push( 5 ); h.push( 1 ); h.push( 4 );
printf( "%d\n" , (int)h.size() ); // 3
printf( "%d\n" , h.top() ); // 5
h.pop();
printf( "%d\n" , h.top() ); // 4
h.push( 2 );
printf( "%d\n" , h.top() ); // 4
h.pop();
printf( "%d\n" , h.top() ); // 2
}
</code></pre>
<h3>STL 中優先權大者爲值較大者</h3>
</section>
<section>
<h2>自定義比較函式</h2>
<pre><code class="cpp" data-trim contenteditable>
struct cmp{
bool operator()( int a , int b ){
return a % 3 < b % 3;
}
};
int main(){
priority_queue<int, vector<int>, cmp> h;
h.push( 4 );
printf( "%d\n" , h.top() ); // 4, 4 % 3 == 1
h.push( 51 );
printf( "%d\n" , h.top() ); // 4, 51 % 3 == 0
h.push( 104 );
printf( "%d\n" , h.top() ); // 104, 104 % 3 == 2
}
</code></pre>
</section>
<section>
<h1>常見問題</h1>
<ul>
<li>Greedy的輔助工具</li>
<li>動態維護極值</li>
</ul>
</section>
<section>
<h2>非負權圖上單源最短路</h2>
<h3>Dijkstra 算法</h3>
</section>
<section>
<pre><code class="cpp" data-trim contenteditable>
vector<PII> e[ N ];
priority_queue<PII,vector<PII>,greater<PII> > heap;
void Dijkstra( int source ){
heap.push( { 0 , source } );
while( heap.size() ){
PII tp = heap.top(); heap.pop();
int tdst = tp.first , tnode = tp.second;
if( vst[ tnode ] ) continue;
dst[ tnode ] = tdst;
vst[ tnode ] = true;
for( size_t i = 0 ; i < e[ tnode ].size() ; i ++ ){
int nxt = e[ tnode ][ i ].first;
int nxtdst = e[ tnode ][ i ].second + dst[ tnode ];
if( vst[ nxt ] ) continue;
heap.push( { nxtdst , nxt } );
}
}
}
</code></pre>
</section>
<section>
<h1>這樣就滿足了嗎?</h1>
</section>
<section>
<h2>Black Magic</h2>
<pre><code class="cpp" data-trim contenteditable>
#include <bits/extc++.h>
#include <stdio.h>
using namespace __gnu_pbds;
typedef priority_queue<int> heap;
int main(){
heap h1 , h2;
h1.push( 1 ); h1.push( 3 );
h2.push( 2 ); h2.push( 4 );
h1.join( h2 );
printf( "%d\n" , (int)h1.size() ); // 4
printf( "%d\n" , (int)h2.size() ); // 0
printf( "%d\n" , h1.top() ); // 4
h1.pop();
}
</code></pre>
<h2>插入及合併$\Theta(1)$!</h2>
</section>
</section>
<!-- disjoint set -->
<section>
<section>
<h1>並查集</h1>
<h1>Disjoint Set</h1>
</section>
<section>
<h2>What is disjoint set?</h2>
<ul>
<li>維護兩兩互斥的集合</li>
<li>支援合併及查詢</li>
</ul>
</section>
<section>
<img src="images/djs.png">
</section>
<section>
<h2>How to do so?</h2>
<h3>沒有現成的可以用QQ</h3>
</section>
<section>
<h2>How to do so?</h2>
<h3>超好寫!</h3>
<pre><code class="cpp" data-trim contenteditable>
struct DisjointSet{
int p[ N ];
void Init( int n ){
for( int i = 1 ; i <= n ; i ++ ) p[ i ] = i;
}
int Find( int x ){
return x == p[ x ] ? x : Find( p[ x ] );
}
void Union( int x , int y ){
p[ Find( x ) ] = Find( y );
}
};
</code></pre>
</section>
<section>
<h1>Upgrade!! 路徑壓縮</h1>
<pre><code class="cpp" data-trim contenteditable>
int Find( int x ){
return x == p[ x ] ? x : p[ x ] = Find( p[ x ] );
}
</code></pre>
<h2>查詢複雜度$O(\lg N)$</h2>
</section>
<section>
<img src="images/djs-1.png">
</section>
<section>
<h1>Upgrade again!! Union by rank</h1>
<h2>啓發式合併的思想</h2>
<h2>查詢複雜度$O(\lg N)$</h2>
</section>
<section>
<pre><code class="cpp" data-trim contenteditable>
int p[ N ] , sz[ N ];
void Init( int n ){
for( int i = 1 ; i <= n ; i ++ )
p[ i ] = i, sz[ i ] = 1;
}
void Union( int x , int y ){
x = Find( x ); y = Find( y );
if( x == y ) return;
p[ x ] = y;
sz[ y ] += sz[ x ];
}
</code></pre>
</section>
<section>
<h1>雙管齊下!</h1>
<h2>操作均攤複雜度$O(\alpha(n))$</h2>
</section>
<section>
<h1>常見問題</h1>
<ul>
<li>連通塊</li>
<li>等價性質</li>
</ul>
</section>
<section>
<p>無向圖 $G$,$N$ 個點、$M$ 條邊,破壞 $Q$ 條邊,問每破壞一條邊後連通塊數?</p>
<img src="images/ural1671.png">
</section>
<section>
<h2>剛剛好像沒有教過將並查集分裂...</h2>
</section>
<section>
<h1>時光倒流!</h1>
<p>破壞 $\rightarrow$ 建造</p>
</section>
<section>
<p>三類動物,$A$ 吃 $B$、$B$ 吃 $C$、$C$ 吃 $A$。給 $N$ 個動物,$K$ 筆資訊包含:</p>
<ul>
<li>$X$ 和 $Y$ 是同類</li>
<li>$X$ 吃 $Y$</li>
</ul>
<p>問有多少條資訊錯誤?</p>
</section>
<section>
<h2>連通塊?</h2>
<h3>$X$ 吃 $Y$ 是同一個集合?</h3>
</section>
<section>
<h2>維護不互斥的資訊!</h2>
<h3>資訊:$X_i$,$X$ 屬於第 $i$ 類</h3>
</section>
<section>
<h2> $X$ 和 $Y$ 同類</h2>
<ol>
<li>檢查是否有 $X_A$ 和 $Y_B,Y_C$ 同類,...</li>
<li>將 $X_A,Y_A$、$X_B,Y_B$、$X_C,Y_C$ 合併爲一個並查集</li>
</ol>
</section>
<section>
<h2> $X$ 吃 $Y$</h2>
<ol>
<li>檢查是否有 $X_A$ 和 $Y_A,Y_C$ 同類,...</li>
<li>將 $X_A,Y_B$、$X_B,Y_C$、$X_C,Y_A$ 合併爲一個並查集</li>
</ol>
</section>
<section>
<pre><code class="cpp" data-trim contenteditable>
if( type == 1 ){
if( djs.Find( trans( x , 0 ) ) == djs.Find( trans( y , 1 ) ) ||
djs.Find( trans( x , 0 ) ) == djs.Find( trans( y , 2 ) ) ){
fake ++; continue;
}
for( int i = 0 ; i < 3 ; i ++ )
djs.Union( trans( x , i ) , trans( y , i ) );
}else{
if( djs.Find( trans( x , 0 ) ) == djs.Find( trans( y , 0 ) ) ||
djs.Find( trans( x , 0 ) ) == djs.Find( trans( y , 2 ) ) ){
fake ++; continue;
}
for( int i = 0 ; i < 3 ; i ++ )
djs.Union( trans( x , i ) , trans( y , ( i + 1 ) % 3 ) );
}
</code></pre>
</section>
<section>
<h1>這樣就滿足了嗎?</h1>
<h3>(again)</h3>
</section>
<section>
<h1>Connected?</h1>
<p>給你 $N$ 個點,$Q$ 筆操作:加邊或將最近 $k$ 條邊拔除,每筆操作完輸出連通塊個數</p>
</section>
<section>
<h1>Connected?</h1>
<ul>
<li>可持久化 Disjoint Set?</li>
<li>倒著做?</li>
</ul>
</section>
<section>
<h1>Undo disjoint set!</h1>
</section>
<section>
<h2>Undo disjoint set!</h2>
<p>Find, Union都是一些assign操作!</p>
</section>
<section>
<h2>Undo disjoint set!</h2>
<pre><code class="cpp" data-trim contenteditable>
vector< pair<int*,int> > h; vector<int> sp;
void assign( int *k, int v ){ h.push_back( {k, *k} ); *k = v; }
void save(){ sp.push_back(h.size()); }
void undo(){
int last=sp.back(); sp.pop_back();
while( h.size()!=last ){
auto x=h.back(); h.pop_back();
*x.first = x.second;
}
}
void Union( int x , int y ){
x = Find( x ); y = Find( y );
if( x == y ) return;
assign( &sz[ x ] , sz[ x ] + sz[ y ] );
assign( &p[ y ] , x );
}
</code></pre>
</section>
</section>
<!-- segment tree -->
<section>
<section>
<h1>線段樹</h1>
<h1>Segment Treee</h1>
</section>
<section>
<p>給你長度 $N$ 的序列 $v$,$Q$ 筆操作:</p>
<ul>
<li>將 $v_i$ 加上 $d$</li>
<li>詢問區間 $[l,r]$ 的總和</li>
</ul>
</section>
<section>
<h2>What is segment tree?</h2>
<ul>
<li>二元樹狀結構</li>
<li>處理區間問題</li>
</ul>
</section>
<section>
<img src="images/st.png">
</section>
<section>
<h2>實作方式-node</h2>
<pre><code class="cpp" data-trim contenteditable>
struct Node{
int val;
Node *lc , *rc;
Node(){ val = 0; lc = rc = NULL; }
void pull(){
val = lc->val + rc->val;
}
};
</code></pre>
</section>
<section>
<h2>初始化線段樹 四部曲</h2>
<ol>
<li>葉節點直接得到資訊</li>
<li>建立左子樹</li>
<li>建立右子樹</li>
<li>pull</li>
</ol>
</section>
<section>
<h2>實作方式-build</h2>
<pre><code class="cpp" data-trim contenteditable>
Node* build( int L , int R ){
Node *node = new Node();
if( L == R ){
node->val = v[ L ];
return node;
}
int mid = ( L + R ) >> 1;
node->lc = build( L , mid );
node->rc = build( mid + 1 , R );
node->pull();
return node;
}
</code></pre>
</section>
<section>
<h2>修改線段樹 三部曲</h2>
<ol>
<li>葉節點直接修改資訊</li>
<li>修改包含 $i$ 的子樹</li>
<li>pull</li>
</ol>
</section>
<section>
<h2>實作方式-modify</h2>
<pre><code class="cpp" data-trim contenteditable>
void modify( Node* node , int L , int R , int i , int d ){
if( L == R ){
assert( L == i );
node->val += d;
return;
}
int mid = ( L + R ) >> 1;
if( i <= mid ) modify( node->lc , L , mid , i , d );
else modify( node->rc , mid + 1 , R , i , d );
node->pull();
}
</code></pre>
</section>
<section>
<img src="images/st-1.png">
</section>
<section>
<h2>詢問線段樹 五步驟</h2>
<ol>
<li>區間不相交,直接回傳</li>
<li>區間包含,回傳節點上資訊</li>
<li>收集左子樹資訊</li>
<li>收集右子樹資訊</li>
<li>結合兩子樹資訊</li>
</ol>
</section>
<section>
<h2>實作方式-query</h2>
<pre><code class="cpp" data-trim contenteditable>
int query( Node* node , int L , int R , int ql , int qr ){
if( ql > R || qr < L ) return 0;
if( ql <= L && R <= qr ) return node->val;
int mid = ( L + R ) >> 1;
return query( node->lc , L , mid , ql , qr ) +
query( node->rc , mid + 1 , R , ql , qr );
}
</code></pre>
</section>
<section>
<img src="images/st-2.png">
</section>
<section>
<p>給你長度 $N$ 的序列 $v$,$Q$ 筆操作:</p>
<ul>
<li>將區間 $[l,r]$ 都加上 $d$</li>
<li>詢問區間 $[l,r]$ 的總和</li>
</ul>
</section>
<section>
<h2>擴充包-線段樹懶標記(Lazy tag)</h2>
<img src="images/fun.jpe">
</section>
<section>
<img src="images/st-3.png">
</section>
<section>
<img src="images/st-4.png">
</section>
<section>
<img src="images/st-5.png">
</section>
<section>
<img src="images/st-6.png">
</section>
<section>
<h2>Push!</h2>
<pre><code class="cpp" data-trim contenteditable>
void push( Node* node , int L , int R ){
if( !node->tag ) return;
if( L != R ){ // check leaf
int mid = ( L + R ) >> 1;
node->lc->tag += node->tag;
node->rc->tag += node->tag;
node->lc->val += node->tag * ( mid - L + 1 );
node->rc->val += node->tag * ( R - mid );
}
node->tag = 0;
}
</code></pre>
</section>
<section>
<h2>線段樹懶標記</h2>
<ul>
<li>修改、詢問時,遇到標記即push!</li>
</ul>
</section>
<section>
<h2>牛刀小試</h2>
<p>$N \times N$($N \le 10^9$) 矩陣初始值爲零,$Q$ 筆操作:</p>
<ul>
<li> 將 $(x,y)$ 座標加上 d </li>
<li> 詢問 $(x_1,y_1)$ 到 $(x_2,y_2) 子矩陣的和。$ </li>
</ul>
</section>
<section>
<h2>離散化?</h2>
<h2>空間爆炸</h2>
</section>
<section>
<h2>擴充包2-copy on write</h2>
</section>
<section>
<h2>用到的點很少,那要用再開!</h2>
<img src="images/stcow.png">
</section>
<section>
<h2>實作方式</h2>
<p>要往下走時先確認有沒有該節點(想想 Trie!)</p>
<p>pull 時確認是否有該節點</p>
<pre><code class="cpp" data-trim contenteditable>
int Val2( Node2* node ){ return node ? node->val : 0; }
void pull2( Node2* node ){
node->val = Val2( node->lc ) + Val2( node->rc );
}
</code></pre>
</section>
<section>
<h2>牛刀小試2</h2>
<p>給長度 $N 的序列 $v$,$Q$ 筆操作:</p>
<ul>
<li> 將 $v_i$ 加上 d </li>
<li> 詢問第 $k$ 筆操作後,區間 $[l,r]$ 的總和 </li>
</ul>
<h3>強迫在線</h3>
</section>
<section>
<h2>擴充包3-可持久化</h2>
</section>
<section>
<h2>一個版本長一棵!</h2>
<img src="images/stper.png">
</section>
<section>
<h2>實作方式</h2>
<p>共同指向共用的節點</p>
<p>修改時新增自己用到的節點</p>
<p>詢問完全一樣!</p>
</section>
<section>
<h2>三刀並用</h2>
<ul>
<li>區間修改 $\rightarrow$ 懶標記</li>
<li>大範圍區間 $\rightarrow$ Copy on write</li>
<li>版本控制 $\rightarrow$ 可持久化</li>
</ul>
</section>
<section>
<h3>區間反轉、區間搬移、區間複製、$\cdots$</h3>
</section>
</section>
<!-- treap -->
<section>
<section>
<h1>樹堆 Treap</h1>
<h3>邁向序列操作大師之路!</h3>
</section>
<section>
<h2>What is Treap?</h2>
<ul>
<li>Tree+Heap</li>
<li>滿足樹性質</li>
<li>滿足堆性質</li>
</ul>
</section>
<section>
<h2>樹性質?</h2>
<ul>
<li>Key $\ge$ 左子節點的 Key</li>
<li>Key $\le$ 右子節點的 Key</li>
</ul>
<img src="images/treap.png">
</section>
<section>
<h2>堆性質?</h2>
<ul>
<li>Pri $\ge$ 左子節點的 Pri</li>
<li>Pri $\ge$ 右子節點的 Pri</li>
</ul>
<img src="images/treap-1.png">
</section>
<section>
<h2>實作方式-Node</h2>
<pre><code class="cpp" data-trim contenteditable>
struct Treap{
Treap *l , *r;
int pri , key , val;
Treap( int _val , int _key ) :
val( _val ) , key( _key ), l( 0 ), r( 0 ), pri( rand() ) {}
};
</code></pre>
</section>
<section>
<h2>實作方式-merge</h2>
<h3>合併三部曲</h3>
<ul>
<li>樹爲空?</li>
<li>誰當根?</li>
<li>要做誰?</li>
</ul>
</section>
<section>
<h2>實作方式-merge</h2>
<pre><code class="cpp" data-trim contenteditable>
Treap* merge( Treap* a , Treap* b ){
if( !a || !b ) return a ? a : b;
if( a->pri > b->pri ){
a->r = merge( a->r , b );
return a;
}else{
b->l = merge( a , b->l );
return b;
}
}
</code></pre>
</section>
<section>
<h2>實作方式-split</h2>
<h3>分裂三部曲</h3>
<ul>
<li>樹爲空?</li>
<li>根送誰?</li>
<li>要做誰?</li>
</ul>
</section>
<section>
<h2>實作方式-split</h2>
<pre><code class="cpp" data-trim contenteditable>
void split( Treap* t , int k , Treap *&a , Treap *&b ){
if( !t ) a = b = NULL;
else if( t->key <= k ){
a = t;
split( t->r , k , a->r , b );
}else{
b = t;
split( t->l , k , a , b->l );
}
}
</code></pre>
</section>
<section>
<h2>沒有其他操作?</h2>
</section>
<section>
<h2>插入操作-Insert</h2>
<pre><code class="cpp" data-trim contenteditable>
Treap* insert( Treap* t , int k ){
Treap *tl , *tr;
split( t , k , tl , tr );
return merge( tl , merge( new Treap( k ) , tr ) );
}
</code></pre>
</section>
<section>
<h2>刪除操作-remove</h2>
<pre><code class="cpp" data-trim contenteditable>
Treap* remove( Treap* t , int k ){
Treap *tl , *tr;
split( t , k - 1 , tl , t );
split( t , k , t , tr );
return merge( tl , tr );
}
</code></pre>
</section>
<section>
<h1>去征服所有序列題吧!</h1>
</section>
</section>
<section>
<h2>Good Luck & Have Fun!</h2>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/math/math.js', async: true },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>