zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
U
Z±d+*ã@s¬dZddlmZddlZddlZddlmZmZmZddl    m
Z
m Z m Z ddl mZddlmZmZmZmZmZmZmZmZddlmZzdd    lmZmZWn$ek
rÀdd    lmZmZYnXdd
lm Z dd lm!Z!e!d kr2dd l    m"Z#ddl    m$Z%zddl&m'Z'Wn"ek
r.ddl(m'Z'YnXn@ddlm)Z)zddl*m'Z'Wn"ek
rpddl+m'Z'YnXddd„Z,Gdd„deƒZ-dd„Z.Gdd„de-ƒZ/e/Z0dS)abSorted List
==============
 
:doc:`Sorted Containers<index>` is an Apache2 licensed Python sorted
collections library, written in pure-Python, and fast as C-extensions. The
:doc:`introduction<introduction>` is the best way to get started.
 
Sorted list implementations:
 
.. currentmodule:: sortedcontainers
 
* :class:`SortedList`
* :class:`SortedKeyList`
 
é)Úprint_functionN)Ú bisect_leftÚ bisect_rightÚinsort)ÚchainÚrepeatÚstarmap)Úlog)ÚaddÚeqÚneÚgtÚgeÚltÚleÚiadd)Údedent)ÚSequenceÚMutableSequence)Úwraps)Ú
hexversioni)Úimap)Úizip)Ú    get_ident)Úreduceú...cs‡fdd„}|S)zHDecorator to make a repr function return fillvalue for a recursive call.cs"tƒ‰tˆƒ‡‡‡fdd„ƒ}|S)Nc    sBt|ƒtƒf}|ˆkrˆSˆ |¡z ˆ|ƒ}W5ˆ |¡X|S©N)Úidrr
Údiscard)ÚselfÚkeyÚresult)Ú    fillvalueÚ repr_runningÚ user_function©úRd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sortedcontainers/sortedlist.pyÚwrapper@s
  z<recursive_repr.<locals>.decorating_function.<locals>.wrapper)Úsetr)r$r'©r")r#r$r&Údecorating_function=s z+recursive_repr.<locals>.decorating_functionr%)r"r*r%r)r&Úrecursive_repr7s r+c@sêeZdZdZdZdgdd„Zdhdd„Zedd    „ƒZd
d „Z    d d „Z
e
Z dd„Z dd„Z dd„ZeZdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„ZeZd&d'„Zd(d)„Zd*d+„Zd,d-„Zdid/d0„Zd1d2„Zdjd4d5„Z d6d7„Z!d8d9„Z"d:d;„Z#e#Z$e#Z%d<d=„Z&d>d?„Z'e'Z(d@dA„Z)dBdC„Z*dDdE„Z+dkdGdH„Z,dldIdJ„Z-dKdL„Z.e.Z/dMdN„Z0dOdP„Z1e1Z2dQdR„Z3dSdT„Z4e4e5dUdVƒZ6e4e7dWdXƒZ8e4e9dYdZƒZ:e4e;d[d\ƒZ<e4e=d]d^ƒZ>e4e?d_d`ƒZ@eAe4ƒZ4dadb„ZBeCƒdcdd„ƒZDdedf„ZEdS)mÚ
SortedLista½Sorted list is a sorted mutable sequence.
 
    Sorted list values are maintained in sorted order.
 
    Sorted list values must be comparable. The total ordering of values must
    not change while they are stored in the sorted list.
 
    Methods for adding values:
 
    * :func:`SortedList.add`
    * :func:`SortedList.update`
    * :func:`SortedList.__add__`
    * :func:`SortedList.__iadd__`
    * :func:`SortedList.__mul__`
    * :func:`SortedList.__imul__`
 
    Methods for removing values:
 
    * :func:`SortedList.clear`
    * :func:`SortedList.discard`
    * :func:`SortedList.remove`
    * :func:`SortedList.pop`
    * :func:`SortedList.__delitem__`
 
    Methods for looking up values:
 
    * :func:`SortedList.bisect_left`
    * :func:`SortedList.bisect_right`
    * :func:`SortedList.count`
    * :func:`SortedList.index`
    * :func:`SortedList.__contains__`
    * :func:`SortedList.__getitem__`
 
    Methods for iterating values:
 
    * :func:`SortedList.irange`
    * :func:`SortedList.islice`
    * :func:`SortedList.__iter__`
    * :func:`SortedList.__reversed__`
 
    Methods for miscellany:
 
    * :func:`SortedList.copy`
    * :func:`SortedList.__len__`
    * :func:`SortedList.__repr__`
    * :func:`SortedList._check`
    * :func:`SortedList._reset`
 
    Sorted lists use lexicographical ordering semantics when compared to other
    sequences.
 
    Some methods of mutable sequences are not supported and will raise
    not-implemented error.
 
    ièNcCsH|dks t‚d|_|j|_g|_g|_g|_d|_|dk    rD| |¡dS)a¢Initialize sorted list instance.
 
        Optional `iterable` argument provides an initial iterable of values to
        initialize the sorted list.
 
        Runtime complexity: `O(n*log(n))`
 
        >>> sl = SortedList()
        >>> sl
        SortedList([])
        >>> sl = SortedList([3, 1, 2, 5, 4])
        >>> sl
        SortedList([1, 2, 3, 4, 5])
 
        :param iterable: initial values (optional)
 
        Nr)    ÚAssertionErrorÚ_lenÚDEFAULT_LOAD_FACTORÚ_loadÚ_listsÚ_maxesÚ_indexÚ_offsetÚ_update©rÚiterabler r%r%r&Ú__init__s zSortedList.__init__cCs0|dkrt |¡S|tkr$t t¡Stdƒ‚dS)aJCreate new sorted list or sorted-key list instance.
 
        Optional `key`-function argument will return an instance of subtype
        :class:`SortedKeyList`.
 
        >>> sl = SortedList()
        >>> isinstance(sl, SortedList)
        True
        >>> sl = SortedList(key=lambda x: -x)
        >>> isinstance(sl, SortedList)
        True
        >>> isinstance(sl, SortedKeyList)
        True
 
        :param iterable: initial values (optional)
        :param key: function used to extract comparison key (optional)
        :return: sorted list or sorted-key list instance
 
        Nz&inherit SortedKeyList for key argument)ÚobjectÚ__new__r,Ú SortedKeyListÚ    TypeError©Úclsr7r r%r%r&r:®s
 
 
zSortedList.__new__cCsdS)z‰Function used to extract comparison key from values.
 
        Sorted list compares values directly so the key function is none.
 
        Nr%©rr%r%r&r ÌszSortedList.keycCs*tt|jgƒ}| ¡||_| |¡dS)a‰Reset sorted list load factor.
 
        The `load` specifies the load-factor of the list. The default load
        factor of 1000 works well for lists from tens to tens-of-millions of
        values. Good practice is to use a value that is the cube root of the
        list size. With billions of elements, the best load factor depends on
        your usage. It's best to leave the load factor at the default until you
        start benchmarking.
 
        See :doc:`implementation` and :doc:`performance-scale` for more
        information.
 
        Runtime complexity: `O(n)`
 
        :param int load: load-factor for sorted list sublists
 
        N)rrr1Ú_clearr0r5)rÚloadÚvaluesr%r%r&Ú_resetÖszSortedList._resetcCs4d|_|jdd…=|jdd…=|jdd…=d|_dS)zQRemove all values from sorted list.
 
        Runtime complexity: `O(n)`
 
        rN)r.r1r2r3r4r?r%r%r&Úclearîs
   zSortedList.clearcCsˆ|j}|j}|r`t||ƒ}|t|ƒkrF|d8}|| |¡|||<nt|||ƒ| |¡n| |g¡| |¡|jd7_dS)aAdd `value` to sorted list.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList()
        >>> sl.add(3)
        >>> sl.add(1)
        >>> sl.add(2)
        >>> sl
        SortedList([1, 2, 3])
 
        :param value: value to add to sorted list
 
        éN)r1r2rÚlenÚappendrÚ_expandr.)rÚvaluer1r2Úposr%r%r&r
ýs
 
 
zSortedList.addc    CsÊ|j}|j}|j}t||ƒ|d>kr†|j}||}||d…}||d…=|d||<| |d|¡| |d|d¡|dd…=n@|rÆ|j|}|r¶||d7<|dd?}q”|dd7<dS©a>Split sublists with length greater than double the load-factor.
 
        Updates the index when the sublist length is less than double the load
        level. This requires incrementing the nodes in a traversal from the
        leaf node to the root. For an example traversal see
        ``SortedList._loc``.
 
        rENéÿÿÿÿr)r0r1r3rFr2Úinsertr4)    rrJr0r1r3r2Ú
_lists_posÚhalfÚchildr%r%r&rH!s$     
 
zSortedList._expandcsÈ|j}|j}t|ƒ‰|rntˆƒd|jkrR| ˆ¡tt|gƒ‰ˆ ¡|     ¡n|j
}ˆD] }||ƒq\dS|j ‰|  ‡‡fdd„t dtˆƒˆƒDƒ¡|  dd„|Dƒ¡tˆƒ|_|jdd…=dS)aUpdate sorted list by adding all values from `iterable`.
 
        Runtime complexity: `O(k*log(n))` -- approximate.
 
        >>> sl = SortedList()
        >>> sl.update([3, 1, 2])
        >>> sl
        SortedList([1, 2, 3])
 
        :param iterable: iterable of values to add
 
        éNc3s|]}ˆ||ˆ…VqdSrr%©Ú.0rJ©r0rBr%r&Ú    <genexpr>asÿz$SortedList.update.<locals>.<genexpr>rcss|]}|dVqdS©rLNr%©rSÚsublistr%r%r&rUcs)r1r2ÚsortedrFr.rGrrÚsortr@r
r0ÚextendÚranger3)rr7r1r2Ú_addÚvalr%rTr&ÚupdateCs( 
 
 
ÿ
zSortedList.updatecCsL|j}|sdSt||ƒ}|t|ƒkr(dS|j}t|||ƒ}||||kS)aZReturn true if `value` is an element of the sorted list.
 
        ``sl.__contains__(value)`` <==> ``value in sl``
 
        Runtime complexity: `O(log(n))`
 
        >>> sl = SortedList([1, 2, 3, 4, 5])
        >>> 3 in sl
        True
 
        :param value: search for value in sorted list
        :return: true if `value` in sorted list
 
        F)r2rrFr1©rrIr2rJr1Úidxr%r%r&Ú __contains__js
 zSortedList.__contains__cCs\|j}|sdSt||ƒ}|t|ƒkr(dS|j}t|||ƒ}||||krX| ||¡dS)aoRemove `value` from sorted list if it is a member.
 
        If `value` is not a member, do nothing.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList([1, 2, 3, 4, 5])
        >>> sl.discard(5)
        >>> sl.discard(0)
        >>> sl == [1, 2, 3, 4]
        True
 
        :param value: `value` to discard from sorted list
 
        N)r2rrFr1Ú_deleter`r%r%r&r‰s
 zSortedList.discardcCs€|j}|std |¡ƒ‚t||ƒ}|t|ƒkr<td |¡ƒ‚|j}t|||ƒ}||||krn| ||¡ntd |¡ƒ‚dS)aRemove `value` from sorted list; `value` must be a member.
 
        If `value` is not a member, raise ValueError.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList([1, 2, 3, 4, 5])
        >>> sl.remove(5)
        >>> sl == [1, 2, 3, 4]
        True
        >>> sl.remove(0)
        Traceback (most recent call last):
          ...
        ValueError: 0 not in list
 
        :param value: `value` to remove from sorted list
        :raises ValueError: if `value` is not in sorted list
 
        ú{0!r} not in listN)r2Ú
ValueErrorÚformatrrFr1rcr`r%r%r&Úremoveªs
 zSortedList.removec
Cs(|j}|j}|j}||}||=|jd8_t|ƒ}||jd?kr–|d||<|r”|j|}|dkr„||d8<|dd?}q^|dd8<nŽt|ƒdkrú|s®|d7}|d}    ||     ||¡||    d||    <||=||=|dd…=| |    ¡n*|r|d||<n||=||=|dd…=dS©a¯Delete value at the given `(pos, idx)`.
 
        Combines lists that are less than half the load level.
 
        Updates the index when the sublist length is more than half the load
        level. This requires decrementing the nodes in a traversal from the
        leaf node to the root. For an example traversal see
        ``SortedList._loc``.
 
        :param int pos: lists index
        :param int idx: sublist index
 
        rErLrN)    r1r2r3r.rFr0r4r[rH)
rrJrar1r2r3rNZ len_lists_posrPÚprevr%r%r&rcÑs< 
 
 zSortedList._deletecCsZ|s|S|j}|s| ¡d}||j7}|rR|d@sD|||d7}|dd?}q(||S)aŒConvert an index pair (lists index, sublist index) into a single
        index number that corresponds to the position of the value in the
        sorted list.
 
        Many queries require the index be built. Details of the index are
        described in ``SortedList._build_index``.
 
        Indexing requires traversing the tree from a leaf node to the root. The
        parent of each node is easily computable at ``(pos - 1) // 2``.
 
        Left-child nodes are always at odd indices and right-child nodes are
        always at even indices.
 
        When traversing up from a right-child node, increment the total by the
        left-child node.
 
        The final index is the sum from traversal and the index in the sublist.
 
        For example, using the index from ``SortedList._build_index``::
 
            _index = 14 5 9 3 2 4 5
            _offset = 3
 
        Tree::
 
                 14
              5      9
            3   2  4   5
 
        Converting an index pair (2, 3) into a single index involves iterating
        like so:
 
        1. Starting at the leaf node: offset + alpha = 3 + 2 = 5. We identify
           the node as a left-child node. At such nodes, we simply traverse to
           the parent.
 
        2. At node 9, position 2, we recognize the node as a right-child node
           and accumulate the left-child in our total. Total is now 5 and we
           traverse to the parent at position 0.
 
        3. Iteration ends at the root.
 
        The index is then the sum of the total and sublist index: 5 + 3 = 8.
 
        :param int pos: lists index
        :param int idx: sublist index
        :return: index in sorted list
 
        rrE)r3Ú _build_indexr4)rrJrar3Útotalr%r%r&Ú_locs2
zSortedList._loccCsê|dkrRt|jdƒ}| |kr6t|jƒd||fS||j7}|dkrdtdƒ‚n||jkrdtdƒ‚|t|jdƒkr~d|fS|j}|s| ¡d}d}t|ƒ}||krÜ||}||kr¾|}n||8}|d}|d>d}q ||j|fS)aiConvert an index into an index pair (lists index, sublist index)
        that can be used to access the corresponding lists position.
 
        Many queries require the index be built. Details of the index are
        described in ``SortedList._build_index``.
 
        Indexing requires traversing the tree to a leaf node. Each node has two
        children which are easily computable. Given an index, pos, the
        left-child is at ``pos * 2 + 1`` and the right-child is at ``pos * 2 +
        2``.
 
        When the index is less than the left-child, traversal moves to the
        left sub-tree. Otherwise, the index is decremented by the left-child
        and traversal moves to the right sub-tree.
 
        At a child node, the indexing pair is computed from the relative
        position of the child node as compared with the offset and the remaining
        index.
 
        For example, using the index from ``SortedList._build_index``::
 
            _index = 14 5 9 3 2 4 5
            _offset = 3
 
        Tree::
 
                 14
              5      9
            3   2  4   5
 
        Indexing position 8 involves iterating like so:
 
        1. Starting at the root, position 0, 8 is compared with the left-child
           node (5) which it is greater than. When greater the index is
           decremented and the position is updated to the right child node.
 
        2. At node 9 with index 3, we again compare the index to the left-child
           node with value 4. Because the index is the less than the left-child
           node, we simply traverse to the left.
 
        3. At node 4 with index 3, we recognize that we are at a leaf node and
           stop iterating.
 
        4. To compute the sublist index, we subtract the offset from the index
           of the leaf node: 5 - 3 = 2. To compute the index in the sublist, we
           simply use the index remaining from iteration. In this case, 3.
 
        The final index pair from our example is (2, 3) which corresponds to
        index 8 in the sorted list.
 
        :param int idx: index in sorted list
        :return: (lists index, sublist index) pair
 
        rrLrEúlist index out of range)rFr1r.Ú
IndexErrorr3rjr4)rraZlast_lenr3rJrPÚ    len_indexZ index_childr%r%r&Ú_posYs27
 
 
 
zSortedList._poscCsBttt|jƒƒ}t|ƒdkr4||jdd…<d|_dSt|ƒ}t|ƒ}tttt    ||ƒƒƒ}t|ƒd@rr| 
|d¡t|ƒdkrš|||jdd…<d|_dSdt t t|ƒddƒƒd}|  td|t|ƒƒ¡||g}t|dƒdkrt|dƒ}t|ƒ}tttt    ||ƒƒƒ}| 
|¡qØttt|ƒ|jƒ|dd|_dS)aBuild a positional index for indexing the sorted list.
 
        Indexes are represented as binary trees in a dense array notation
        similar to a binary heap.
 
        For example, given a lists representation storing integers::
 
            0: [1, 2, 3]
            1: [4, 5]
            2: [6, 7, 8, 9]
            3: [10, 11, 12, 13, 14]
 
        The first transformation maps the sub-lists by their length. The
        first row of the index is the length of the sub-lists::
 
            0: [3, 2, 4, 5]
 
        Each row after that is the sum of consecutive pairs of the previous
        row::
 
            1: [5, 9]
            2: [14]
 
        Finally, the index is built by concatenating these lists together::
 
            _index = [14, 5, 9, 3, 2, 4, 5]
 
        An offset storing the start of the first row is also stored::
 
            _offset = 3
 
        When built, the index can be used for efficient indexing into the list.
        See the comment and notes on ``SortedList._pos`` for details.
 
        rENrrLé)ÚlistÚmaprFr1r3r4Úiterrr
ÚziprGÚintr    r[rrrÚreversed)rZrow0ÚheadÚtailZrow1ÚsizeÚtreeÚrowr%r%r&rj·s0$     zSortedList._build_indexc Cst|tƒrä| |j¡\}}}|dkr˜||kr˜|dkrF||jkrF| ¡S|jd||kr˜| td|ƒ¡}||jkr†|| t|dƒ¡7}| ¡| |¡St|||ƒ}|dkr´t|ƒ}|j    |j
}}|D]}||ƒ\}    }
||    |
ƒqÆn|     |¡\}    }
|    |
¡dS)aáRemove value at `index` from sorted list.
 
        ``sl.__delitem__(index)`` <==> ``del sl[index]``
 
        Supports slicing.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList('abcde')
        >>> del sl[2]
        >>> sl
        SortedList(['a', 'b', 'd', 'e'])
        >>> del sl[:2]
        >>> sl
        SortedList(['d', 'e'])
 
        :param index: integer or slice for indexing
        :raises IndexError: if index out of range
 
        rEréN) Ú
isinstanceÚsliceÚindicesr.r@Ú_getitemr5r\rwrprc) rÚindexÚstartÚstopÚsteprBr€rprcrJrar%r%r&Ú __delitem__üs(
 
 
  zSortedList.__delitem__cs(ˆj}t|tƒrb| ˆj¡\}}}|dkr||kr|dkrX|ˆjkrXttˆjgƒSˆ |¡\}}||}|||}    t|ƒ|    kr’|||    …S|ˆjkr¶t|ƒd}
t||
ƒ}    nˆ |¡\}
}    |||d…} ||d|
…} tt| | ƒ} | ||
d|    …7} | S|dkr@||kr@ˆ     t|d|dƒ¡} |  
¡| St |||ƒ}t ‡fdd„|DƒƒSˆjr˜|dkr€|ddS|dkr |ddSnt dƒ‚d|krÀt|dƒkrÐnn |d|St|dƒ}| |krödkr
nn|d||Sˆ |¡\}}|||SdS)aÚLookup value at `index` in sorted list.
 
        ``sl.__getitem__(index)`` <==> ``sl[index]``
 
        Supports slicing.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList('abcde')
        >>> sl[1]
        'b'
        >>> sl[-1]
        'e'
        >>> sl[2:5]
        ['c', 'd', 'e']
 
        :param index: integer or slice for indexing
        :return: value or list of values
        :raises IndexError: if index out of range
 
        rErNrLc3s|]}ˆ |¡VqdSr)r)rSr‚r?r%r&rUssz)SortedList.__getitem__.<locals>.<genexpr>rm)r1r~rr€r.rrrprFrÚreverser\rrrn)rr‚r1rƒr„r…Z    start_posZ    start_idxÚ
start_listZstop_idxZstop_posÚprefixZmiddler!r€Úlen_lastrJrar%r?r&Ú __getitem__0sN    
 
 
$  zSortedList.__getitem__cCsd}t|ƒ‚dS)zÑRaise not-implemented error.
 
        ``sl.__setitem__(index, value)`` <==> ``sl[index] = value``
 
        :raises NotImplementedError: use ``del sl[index]`` and
            ``sl.add(value)`` instead
 
        z3use ``del sl[index]`` and ``sl.add(value)`` insteadN©ÚNotImplementedError)rr‚rIÚmessager%r%r&Ú __setitem__‹s    zSortedList.__setitem__cCs t |j¡S)zîReturn an iterator over the sorted list.
 
        ``sl.__iter__()`` <==> ``iter(sl)``
 
        Iterating the sorted list while adding or deleting values may raise a
        :exc:`RuntimeError` or fail to iterate over all values.
 
        )rÚ from_iterabler1r?r%r%r&Ú__iter__˜s    zSortedList.__iter__cCst ttt|jƒƒ¡S)zýReturn a reverse iterator over the sorted list.
 
        ``sl.__reversed__()`` <==> ``reversed(sl)``
 
        Iterating the sorted list while adding or deleting values may raise a
        :exc:`RuntimeError` or fail to iterate over all values.
 
        )rrrsrwr1r?r%r%r&Ú __reversed__¤s    zSortedList.__reversed__cCs tdƒ‚dS)a¨Raise not-implemented error.
 
        Sorted list maintains values in ascending sort order. Values may not be
        reversed in-place.
 
        Use ``reversed(sl)`` for an iterator over values in descending sort
        order.
 
        Implemented to override `MutableSequence.reverse` which provides an
        erroneous default implementation.
 
        :raises NotImplementedError: use ``reversed(sl)`` instead
 
        zuse ``reversed(sl)`` insteadNrŒr?r%r%r&r‡°szSortedList.reverseFc Cs|j}|stdƒSt||ƒ |j¡\}}}||kr:tdƒS|j}||ƒ\}}||krrt|jƒd}    t|jdƒ}
n ||ƒ\}    }
| |||    |
|¡S)aðReturn an iterator that slices sorted list from `start` to `stop`.
 
        The `start` and `stop` index are treated inclusive and exclusive,
        respectively.
 
        Both `start` and `stop` default to `None` which is automatically
        inclusive of the beginning and end of the sorted list.
 
        When `reverse` is `True` the values are yielded from the iterator in
        reverse order; `reverse` defaults to `False`.
 
        >>> sl = SortedList('abcdefghij')
        >>> it = sl.islice(2, 6)
        >>> list(it)
        ['c', 'd', 'e', 'f']
 
        :param int start: start index (inclusive)
        :param int stop: stop index (exclusive)
        :param bool reverse: yield values in reverse order
        :return: iterator
 
        r%rErL)r.rtrr€rprFr1Ú_islice) rrƒr„r‡r.Ú_rpÚmin_posÚmin_idxÚmax_posÚmax_idxr%r%r&ÚisliceÂs  zSortedList.islicec Csº|j}||krtdƒS||krZ|r@tt||ƒƒ}t||j|ƒSt||ƒ}t||j|ƒS|d}||krî|r²t|t||ƒƒ}    t|ƒ}
tt||jt|
ƒƒt||jt|    ƒƒƒSt|t||ƒƒ}    t|ƒ}
tt||j|    ƒt||j|
ƒƒS|r`t|t||ƒƒ}    t||ƒ} t|jt| ƒƒ} t|ƒ}
tt||jt|
ƒƒt tt| ƒ¡t||jt|    ƒƒƒSt|t||ƒƒ}    t||ƒ} t|j| ƒ} t|ƒ}
tt||j|    ƒt | ¡t||j|
ƒƒS)ayReturn an iterator that slices sorted list using two index pairs.
 
        The index pairs are (min_pos, min_idx) and (max_pos, max_idx), the
        first inclusive and the latter exclusive. See `_pos` for details on how
        an index is converted to an index pair.
 
        When `reverse` is `True`, values are yielded from the iterator in
        reverse order.
 
        r%rE)    r1rtrwr\rsr‹rFrr) rr•r–r—r˜r‡r1r€Znext_posZ min_indicesZ max_indicesZsublist_indicesZsublistsr%r%r&r“ðsV 
þþ
ý
 ýzSortedList._islice©TTc Cs@|j}|stdƒS|j}|dkr*d}d}nb|dr`t||ƒ}|t|ƒkrPtdƒSt|||ƒ}n,t||ƒ}|t|ƒkr~tdƒSt|||ƒ}|dkr®t|ƒd}    t||    ƒ}
n€|dròt||ƒ}    |    t|ƒkrâ|    d8}    t||    ƒ}
nt||    |ƒ}
n<t||ƒ}    |    t|ƒkr |    d8}    t||    ƒ}
nt||    |ƒ}
| |||    |
|¡S)aÛCreate an iterator of values between `minimum` and `maximum`.
 
        Both `minimum` and `maximum` default to `None` which is automatically
        inclusive of the beginning and end of the sorted list.
 
        The argument `inclusive` is a pair of booleans that indicates whether
        the minimum and maximum ought to be included in the range,
        respectively. The default is ``(True, True)`` such that the range is
        inclusive of both minimum and maximum.
 
        When `reverse` is `True` the values are yielded from the iterator in
        reverse order; `reverse` defaults to `False`.
 
        >>> sl = SortedList('abcdefghij')
        >>> it = sl.irange('c', 'f')
        >>> list(it)
        ['c', 'd', 'e', 'f']
 
        :param minimum: minimum value to start iterating
        :param maximum: maximum value to stop iterating
        :param inclusive: pair of booleans
        :param bool reverse: yield values in reverse order
        :return: iterator
 
        r%NrrE)r2rtr1rrFrr“) rÚminimumÚmaximumÚ    inclusiver‡r2r1r•r–r—r˜r%r%r&Úirange0s>
 
 
 
zSortedList.irangecCs|jS)z~Return the size of the sorted list.
 
        ``sl.__len__()`` <==> ``len(sl)``
 
        :return: size of sorted list
 
        )r.r?r%r%r&Ú__len__ƒszSortedList.__len__cCsF|j}|sdSt||ƒ}|t|ƒkr*|jSt|j||ƒ}| ||¡S)aèReturn an index to insert `value` in the sorted list.
 
        If the `value` is already present, the insertion point will be before
        (to the left of) any existing values.
 
        Similar to the `bisect` module in the standard library.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList([10, 11, 12, 13, 14])
        >>> sl.bisect_left(12)
        2
 
        :param value: insertion index of value in sorted list
        :return: index
 
        r)r2rrFr.r1rl©rrIr2rJrar%r%r&rŽs
 zSortedList.bisect_leftcCsF|j}|sdSt||ƒ}|t|ƒkr*|jSt|j||ƒ}| ||¡S)aReturn an index to insert `value` in the sorted list.
 
        Similar to `bisect_left`, but if `value` is already present, the
        insertion point will be after (to the right of) any existing values.
 
        Similar to the `bisect` module in the standard library.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList([10, 11, 12, 13, 14])
        >>> sl.bisect_right(12)
        3
 
        :param value: insertion index of value in sorted list
        :return: index
 
        r)r2rrFr.r1rlr r%r%r&r®s
 zSortedList.bisect_rightc
Cs¢|j}|sdSt||ƒ}|t|ƒkr(dS|j}t|||ƒ}t||ƒ}|t|ƒkrd|j| ||¡St|||ƒ}||kr‚||S| ||¡}| ||¡}    ||    S)a)Return number of occurrences of `value` in the sorted list.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
        >>> sl.count(3)
        3
 
        :param value: value to count in sorted list
        :return: count
 
        r)r2rrFr1rr.rl)
rrIr2Úpos_leftr1Úidx_leftZ    pos_rightZ    idx_rightÚrightÚleftr%r%r&ÚcountÑs" 
 
   zSortedList.countcCs
| |¡S)zyReturn a shallow copy of the sorted list.
 
        Runtime complexity: `O(n)`
 
        :return: new sorted list
 
        )Ú    __class__r?r%r%r&ÚcopyùszSortedList.copycCs tdƒ‚dS)zàRaise not-implemented error.
 
        Implemented to override `MutableSequence.append` which provides an
        erroneous default implementation.
 
        :raises NotImplementedError: use ``sl.add(value)`` instead
 
        úuse ``sl.add(value)`` insteadNrŒ©rrIr%r%r&rGs    zSortedList.appendcCs tdƒ‚dS)zäRaise not-implemented error.
 
        Implemented to override `MutableSequence.extend` which provides an
        erroneous default implementation.
 
        :raises NotImplementedError: use ``sl.update(values)`` instead
 
        z!use ``sl.update(values)`` insteadNrŒ©rrBr%r%r&r[s    zSortedList.extendcCs tdƒ‚dS)zjRaise not-implemented error.
 
        :raises NotImplementedError: use ``sl.add(value)`` instead
 
        r¨NrŒ)rr‚rIr%r%r&rMszSortedList.insertrLcCs6|jstdƒ‚|j}|dkr8|dd}| dd¡|S|dkrxt|ƒd}t||ƒd}|||}| ||¡|Sd|kr”t|dƒkr´nn|d|}| d|¡|St|dƒ}| |krØdkr nn0t|ƒd}||}|||}| ||¡|S| |¡\}}|||}| ||¡|S)aRemove and return value at `index` in sorted list.
 
        Raise :exc:`IndexError` if the sorted list is empty or index is out of
        range.
 
        Negative indices are supported.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList('abcde')
        >>> sl.pop()
        'e'
        >>> sl.pop(2)
        'c'
        >>> sl
        SortedList(['a', 'b', 'd'])
 
        :param int index: index of value (default -1)
        :return: value
        :raises IndexError: if index is out of range
 
        zpop index out of rangerrLrE)r.rnr1rcrFrp)rr‚r1r^rJÚlocrŠrar%r%r&Úpop's8              zSortedList.popc Cs6|j}|std |¡ƒ‚|dkr$d}|dkr4||7}|dkr@d}|dkrL|}|dkr\||7}||krh|}||kr~td |¡ƒ‚|j}t||ƒ}|t|ƒkr¨td |¡ƒ‚|j}t|||ƒ}||||krÚtd |¡ƒ‚|d8}| ||¡}    ||    kr|    |kr$|    Sn| |¡d}
||
kr$|Std |¡ƒ‚dS)aqReturn first index of value in sorted list.
 
        Raise ValueError if `value` is not present.
 
        Index must be between `start` and `stop` for the `value` to be
        considered present. The default value, None, for `start` and `stop`
        indicate the beginning and end of the sorted list.
 
        Negative indices are supported.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList('abcde')
        >>> sl.index('d')
        3
        >>> sl.index('z')
        Traceback (most recent call last):
          ...
        ValueError: 'z' is not in list
 
        :param value: value in sorted list
        :param int start: start index (default None, start of sorted list)
        :param int stop: stop index (default None, end of sorted list)
        :return: index of value
        :raises ValueError: if value is not present
 
        ú{0!r} is not in listNrrE)    r.rerfr2rrFr1rlÚ _bisect_right) rrIrƒr„r.r2r¡r1r¢r¤r£r%r%r&r‚csD
 
 
 
zSortedList.indexcCs"tt|jgƒ}| |¡| |¡S)a¾Return new sorted list containing all values in both sequences.
 
        ``sl.__add__(other)`` <==> ``sl + other``
 
        Values in `other` do not need to be in sorted order.
 
        Runtime complexity: `O(n*log(n))`
 
        >>> sl1 = SortedList('bat')
        >>> sl2 = SortedList('cat')
        >>> sl1 + sl2
        SortedList(['a', 'a', 'b', 'c', 't', 't'])
 
        :param other: other iterable
        :return: new sorted list
 
        )rrr1r[r¦©rÚotherrBr%r%r&Ú__add__°s
zSortedList.__add__cCs| |¡|S)a®Update sorted list with values from `other`.
 
        ``sl.__iadd__(other)`` <==> ``sl += other``
 
        Values in `other` do not need to be in sorted order.
 
        Runtime complexity: `O(k*log(n))` -- approximate.
 
        >>> sl = SortedList('bat')
        >>> sl += 'cat'
        >>> sl
        SortedList(['a', 'a', 'b', 'c', 't', 't'])
 
        :param other: other iterable
        :return: existing sorted list
 
        )r5)rr°r%r%r&Ú__iadd__És
zSortedList.__iadd__cCstt|jgƒ|}| |¡S)ajReturn new sorted list with `num` shallow copies of values.
 
        ``sl.__mul__(num)`` <==> ``sl * num``
 
        Runtime complexity: `O(n*log(n))`
 
        >>> sl = SortedList('abc')
        >>> sl * 3
        SortedList(['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'])
 
        :param int num: count of shallow copies
        :return: new sorted list
 
        )rrr1r¦©rÚnumrBr%r%r&Ú__mul__ßszSortedList.__mul__cCs(tt|jgƒ|}| ¡| |¡|S)aUpdate the sorted list with `num` shallow copies of values.
 
        ``sl.__imul__(num)`` <==> ``sl *= num``
 
        Runtime complexity: `O(n*log(n))`
 
        >>> sl = SortedList('abc')
        >>> sl *= 3
        >>> sl
        SortedList(['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'])
 
        :param int num: count of shallow copies
        :return: existing sorted list
 
        )rrr1r@r5r³r%r%r&Ú__imul__ôs
zSortedList.__imul__cs:‡fdd„}ˆj}d |¡|_d}t| |||¡ƒ|_|S)zMake comparator method.cspt|tƒstS|j}t|ƒ}||kr<ˆtkr0dSˆtkr<dSt||ƒD]\}}||krFˆ||ƒSqFˆ||ƒS)z,Compare method for sorted list and sequence.FT)r~rÚNotImplementedr.rFr r ru)rr°Zself_lenZ    len_otherÚalphaÚbeta©Úseq_opr%r&Úcomparer s
z'SortedList.__make_cmp.<locals>.comparerz__{0}__a7Return true if and only if sorted list is {0} `other`.
 
        ``sl.__{1}__(other)`` <==> ``sl {2} other``
 
        Comparisons use lexicographical order as with sequences.
 
        Runtime complexity: `O(n)`
 
        :param other: `other` sequence
        :return: true if sorted list is {0} `other`
 
        )Ú__name__rfrÚ__doc__)r»ÚsymbolÚdocr¼Z seq_op_nameZdoc_strr%rºr&Z
__make_cmp
s    zSortedList.__make_cmpz==zequal toz!=z not equal toú<z    less thanú>z greater thanz<=zless than or equal toz>=zgreater than or equal tocCstt|jgƒ}t|ƒ|ffSr)rrr1Útyperªr%r%r&Ú
__reduce__;szSortedList.__reduce__cCsd t|ƒjt|ƒ¡S)z‹Return string representation of sorted list.
 
        ``sl.__repr__()`` <==> ``repr(sl)``
 
        :return: string representation
 
        z
{0}({1!r}))rfrÃr½rrr?r%r%r&Ú__repr__@s    zSortedList.__repr__cszl|jdkst‚t|jƒt|jƒks*t‚|jtdd„|jDƒƒksHt‚|jD]0}tdt|ƒƒD]}||d||ks`t‚q`qNtdt|jƒƒD](}|j|dd|j|dkst‚qtt|jƒƒD] }|j||j|dksÈt‚qÈ|jd>‰t‡fdd„|jDƒƒst‚|jd?}tdt|jƒdƒD]}t|j|ƒ|ks0t‚q0|j    rl|j|j    dksnt‚t|j    ƒ|j
t|jƒksŽt‚tt|jƒƒD].}|j    |j
|}|t|j|ƒksœt‚qœt|j
ƒD]”}|d>d}|t|j    ƒkr |j    |dksht‚n\|dt|j    ƒkr<|j    ||j    |ksht‚n,|j    ||j    |d}||j    |ksÖt‚qÖWn”t j t jdtd    |jƒtd
|jƒtd |j
ƒtd t|j    ƒƒtd |j    ƒtdt|jƒƒtd|jƒtdt|jƒƒtd|jƒ‚YnXdS)zNCheck invariants of sorted list.
 
        Runtime complexity: `O(n)`
 
        rQcss|]}t|ƒVqdSr©rFrWr%r%r&rUUsz$SortedList._check.<locals>.<genexpr>rErLrc3s|]}t|ƒˆkVqdSrrÆrW©Údoubler%r&rUjs©ÚfilerFrAÚoffsetror‚Ú    len_maxesÚmaxesÚ    len_listsÚlistsN)r0r-rFr2r1r.Úsumr\Úallr3r4Ú    tracebackÚ    print_excÚsysÚstdoutÚprint)rrXrJrOÚleafrPÚ    child_sumr%rÇr&Ú_checkLsT
&
 
        zSortedList._check)NN)NN)NNF)NNršF)rL)NN)Fr½Ú
__module__Ú __qualname__r¾r/r8r:Úpropertyr rCrDr@r
rHr_r5rbrrgrcrlrprjr†r‹rrr‘r’r‡r™r“ržrŸrrÚbisectr®r¥r§Ú__copy__rGr[rMr¬r‚r±Ú__radd__r²rµÚ__rmul__r¶Z_SortedList__make_cmpr Ú__eq__r Ú__ne__rÚ__lt__r Ú__gt__rÚ__le__rÚ__ge__Ú staticmethodrÄr+rÅrÙr%r%r%r&r,Us~7
 
 
     $"$!'7Q^E4X   
.@ÿ
S  (
      
<
M(      
r,cCs|S)zIdentity function.r%)rIr%r%r&Úidentity–srèc@seZdZdZdefdd„Zdefdd„Zedd„ƒZd    d
„Z    e    Z
d d „Z d d„Z dd„Z e Zdd„Zdd„Zdd„Zdd„Zd7dd„Zd8dd„ZeZdd „Zd!d"„ZeZd#d$„ZeZd%d&„ZeZeZd'd(„Zd)d*„ZeZ d9d+d,„Z!d-d.„Z"e"Z#d/d0„Z$d1d2„Z%e&ƒd3d4„ƒZ'd5d6„Z(dS):r;aiSorted-key list is a subtype of sorted list.
 
    The sorted-key list maintains values in comparison order based on the
    result of a key function applied to every value.
 
    All the same methods that are available in :class:`SortedList` are also
    available in :class:`SortedKeyList`.
 
    Additional methods provided:
 
    * :attr:`SortedKeyList.key`
    * :func:`SortedKeyList.bisect_key_left`
    * :func:`SortedKeyList.bisect_key_right`
    * :func:`SortedKeyList.irange_key`
 
    Some examples below use:
 
    >>> from operator import neg
    >>> neg
    <built-in function neg>
    >>> neg(1)
    -1
 
    NcCsH||_d|_|j|_g|_g|_g|_g|_d|_|dk    rD|     |¡dS)a6Initialize sorted-key list instance.
 
        Optional `iterable` argument provides an initial iterable of values to
        initialize the sorted-key list.
 
        Optional `key` argument defines a callable that, like the `key`
        argument to Python's `sorted` function, extracts a comparison key from
        each value. The default is the identity function.
 
        Runtime complexity: `O(n*log(n))`
 
        >>> from operator import neg
        >>> skl = SortedKeyList(key=neg)
        >>> skl
        SortedKeyList([], key=<built-in function neg>)
        >>> skl = SortedKeyList([3, 1, 2], key=neg)
        >>> skl
        SortedKeyList([3, 2, 1], key=<built-in function neg>)
 
        :param iterable: initial values (optional)
        :param key: function used to extract comparison key (optional)
 
        rN)
Ú_keyr.r/r0r1Ú_keysr2r3r4r5r6r%r%r&r8´szSortedKeyList.__init__cCs
t |¡Sr)r9r:r=r%r%r&r:ÙszSortedKeyList.__new__cCs|jS)z4Function used to extract comparison key from values.)rér?r%r%r&r ÝszSortedKeyList.keycCs:d|_|jdd…=|jdd…=|jdd…=|jdd…=dS)zURemove all values from sorted-key list.
 
        Runtime complexity: `O(n)`
 
        rN)r.r1rêr2r3r?r%r%r&rDãs
   zSortedKeyList.clearcCsÒ|j}|j}|j}| |¡}|ržt||ƒ}|t|ƒkrd|d8}|| |¡|| |¡|||<n.t|||ƒ}|| ||¡|| ||¡| |¡n"| |g¡| |g¡| |¡|j    d7_    dS)a{Add `value` to sorted-key list.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> from operator import neg
        >>> skl = SortedKeyList(key=neg)
        >>> skl.add(3)
        >>> skl.add(1)
        >>> skl.add(2)
        >>> skl
        SortedKeyList([3, 2, 1], key=<built-in function neg>)
 
        :param value: value to add to sorted-key list
 
        rEN)
r1rêr2rérrFrGrMrHr.)rrIr1rêr2r rJrar%r%r&r
òs&
 
 
 
zSortedKeyList.addc Cs|j}|j}|j}t||ƒ|jd>kr¼|j}|j}||}||}||d…}    ||d…}
||d…=||d…=|d||<| |d|    ¡| |d|
¡| |d|
d¡|dd…=n@|rü|j|} | rì|| d7<| dd?} qÊ|dd7<dSrK)r1rêr3rFr0r2rMr4) rrJr1rêr3r2r0rNZ    _keys_posrOZ    half_keysrPr%r%r&rHs.      
 
 
zSortedKeyList._expandcsòˆj}ˆj}ˆj}t|ˆjd‰|r€tˆƒdˆjkrd| ˆ¡tt    |gƒ‰ˆj
ˆjdˆ  ¡nˆj }ˆD] }||ƒqndSˆj ‰| ‡‡fdd„tdtˆƒˆƒDƒ¡| ‡fdd„|Dƒ¡| dd„|Dƒ¡tˆƒˆ_ˆjdd…=dS)    atUpdate sorted-key list by adding all values from `iterable`.
 
        Runtime complexity: `O(k*log(n))` -- approximate.
 
        >>> from operator import neg
        >>> skl = SortedKeyList(key=neg)
        >>> skl.update([3, 1, 2])
        >>> skl
        SortedKeyList([3, 2, 1], key=<built-in function neg>)
 
        :param iterable: iterable of values to add
 
        ©r rQNc3s|]}ˆ||ˆ…VqdSrr%rRrTr%r&rUesÿz'SortedKeyList.update.<locals>.<genexpr>rc3s|]}ttˆj|ƒƒVqdSr)rrrsré)rSZ_listr?r%r&rUgscss|]}|dVqdSrVr%rWr%r%r&rUhs)r1rêr2rYrérFr.rGrrrZr@r
r0r[r\r3)rr7r1rêr2r]r^r%)r0rrBr&r_Es,
 
 
ÿ
zSortedKeyList.updatec
CsÂ|j}|sdS| |¡}t||ƒ}|t|ƒkr2dS|j}|j}t|||ƒ}t|ƒ}t||ƒ}    ||||krtdS||||krˆdS|d7}||    kr`|d7}||kr¬dSt||ƒ}    d}q`dS)a›Return true if `value` is an element of the sorted-key list.
 
        ``skl.__contains__(value)`` <==> ``value in skl``
 
        Runtime complexity: `O(log(n))`
 
        >>> from operator import neg
        >>> skl = SortedKeyList([1, 2, 3, 4, 5], key=neg)
        >>> 3 in skl
        True
 
        :param value: search for value in sorted-key list
        :return: true if `value` in sorted-key list
 
        FTrErN©r2rérrFr1rê©
rrIr2r rJr1rêraÚlen_keysÚ len_sublistr%r%r&rbos.
 
   zSortedKeyList.__contains__c
CsÎ|j}|sdS| |¡}t||ƒ}|t|ƒkr2dS|j}|j}t|||ƒ}t|ƒ}t||ƒ}    ||||krtdS||||kr”| ||¡dS|d7}||    kr`|d7}||kr¸dSt||ƒ}    d}q`dS)a¬Remove `value` from sorted-key list if it is a member.
 
        If `value` is not a member, do nothing.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> from operator import neg
        >>> skl = SortedKeyList([5, 4, 3, 2, 1], key=neg)
        >>> skl.discard(1)
        >>> skl.discard(0)
        >>> skl == [5, 4, 3, 2]
        True
 
        :param value: `value` to discard from sorted-key list
 
        NrEr)r2rérrFr1rêrcrír%r%r&r s0
 
    zSortedKeyList.discardc
Csö|j}|std |¡ƒ‚| |¡}t||ƒ}|t|ƒkrFtd |¡ƒ‚|j}|j}t|||ƒ}t|ƒ}t||ƒ}    ||||kr’td |¡ƒ‚||||kr²| ||¡dS|d7}||    krt|d7}||kràtd |¡ƒ‚t||ƒ}    d}qtdS)aSRemove `value` from sorted-key list; `value` must be a member.
 
        If `value` is not a member, raise ValueError.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> from operator import neg
        >>> skl = SortedKeyList([1, 2, 3, 4, 5], key=neg)
        >>> skl.remove(5)
        >>> skl == [4, 3, 2, 1]
        True
        >>> skl.remove(0)
        Traceback (most recent call last):
          ...
        ValueError: 0 not in list
 
        :param value: `value` to remove from sorted-key list
        :raises ValueError: if `value` is not in sorted-key list
 
        rdNrEr)    r2rerfrérrFr1rêrcrír%r%r&rgÑs0
 
    zSortedKeyList.removec Cs\|j}|j}|j}|j}||}||}||=||=|jd8_t|ƒ}    |    |jd?krª|d||<|r¨|j|}
|
dkr˜||
d8<|
dd?}
qr|dd8<n®t|ƒdkr(|sÄ|d7}|d} ||  ||¡||  ||¡|| d|| <||=||=||=|dd…=|     | ¡n0|    r<|d||<n||=||=||=|dd…=dSrh)
r1rêr2r3r.rFr0r4r[rH) rrJrar1rêr2r3Zkeys_posZ    lists_posZ len_keys_posrPrir%r%r&rcsH 
 
 zSortedKeyList._deleteršFcCs>|dk    r| |¡nd}|dk    r(| |¡nd}|j||||dS)aCreate an iterator of values between `minimum` and `maximum`.
 
        Both `minimum` and `maximum` default to `None` which is automatically
        inclusive of the beginning and end of the sorted-key list.
 
        The argument `inclusive` is a pair of booleans that indicates whether
        the minimum and maximum ought to be included in the range,
        respectively. The default is ``(True, True)`` such that the range is
        inclusive of both minimum and maximum.
 
        When `reverse` is `True` the values are yielded from the iterator in
        reverse order; `reverse` defaults to `False`.
 
        >>> from operator import neg
        >>> skl = SortedKeyList([11, 12, 13, 14, 15], key=neg)
        >>> it = skl.irange(14.5, 11.5)
        >>> list(it)
        [14, 13, 12]
 
        :param minimum: minimum value to start iterating
        :param maximum: maximum value to stop iterating
        :param inclusive: pair of booleans
        :param bool reverse: yield values in reverse order
        :return: iterator
 
        N)Úmin_keyÚmax_keyrr‡)réÚ _irange_key)rr›rœrr‡rðrñr%r%r&ržBsþzSortedKeyList.irangec Cs@|j}|stdƒS|j}|dkr*d}d}nb|dr`t||ƒ}|t|ƒkrPtdƒSt|||ƒ}n,t||ƒ}|t|ƒkr~tdƒSt|||ƒ}|dkr®t|ƒd}    t||    ƒ}
n€|dròt||ƒ}    |    t|ƒkrâ|    d8}    t||    ƒ}
nt||    |ƒ}
n<t||ƒ}    |    t|ƒkr |    d8}    t||    ƒ}
nt||    |ƒ}
| |||    |
|¡S)aCreate an iterator of values between `min_key` and `max_key`.
 
        Both `min_key` and `max_key` default to `None` which is automatically
        inclusive of the beginning and end of the sorted-key list.
 
        The argument `inclusive` is a pair of booleans that indicates whether
        the minimum and maximum ought to be included in the range,
        respectively. The default is ``(True, True)`` such that the range is
        inclusive of both minimum and maximum.
 
        When `reverse` is `True` the values are yielded from the iterator in
        reverse order; `reverse` defaults to `False`.
 
        >>> from operator import neg
        >>> skl = SortedKeyList([11, 12, 13, 14, 15], key=neg)
        >>> it = skl.irange_key(-14, -12)
        >>> list(it)
        [14, 13, 12]
 
        :param min_key: minimum key to start iterating
        :param max_key: maximum key to stop iterating
        :param inclusive: pair of booleans
        :param bool reverse: yield values in reverse order
        :return: iterator
 
        r%NrrE)r2rtrêrrFrr“) rrðrñrr‡r2rêr•r–r—r˜r%r%r&Ú
irange_keyfs>
 
 
 
zSortedKeyList.irange_keycCs| | |¡¡S)aReturn an index to insert `value` in the sorted-key list.
 
        If the `value` is already present, the insertion point will be before
        (to the left of) any existing values.
 
        Similar to the `bisect` module in the standard library.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> from operator import neg
        >>> skl = SortedKeyList([5, 4, 3, 2, 1], key=neg)
        >>> skl.bisect_left(1)
        4
 
        :param value: insertion index of value in sorted-key list
        :return: index
 
        )Ú_bisect_key_leftrér©r%r%r&r¼szSortedKeyList.bisect_leftcCs| | |¡¡S)a5Return an index to insert `value` in the sorted-key list.
 
        Similar to `bisect_left`, but if `value` is already present, the
        insertion point will be after (to the right of) any existing values.
 
        Similar to the `bisect` module in the standard library.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> from operator import neg
        >>> skl = SortedList([5, 4, 3, 2, 1], key=neg)
        >>> skl.bisect_right(1)
        5
 
        :param value: insertion index of value in sorted-key list
        :return: index
 
        )Ú_bisect_key_rightrér©r%r%r&rÒszSortedKeyList.bisect_rightcCsF|j}|sdSt||ƒ}|t|ƒkr*|jSt|j||ƒ}| ||¡S)aReturn an index to insert `key` in the sorted-key list.
 
        If the `key` is already present, the insertion point will be before (to
        the left of) any existing keys.
 
        Similar to the `bisect` module in the standard library.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> from operator import neg
        >>> skl = SortedKeyList([5, 4, 3, 2, 1], key=neg)
        >>> skl.bisect_key_left(-1)
        4
 
        :param key: insertion index of key in sorted-key list
        :return: index
 
        r)r2rrFr.rêrl©rr r2rJrar%r%r&Úbisect_key_leftês
 zSortedKeyList.bisect_key_leftcCsF|j}|sdSt||ƒ}|t|ƒkr*|jSt|j||ƒ}| ||¡S)a4Return an index to insert `key` in the sorted-key list.
 
        Similar to `bisect_key_left`, but if `key` is already present, the
        insertion point will be after (to the right of) any existing keys.
 
        Similar to the `bisect` module in the standard library.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> from operator import neg
        >>> skl = SortedList([5, 4, 3, 2, 1], key=neg)
        >>> skl.bisect_key_right(-1)
        5
 
        :param key: insertion index of key in sorted-key list
        :return: index
 
        r)r2rrFr.rêrlrör%r%r&Úbisect_key_right    s
 zSortedKeyList.bisect_key_rightc CsÊ|j}|sdS| |¡}t||ƒ}|t|ƒkr2dS|j}|j}t|||ƒ}d}t|ƒ}    t||ƒ}
||||krx|S||||kr|d7}|d7}||
krd|d7}||    kr´|St||ƒ}
d}qddS)adReturn number of occurrences of `value` in the sorted-key list.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> from operator import neg
        >>> skl = SortedKeyList([4, 4, 4, 4, 3, 3, 3, 2, 2, 1], key=neg)
        >>> skl.count(2)
        2
 
        :param value: value to count in sorted-key list
        :return: count
 
        rrENrì) rrIr2r rJr1rêrarkrîrïr%r%r&r¥3    s0
 
   zSortedKeyList.countcCs|j||jdS)zReturn a shallow copy of the sorted-key list.
 
        Runtime complexity: `O(n)`
 
        :return: new sorted-key list
 
        rë)r¦rér?r%r%r&r§a    szSortedKeyList.copycCs¨|j}|std |¡ƒ‚|dkr$d}|dkr4||7}|dkr@d}|dkrL|}|dkr\||7}||krh|}||kr~td |¡ƒ‚|j}| |¡}t||ƒ}|t|ƒkr²td |¡ƒ‚|d8}|j}|j}    t|    ||ƒ}
t|    ƒ} t|    |ƒ} |    ||
|krtd |¡ƒ‚|||
|krT|     ||
¡} || kr>|krFnn| S| |krTq–|
d7}
|
| krè|d7}|| kr„td |¡ƒ‚t|    |ƒ} d}
qètd |¡ƒ‚dS)a»Return first index of value in sorted-key list.
 
        Raise ValueError if `value` is not present.
 
        Index must be between `start` and `stop` for the `value` to be
        considered present. The default value, None, for `start` and `stop`
        indicate the beginning and end of the sorted-key list.
 
        Negative indices are supported.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> from operator import neg
        >>> skl = SortedKeyList([5, 4, 3, 2, 1], key=neg)
        >>> skl.index(2)
        3
        >>> skl.index(0)
        Traceback (most recent call last):
          ...
        ValueError: 0 is not in list
 
        :param value: value in sorted-key list
        :param int start: start index (default None, start of sorted-key list)
        :param int stop: stop index (default None, end of sorted-key list)
        :return: index of value
        :raises ValueError: if value is not present
 
        r­NrrE)
r.rerfr2rérrFr1rêrl)rrIrƒr„r.r2r rJr1rêrarîrïr«r%r%r&r‚n    sX
 
 
 
 zSortedKeyList.indexcCs(tt|jgƒ}| |¡|j||jdS)a)Return new sorted-key list containing all values in both sequences.
 
        ``skl.__add__(other)`` <==> ``skl + other``
 
        Values in `other` do not need to be in sorted-key order.
 
        Runtime complexity: `O(n*log(n))`
 
        >>> from operator import neg
        >>> skl1 = SortedKeyList([5, 4, 3], key=neg)
        >>> skl2 = SortedKeyList([2, 1, 0], key=neg)
        >>> skl1 + skl2
        SortedKeyList([5, 4, 3, 2, 1, 0], key=<built-in function neg>)
 
        :param other: other iterable
        :return: new sorted-key list
 
        rë)rrr1r[r¦rér¯r%r%r&r±Ã    s
zSortedKeyList.__add__cCs"tt|jgƒ|}|j||jdS)a°Return new sorted-key list with `num` shallow copies of values.
 
        ``skl.__mul__(num)`` <==> ``skl * num``
 
        Runtime complexity: `O(n*log(n))`
 
        >>> from operator import neg
        >>> skl = SortedKeyList([3, 2, 1], key=neg)
        >>> skl * 2
        SortedKeyList([3, 3, 2, 2, 1, 1], key=<built-in function neg>)
 
        :param int num: count of shallow copies
        :return: new sorted-key list
 
        rë)rrr1r¦rér³r%r%r&rµÝ    szSortedKeyList.__mul__cCs tt|jgƒ}t|ƒ||jffSr)rrr1rÃr rªr%r%r&rÄñ    szSortedKeyList.__reduce__cCst|ƒj}d |t|ƒ|j¡S)z‘Return string representation of sorted-key list.
 
        ``skl.__repr__()`` <==> ``repr(skl)``
 
        :return: string representation
 
        z{0}({1!r}, key={2!r}))rÃr½rfrrré)rZ    type_namer%r%r&rÅö    s    
zSortedKeyList.__repr__c s’zÚ|jdkst‚t|jƒt|jƒkr8t|jƒks>nt‚|jtdd„|jDƒƒks\t‚|jD]0}tdt|ƒƒD]}||d||kstt‚qtqbtdt|jƒƒD](}|j|dd|j|dks¤t‚q¤t    |j|jƒD]F\}}t|ƒt|ƒksøt‚t    ||ƒD]\}}| 
|¡|kst‚qqÜtt|jƒƒD]$}|j||j|dks2t‚q2|jd>‰t ‡fdd„|jDƒƒs€t‚|jd?}tdt|jƒdƒD]}t|j|ƒ|ksžt‚qž|j rÚ|j|j dksÜt‚t|j ƒ|j t|jƒksüt‚tt|jƒƒD].}|j |j |}|t|j|ƒks
t‚q
t|j ƒD]”}|d>d}    |    t|j ƒkrz|j |dksÖt‚n\|    dt|j ƒkrª|j ||j |    ksÖt‚n,|j |    |j |    d}
|
|j |ksDt‚qDWn°tjtjdtd    |jƒtd
|jƒtd |j ƒtd t|j ƒƒtd |j ƒtdt|jƒƒtd|jƒtdt|jƒƒtd|jƒtdt|jƒƒtd|jƒ‚YnXdS)zRCheck invariants of sorted-key list.
 
        Runtime complexity: `O(n)`
 
        rQcss|]}t|ƒVqdSrrÆrWr%r%r&rU
sz'SortedKeyList._check.<locals>.<genexpr>rErLrc3s|]}t|ƒˆkVqdSrrÆrWrÇr%r&rU(
srÉrFrArËror‚rÌrÍrîÚkeysrÎrÏN)r0r-rFr2r1rêr.rÐr\rurérÑr3r4rÒrÓrÔrÕrÖ) rrXrJZ val_sublistZ key_sublistr^r rOr×rPrØr%rÇr&rÙ
s`,
&"
 
         zSortedKeyList._check)NNršF)NNršF)NN))r½rÚrÛr¾rèr8r:rÜr rDr@r
rHr_r5rbrrgrcržróròrrrÝr÷rôrøZ
bisect_keyrõr¥r§rÞr‚r±rßrµrÄr+rÅrÙr%r%r%r&r;›sR%
 ,''115<ÿ
$ÿ
S!!.
 
U
r;)r)1r¾Ú
__future__rrÔrÒrÝrrrÚ    itertoolsrrrÚmathr    Úoperatorr
r r r rrrrÚtextwraprÚcollections.abcrrÚ ImportErrorÚ collectionsÚ    functoolsrrrrsrruÚthreadrÚ dummy_threadrÚ_threadÚ _dummy_threadr+r,rèr;ZSortedListWithKeyr%r%r%r&Ú<module>sd  (   
 
MB