-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
1566 lines (1386 loc) · 49 KB
/
Main.java
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
//package start;
import java.io.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.*;
import java.util.function.BiFunction;
public class Main extends IO {
public static void main(String[] args) throws Exception {
}
}
class math {
protected static long remains = 0x3B9ACA07; // 1000000007
static Integer[] convertBase(int value, int toBase) {
if (toBase == 1) {
throw new InputMismatchException("base = 1");
}
List<Integer> result = new ArrayList<>();
while (value / toBase >= 1) {
result.add(0, value % toBase);
value /= toBase;
}
result.add(0, value % toBase);
return result.toArray(Integer[]::new);
}
protected static int gcd(int a, int b) { // NOD
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static float gcd(float a, float b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static double gcd(double a, double b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static double lcm(double a, double b) { // NOK
return a / gcd(a, b) * b;
}
protected static float lcm(float a, float b) { // NOK
return a / gcd(a, b) * b;
}
protected static int lcm(int a, int b) { // NOK
return a / gcd(a, b) * b;
}
protected static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
protected static long mult(long a, long b) {
return ((a % remains) * (b % remains)) % remains;
}
protected static long binpow(long base, long power) {
long res = 1;
while (power != 0)
if ((power & 1) == 1) {
res = mult(res, base);
power--;
} else {
base = mult(base, base);
power >>= 1;
}
return res;
}
protected static double log(double value, int base) {
return Math.log(value) / Math.log(base);
}
protected static long factorial(int number) {
if (number < 0) {
return 0;
}
return solveFactorial(number);
}
private static long solveFactorial(int number) {
if (number > 0) {
return solveFactorial(number - 1) * number;
}
return 1;
}
static boolean[] solveEratosfen(int count) {
boolean[] isPrimes = new boolean[count];
Arrays.fill(isPrimes, true);
isPrimes[0] = false;
isPrimes[1] = false;
for (int i = 2; i < isPrimes.length; i++) {
if (isPrimes[i]) {
for (int j = 2; i * j < isPrimes.length; j++) {
isPrimes[i * j] = false;
}
}
}
return isPrimes;
}
}
class ArraySegments<T extends Comparable<? super T>> {
private List<Pair<T, T>> geps;
ArraySegments() {
this.geps = new ArrayList<>();
}
void addGep(T start, T stop) {
int indexStart = indexOf(start);
int indexStop = indexOf(stop);
if (indexStart != -1 && indexStop == -1) {
this.geps.get(indexStart).setSecond(stop);
T startGep = this.geps.get(indexStart).getFirstElement();
T stopGep = this.geps.get(indexStart).getSecondElement();
int i = indexStart + 1;
while (i < geps.size()) {
if (this.geps.get(i).getFirstElement().compareTo(startGep) >= 0 && this.geps.get(i).getSecondElement().compareTo(stopGep) <= 0) {
this.geps.remove(i);
} else {
break;
}
}
} else if (indexStart != -1 && indexStop != -1) {
this.geps.get(indexStart).setSecond(this.geps.get(indexStop).getSecondElement());
if (indexStart == indexStop) {
return;
}
this.geps.remove(indexStop);
T startGep = this.geps.get(indexStart).getFirstElement();
T stopGep = this.geps.get(indexStart).getSecondElement();
int i = indexStart + 1;
while (i < this.geps.size()) {
if (this.geps.get(i).getFirstElement().compareTo(startGep) >= 0 && this.geps.get(i).getSecondElement().compareTo(stopGep) <= 0) {
this.geps.remove(i);
} else {
break;
}
}
} else if (indexStart == -1 && indexStop != -1) {
this.geps.get(indexStop).setFirst(start);
T startGep = this.geps.get(indexStop).getFirstElement();
T stopGep = this.geps.get(indexStop).getSecondElement();
int i = indexStop - 1;
while (i >= 0) {
if (this.geps.get(i).getFirstElement().compareTo(startGep) >= 0 && this.geps.get(i).getSecondElement().compareTo(stopGep) <= 0) {
this.geps.remove(i);
} else {
break;
}
i--;
}
} else if (indexStart == -1 && indexStop == -1) {
if (this.geps.size() != 0) {
int l = 0;
int r = this.geps.size();
while (l + 1 != r) {
int mid = (l + r) >> 1;
if (this.geps.get(mid).getFirstElement().compareTo(start) < 0) {
l = mid;
} else {
r = mid;
}
}
this.geps.add(l + 1, Pair.createPair(start, stop));
indexStart = l + 1;
} else {
this.geps.add(Pair.createPair(start, stop));
return;
}
int i = indexStart + 1;
while (i < this.geps.size()) {
if (this.geps.get(i).getFirstElement().compareTo(start) >= 0 && this.geps.get(i).getSecondElement().compareTo(stop) <= 0) {
this.geps.remove(i);
} else {
break;
}
}
}
}
int indexOf(T value) {
if (geps.size() == 0) {
return -1;
}
int l = 0;
int r = geps.size();
while (l + 1 != r) {
int mid = (l + r) >> 1;
if (geps.get(mid).getFirstElement().compareTo(value) <= 0) {
l = mid;
} else {
r = mid;
}
}
if (geps.get(l).getFirstElement().compareTo(value) <= 0 && geps.get(l).getSecondElement().compareTo(value) >= 0) {
return l;
}
return -1;
}
void clear() {
this.geps.clear();
}
}
class Int implements Comparable<Integer> {
protected int value;
Int(int value) {
this.value = value;
}
public Int add(int value) {
this.value += value;
return this;
}
@Override
public int compareTo(Integer o) {
return (this.value < o) ? -1 : ((this.value == o) ? 0 : 1);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == (Integer) obj;
}
return false;
}
@Override
public int hashCode() {
return value;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
}
class Fraction<T extends Number> extends Pair {
private Fraction(T dividend, T divider) {
super(dividend, divider);
reduce();
}
protected static <T extends Number> Fraction<T> createFraction(T dividend, T divider) {
return new Fraction<>(dividend, divider);
}
protected void reduce() {
if (getFirstElement() instanceof Integer) {
Integer Dividend = (Integer) getFirstElement();
Integer Divider = (Integer) getSecondElement();
int gcd = math.gcd(Dividend, Divider);
setFirst(Dividend / gcd);
setSecond(Divider / gcd);
} else if (getFirstElement() instanceof Long) {
Long Dividend = (Long) getFirstElement();
Long Divider = (Long) getSecondElement();
long gcd = math.gcd(Dividend, Divider);
setFirst(Dividend / gcd);
setSecond(Divider / gcd);
} else if (getFirstElement() instanceof Float) {
Float Dividend = (Float) getFirstElement();
Float Divider = (Float) getSecondElement();
float gcd = math.gcd(Dividend, Divider);
setFirst(Dividend / gcd);
setSecond(Divider / gcd);
} else if (getFirstElement() instanceof Double) {
Double Dividend = (Double) getFirstElement();
Double Divider = (Double) getSecondElement();
double gcd = math.gcd(Dividend, Divider);
setFirst(Dividend / gcd);
setSecond(Divider / gcd);
}
}
protected void addWithoutReturn(Fraction number) throws UnsupportedOperationException {
add(number, 0);
}
private Fraction add(Fraction number, int function) throws UnsupportedOperationException {
if (getFirstElement() instanceof Integer && number.getFirstElement() instanceof Integer) {
Integer Dividend = (Integer) getFirstElement();
Integer Divider = (Integer) getSecondElement();
Integer Dividend1 = (Integer) number.getFirstElement();
Integer Divider1 = (Integer) number.getSecondElement();
Integer lcm = math.lcm(Divider, Divider1);
if (function == 0) {
setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1);
setSecond(lcm);
reduce();
return null;
}
Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm);
result.reduce();
return result;
} else if (getFirstElement() instanceof Long && number.getFirstElement() instanceof Long) {
Long Dividend = (Long) getFirstElement();
Long Divider = (Long) getSecondElement();
Long Dividend1 = (Long) number.getFirstElement();
Long Divider1 = (Long) number.getSecondElement();
Long lcm = math.lcm(Divider, Divider1);
if (function == 0) {
setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1);
setSecond(lcm);
reduce();
return null;
}
Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm);
result.reduce();
return result;
} else if (getFirstElement() instanceof Float && number.getFirstElement() instanceof Float) {
Float Dividend = (Float) getFirstElement();
Float Divider = (Float) getSecondElement();
Float Dividend1 = (Float) number.getFirstElement();
Float Divider1 = (Float) number.getSecondElement();
Float lcm = math.lcm(Divider, Divider1);
if (function == 0) {
setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1);
setSecond(lcm);
reduce();
return null;
}
Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm);
result.reduce();
return result;
} else if (getFirstElement() instanceof Double && number.getFirstElement() instanceof Double) {
Double Dividend = (Double) getFirstElement();
Double Divider = (Double) getSecondElement();
Double Dividend1 = (Double) number.getFirstElement();
Double Divider1 = (Double) number.getSecondElement();
Double lcm = math.lcm(Divider, Divider1);
if (function == 0) {
setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1);
setSecond(lcm);
reduce();
return null;
}
Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm);
result.reduce();
return result;
} else {
throw new UnsupportedOperationException();
}
}
protected Fraction addWithReturn(Fraction number) {
return add(number, 1);
}
protected void multiplyWithoutReturn(Fraction number) throws UnsupportedOperationException {
multiply(number, 0);
}
protected Fraction multiplyWithReturn(Fraction number) throws UnsupportedOperationException {
return multiply(number, 1);
}
private Fraction multiply(Fraction number, int function) throws UnsupportedOperationException {
if (getFirstElement() instanceof Integer && number.getFirstElement() instanceof Integer) {
Integer first = (Integer) getFirstElement() * (Integer) number.getFirstElement();
Integer second = (Integer) getSecondElement() * (Integer) number.getSecondElement();
if (function == 0) {
setFirst(first);
setSecond(second);
reduce();
return null;
}
Fraction answer = Fraction.createFraction(first, second);
answer.reduce();
return answer;
} else if (getFirstElement() instanceof Long && number.getFirstElement() instanceof Long) {
Long first = (Long) getFirstElement() * (Long) number.getFirstElement();
Long second = (Long) getSecondElement() * (Long) number.getSecondElement();
if (function == 0) {
setFirst(first);
setSecond(second);
reduce();
return null;
}
Fraction answer = Fraction.createFraction(first, second);
answer.reduce();
return answer;
} else if (getFirstElement() instanceof Float && number.getFirstElement() instanceof Float) {
Float first = (Float) getFirstElement() * (Float) number.getFirstElement();
Float second = (Float) getSecondElement() * (Float) number.getSecondElement();
if (function == 0) {
setFirst(first);
setSecond(second);
reduce();
return null;
}
Fraction answer = Fraction.createFraction(first, second);
answer.reduce();
return answer;
} else if (getFirstElement() instanceof Double && number.getFirstElement() instanceof Double) {
Double first = (Double) getFirstElement() * (Double) number.getFirstElement();
Double second = (Double) getSecondElement() * (Double) number.getSecondElement();
if (function == 0) {
setFirst(first);
setSecond(second);
reduce();
return null;
}
Fraction answer = Fraction.createFraction(first, second);
answer.reduce();
return answer;
} else {
throw new UnsupportedOperationException();
}
}
}
class Pair<T, T1> implements Cloneable {
private T first;
private T1 second;
Pair(T obj, T1 obj1) {
first = obj;
second = obj1;
}
protected static <T, T1> Pair<T, T1> createPair(T element, T1 element1) {
return new Pair<>(element, element1);
}
protected T getFirstElement() {
return first;
}
protected T1 getSecondElement() {
return second;
}
protected void setFirst(T element) {
first = element;
}
protected void setSecond(T1 element) {
second = element;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Pair)) {
return false;
}
Pair pair = (Pair) obj;
return first.equals(pair.first) && second.equals(pair.second);
}
@Override
public int hashCode() {
int hashCode = 1;
hashCode = 31 * hashCode + (first == null ? 0 : first.hashCode());
return 31 * hashCode + (second == null ? 0 : second.hashCode());
}
@Override
public Object clone() {
return Pair.createPair(first, second);
}
}
class Graph {
private int[][] base;
private boolean[] used;
private int quantity;
private Integer[] ancestor;
public int[][] getBase() {
return base;
}
public boolean[] getUsed() {
return used;
}
public int getQuantity() {
return quantity;
}
public Integer[] getAncestor() {
return ancestor;
}
public void setBase(int[][] base) {
this.base = base;
}
protected void start(int length) {
used = new boolean[length];
ancestor = new Integer[length];
Arrays.fill(ancestor, -1);
quantity = 0;
}
protected void edgesMatrixToDefault(int length, int inputQuantity, boolean readConsole, int[][] value) throws Exception {
base = new int[length][];
List<ArrayList<Integer>> inputBase = new ArrayList<>();
for (int i = 0; i < length; i++) {
inputBase.add(new ArrayList<>());
}
for (int i = 0; i < inputQuantity; i++) {
int[] input = readConsole ? IO.readArrayInt(" ") : value[i];
inputBase.get(input[0] - 1).add(input[1] - 1);
inputBase.get(input[0] - 1).add(input[2]); // price
inputBase.get(input[1] - 1).add(input[0] - 1);
inputBase.get(input[1] - 1).add(input[2]); // price
}
for (int i = 0; i < length; i++) {
base[i] = inputBase.get(i).stream().mapToInt(Integer::intValue).toArray();
}
start(length);
}
protected void adjacencyMatrixToDefault(int length, int not, boolean readConsole, int[][] value) throws Exception {
this.base = new int[length][];
List<Integer> buffer = new ArrayList<>();
for (int i = 0; i < length; i++) {
int[] InputArray = readConsole ? IO.readArrayInt(" ") : value[i];
for (int index = 0; index < length; index++) {
if (i != index && InputArray[index] != not) {
buffer.add(index);
// buffer.add(InputArray[index]); // price
}
}
this.base[i] = buffer.stream().mapToInt(Integer::intValue).toArray();
buffer.clear();
}
start(length);
}
protected int dfs(int position) throws Exception {
used[position] = true;
int next;
int count = 0;
for (int index = 0; index < base[position].length; index += 2) {
next = base[position][index];
if (!used[next]) {
ancestor[next] = position;
count += dfs(next);
}
/*else {
if (next != ancestor[position]) { // if cycle
throw new Exception();
}
}*/
}
if (base[position].length == 2 && base[position][0] == ancestor[position]) {
return 1;
}
return count;
}
protected int dijkstra(int start, int stop, int size) {
start--;
stop--;
int[] dist = new int[size];
for (int i = 0; i < size; i++) {
if (i != start) {
dist[i] = Integer.MAX_VALUE;
}
ancestor[i] = start;
}
Queue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt((int[] ints) -> ints[1]));
queue.add(new int[]{start, 0});
int position;
int[] getQueue;
while (queue.size() != 0) {
getQueue = queue.poll();
position = getQueue[0];
if (getQueue[1] > dist[position]) {
continue;
}
for (int index = 0; index < this.base[position].length; index += 2) {
if (dist[position] + this.base[position][index + 1] < dist[this.base[position][index]] && !this.used[this.base[position][index]]) {
dist[this.base[position][index]] = dist[position] + this.base[position][index + 1];
this.ancestor[this.base[position][index]] = position;
queue.add(new int[]{this.base[position][index], dist[this.base[position][index]]});
}
}
used[position] = true;
}
return dist[stop] == Integer.MAX_VALUE ? -1 : dist[stop];
}
protected static boolean solveFloydWarshall(int[][] base, int length, int not) {
for (int k = 0; k < length; k++) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (base[i][k] == not || base[k][j] == not) {
continue;
}
int total = base[i][k] + base[k][j];
if (base[i][j] != not) {
base[i][j] = Math.min(base[i][j], total);
} else {
base[i][j] = total;
}
}
}
}
for (int index = 0; index < length; index++) {
if (base[index][index] != 0) { // if cycle
return false;
}
}
return true;
}
protected static Pair<Long, int[][]> solveKruskal(int[][] edgesMatrix, final int countVertex, final int indexSort) {
int[][] answer = new int[countVertex - 1][2];
long sum = 0;
Arrays.sort(edgesMatrix, Comparator.comparingInt(value -> value[indexSort]));
SystemOfDisjointSets dsu = new SystemOfDisjointSets(countVertex);
for (int i = 0; i < countVertex; i++) {
dsu.makeSet(i);
}
int index = 0;
for (int[] value : edgesMatrix) {
if (dsu.mergeSets(value[0], value[1])) {
sum += value[indexSort];
answer[index] = new int[]{value[0], value[1]};
index++;
}
}
if (index < countVertex - 1) {
return Pair.createPair(null, null);
}
return Pair.createPair(sum, answer);
}
static class SegmentTree {
private int[] segmentArray;
private BiFunction<Integer, Integer, Integer> function;
protected void setSegmentArray(int[] segmentArray) {
this.segmentArray = segmentArray;
}
protected int[] getSegmentArray() {
return segmentArray.clone();
}
protected void setFunction(BiFunction<Integer, Integer, Integer> function) {
this.function = function;
}
protected BiFunction<Integer, Integer, Integer> getFunction() {
return function;
}
SegmentTree() {
}
SegmentTree(int[] startBase, int neutral, BiFunction<Integer, Integer, Integer> function) {
this.function = function;
int length = startBase.length;
int[] base;
if ((length & (length - 1)) != 0) {
int pow = 0;
while (length > 0) {
length >>= 1;
pow++;
}
pow--;
base = new int[2 << pow];
System.arraycopy(startBase, 0, base, 0, startBase.length);
Arrays.fill(base, startBase.length, base.length, neutral);
} else {
base = startBase;
}
segmentArray = new int[base.length << 1]; // maybe * 4
Arrays.fill(segmentArray, neutral);
inDepth(base, 1, 0, base.length - 1);
}
private void inDepth(int[] base, int position, int low, int high) {
if (low == high) {
segmentArray[position] = base[low];
} else {
int mid = (low + high) >> 1;
inDepth(base, position << 1, low, mid);
inDepth(base, (position << 1) + 1, mid + 1, high);
segmentArray[position] = function.apply(segmentArray[position << 1], segmentArray[(position << 1) + 1]);
}
}
protected int getValue(int left, int right, int neutral) {
return findValue(1, 0, ((segmentArray.length) >> 1) - 1, left, right, neutral);
}
private int findValue(int position, int low, int high, int left, int right, int neutral) {
if (left > right) {
return neutral;
}
if (left == low && right == high) {
return segmentArray[position];
}
int mid = (low + high) >> 1;
return function.apply(findValue(position << 1, low, mid, left, Math.min(right, mid), neutral),
findValue((position << 1) + 1, mid + 1, high, Math.max(left, mid + 1), right, neutral));
}
protected void replaceValue(int index, int value) {
update(1, 0, (segmentArray.length >> 1) - 1, index, value);
}
private void update(int position, int low, int high, int index, int value) {
if (low == high) {
segmentArray[position] = value;
} else {
int mid = (low + high) >> 1;
if (index <= mid) {
update(position << 1, low, mid, index, value);
} else {
update((position << 1) + 1, mid + 1, high, index, value);
}
segmentArray[position] = function.apply(segmentArray[position << 1], segmentArray[(position << 1) + 1]);
}
}
}
static class SegmentTreeGeneric<T> {
private Object[] segmentArray;
private BiFunction<T, T, T> function;
protected void setSegmentArray(T[] segmentArray) {
this.segmentArray = segmentArray;
}
protected Object getSegmentArray() {
return segmentArray.clone();
}
protected void setFunction(BiFunction<T, T, T> function) {
this.function = function;
}
protected BiFunction<T, T, T> getFunction() {
return function;
}
SegmentTreeGeneric() {
}
SegmentTreeGeneric(T[] startBase, T neutral, BiFunction<T, T, T> function) {
this.function = function;
int length = startBase.length;
Object[] base;
if ((length & (length - 1)) != 0) {
int pow = 0;
while (length > 0) {
length >>= 1;
pow++;
}
pow--;
base = new Object[2 << pow];
System.arraycopy(startBase, 0, base, 0, startBase.length);
Arrays.fill(base, startBase.length, base.length, neutral);
} else {
base = startBase;
}
segmentArray = new Object[base.length << 1]; // maybe * 4
Arrays.fill(segmentArray, neutral);
inDepth(base, 1, 0, base.length - 1);
}
private void inDepth(Object[] base, int position, int low, int high) {
if (low == high) {
segmentArray[position] = base[low];
} else {
int mid = (low + high) >> 1;
inDepth(base, position << 1, low, mid);
inDepth(base, (position << 1) + 1, mid + 1, high);
segmentArray[position] = function.apply((T) segmentArray[position << 1], (T) segmentArray[(position << 1) + 1]);
}
}
protected T getValue(int left, int right, T neutral) {
return findValue(1, 0, ((segmentArray.length) >> 1) - 1, left, right, neutral);
}
private T findValue(int position, int low, int high, int left, int right, T neutral) {
if (left > right) {
return neutral;
}
if (left == low && right == high) {
return (T) segmentArray[position];
}
int mid = (low + high) >> 1;
return function.apply(findValue(position << 1, low, mid, left, Math.min(right, mid), neutral),
findValue((position << 1) + 1, mid + 1, high, Math.max(left, mid + 1), right, neutral));
}
protected void replaceValue(int index, T value) {
update(1, 0, (segmentArray.length >> 1) - 1, index, value);
}
private void update(int position, int low, int high, int index, T value) {
if (low == high) {
segmentArray[position] = value;
} else {
int mid = (low + high) >> 1;
if (index <= mid) {
update(position << 1, low, mid, index, value);
} else {
update((position << 1) + 1, mid + 1, high, index, value);
}
segmentArray[position] = function.apply((T) segmentArray[position << 1], (T) segmentArray[(position << 1) + 1]);
}
}
}
}
class SystemOfDisjointSets {
private int[] rank;
private int[] dsu;
SystemOfDisjointSets(int size) {
this.rank = new int[size];
this.dsu = new int[size];
}
protected void makeSet(int value) {
dsu[value] = value;
rank[value] = 0;
}
protected int findSet(int value) {
if (value == dsu[value]) {
return value;
}
return dsu[value] = findSet(dsu[value]);
}
protected boolean mergeSets(int first, int second) {
first = findSet(first);
second = findSet(second);
if (first != second) {
if (rank[first] < rank[second]) {
int number = first;
first = second;
second = number;
}
dsu[second] = first;
if (rank[first] == rank[second]) {
rank[first]++;
}
return true;
}
return false;
}
}
interface Array {
void useArray(int[] a);
}
interface Method<T> {
void use(T value);
}
class BigNumber {
private static int size = 100;
private static int[] pr = new int[size];
private static int[][] r = new int[size][size];
private int[] a = new int[size];
static void init() {
for (int x = 1000_000_000, i = 0; i < size; ++x)
if (BigInteger.valueOf(x).isProbablePrime(100))
pr[i++] = x;
for (int i = 0; i < size; ++i)
for (int j = i + 1; j < size; ++j)
r[i][j] = BigInteger.valueOf(pr[i]).modInverse(
BigInteger.valueOf(pr[j])).intValue();
}
public BigNumber() {
}
public BigNumber(int n) {
for (int i = 0; i < size; ++i)
a[i] = n % pr[i];
}
public BigNumber(BigInteger n) {
for (int i = 0; i < size; ++i)
a[i] = n.mod(BigInteger.valueOf(pr[i])).intValue();
}
public BigNumber add(BigNumber n) {
BigNumber result = new BigNumber();
for (int i = 0; i < size; ++i)
result.a[i] = (a[i] + n.a[i]) % pr[i];
return result;
}
public BigNumber subtract(BigNumber n) {
BigNumber result = new BigNumber();
for (int i = 0; i < size; ++i)
result.a[i] = (a[i] - n.a[i] + pr[i]) % pr[i];
return result;
}
public BigNumber multiply(BigNumber n) {
BigNumber result = new BigNumber();
for (int i = 0; i < size; ++i)
result.a[i] = (int) (((long) a[i] * n.a[i]) % pr[i]);
return result;
}
public BigInteger bigIntegerValue(boolean can_be_negative) {
BigInteger result = BigInteger.ZERO,
mult = BigInteger.ONE;
int[] x = new int[size];
for (int i = 0; i < size; ++i) {
x[i] = a[i];
for (int j = 0; j < i; ++j) {
long cur = (long) (x[i] - x[j]) * r[j][i];
x[i] = (int) ((cur % pr[i] + pr[i]) % pr[i]);
}
result = result.add(mult.multiply(BigInteger.valueOf(x[i])));
mult = mult.multiply(BigInteger.valueOf(pr[i]));
}
if (can_be_negative)
if (result.compareTo(mult.shiftRight(1)) >= 0)
result = result.subtract(mult);
return result;
}
}
class FastSort {
enum TypeSort {
RANDOM,
SHELL,
HEAP,
MERGE,
STRAIGHT_MERGE,
INSERTION