package kickstart2020.c

// TODO still slow
fun main() {
    val inputFileName = "src/kickstart2020/c/Candies.in"
    java.io.File(inputFileName).takeIf { it.exists() }?.also { System.setIn(it.inputStream()) }

    for (case in 1..readLine()!!.toInt()) {
        val (_, q) = readLine()!!.split(' ').map { it.toInt() }
        val a = readLine()!!.split(' ').map { it.toInt() }.toIntArray()

        val divider = Math.min(a.size, 330)

        val calcSection: (Int) -> Pair<Long, Long> = { section ->
            var sum1 = 0L
            var sum2 = 0L
            var mul = 1

            for (i in 0 until divider) {
                sum1 += mul * (i + 1) * a[section * divider + i]
                sum2 += mul * a[section * divider + i]
                mul = -mul
            }

            sum1 to sum2
        }

        val cache = (0 until a.size / divider).map(calcSection).toTypedArray()

        var result = 0L

        repeat(q) {
            val operation = readLine()!!.split(' ')

            when (operation[0]) {
                "Q" -> {
                    val (l, r) = operation.slice(1..2).map { it.toInt() }
                    val startSection = (l + divider - 2) / divider
                    val endSection = r / divider
                    var mul = 1 // ok since divider is even

                    if (((l - 1) % divider > 0) || (startSection >= endSection)) {
                        val r0 = if (startSection >= endSection) r else startSection * divider

                        for (i in l..r0) {
                            result += mul * (i - l + 1) * a[i - 1]
                            mul = -mul
                        }
                    }

                    for (section in startSection until endSection) {
                        result += mul * cache[section].let { it.first + (section * divider - l + 1) * it.second }
                    }

                    if ((r % divider > 0) && (startSection < endSection)) {
                        val l0 = endSection * divider + 1

                        for (i in l0..r) {
                            result += mul * (i - l + 1) * a[i - 1]
                            mul = -mul
                        }
                    }
                }

                "U" -> {
                    val (x, v) = operation.slice(1..2).map { it.toInt() }
                    val section = (x - 1) / divider
                    a[x - 1] = v
                    cache[section] = calcSection(section)
                }
            }
        }

        println("Case #$case: $result")
    }
}

Compiled from "Candies.kt"
public final class kickstart2020.c.CandiesKt {
  public static final void main();
    Code:
       0: ldc           #8                  // String src/kickstart2020/c/Candies.in
       2: astore_0
       3: new           #10                 // class java/io/File
       6: dup
       7: aload_0
       8: invokespecial #14                 // Method java/io/File."<init>":(Ljava/lang/String;)V
      11: astore_2
      12: aload_2
      13: astore_3
      14: iconst_0
      15: istore        4
      17: aload_3
      18: invokevirtual #18                 // Method java/io/File.exists:()Z
      21: ifeq          28
      24: aload_2
      25: goto          29
      28: aconst_null
      29: dup
      30: ifnull        56
      33: astore_2
      34: aload_2
      35: astore_3
      36: iconst_0
      37: istore        4
      39: new           #20                 // class java/io/FileInputStream
      42: dup
      43: aload_3
      44: invokespecial #23                 // Method java/io/FileInputStream."<init>":(Ljava/io/File;)V
      47: checkcast     #25                 // class java/io/InputStream
      50: invokestatic  #31                 // Method java/lang/System.setIn:(Ljava/io/InputStream;)V
      53: goto          58
      56: pop
      57: nop
      58: iconst_1
      59: istore_1
      60: invokestatic  #37                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
      63: dup
      64: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      67: invokestatic  #49                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      70: istore_2
      71: iload_1
      72: iload_2
      73: if_icmpgt     1264
      76: invokestatic  #37                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
      79: dup
      80: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      83: checkcast     #51                 // class java/lang/CharSequence
      86: iconst_1
      87: newarray       char
      89: astore        4
      91: aload         4
      93: iconst_0
      94: bipush        32
      96: castore
      97: aload         4
      99: iconst_0
     100: iconst_0
     101: bipush        6
     103: aconst_null
     104: invokestatic  #57                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
     107: checkcast     #59                 // class java/lang/Iterable
     110: astore        4
     112: iconst_0
     113: istore        5
     115: aload         4
     117: astore        6
     119: new           #61                 // class java/util/ArrayList
     122: dup
     123: aload         4
     125: bipush        10
     127: invokestatic  #67                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     130: invokespecial #70                 // Method java/util/ArrayList."<init>":(I)V
     133: checkcast     #72                 // class java/util/Collection
     136: astore        7
     138: iconst_0
     139: istore        8
     141: aload         6
     143: invokeinterface #76,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     148: astore        9
     150: aload         9
     152: invokeinterface #81,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     157: ifeq          204
     160: aload         9
     162: invokeinterface #85,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     167: astore        10
     169: aload         7
     171: aload         10
     173: checkcast     #87                 // class java/lang/String
     176: astore        11
     178: astore        32
     180: iconst_0
     181: istore        12
     183: aload         11
     185: invokestatic  #49                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     188: nop
     189: invokestatic  #91                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     192: aload         32
     194: swap
     195: invokeinterface #95,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     200: pop
     201: goto          150
     204: aload         7
     206: checkcast     #97                 // class java/util/List
     209: nop
     210: iconst_1
     211: invokeinterface #101,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     216: checkcast     #103                // class java/lang/Number
     219: invokevirtual #107                // Method java/lang/Number.intValue:()I
     222: istore        4
     224: invokestatic  #37                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
     227: dup
     228: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     231: checkcast     #51                 // class java/lang/CharSequence
     234: iconst_1
     235: newarray       char
     237: astore        6
     239: aload         6
     241: iconst_0
     242: bipush        32
     244: castore
     245: aload         6
     247: iconst_0
     248: iconst_0
     249: bipush        6
     251: aconst_null
     252: invokestatic  #57                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
     255: checkcast     #59                 // class java/lang/Iterable
     258: astore        6
     260: iconst_0
     261: istore        7
     263: aload         6
     265: astore        8
     267: new           #61                 // class java/util/ArrayList
     270: dup
     271: aload         6
     273: bipush        10
     275: invokestatic  #67                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     278: invokespecial #70                 // Method java/util/ArrayList."<init>":(I)V
     281: checkcast     #72                 // class java/util/Collection
     284: astore        9
     286: iconst_0
     287: istore        10
     289: aload         8
     291: invokeinterface #76,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     296: astore        11
     298: aload         11
     300: invokeinterface #81,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     305: ifeq          352
     308: aload         11
     310: invokeinterface #85,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     315: astore        12
     317: aload         9
     319: aload         12
     321: checkcast     #87                 // class java/lang/String
     324: astore        13
     326: astore        32
     328: iconst_0
     329: istore        14
     331: aload         13
     333: invokestatic  #49                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     336: nop
     337: invokestatic  #91                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     340: aload         32
     342: swap
     343: invokeinterface #95,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     348: pop
     349: goto          298
     352: aload         9
     354: checkcast     #97                 // class java/util/List
     357: nop
     358: checkcast     #72                 // class java/util/Collection
     361: invokestatic  #111                // Method kotlin/collections/CollectionsKt.toIntArray:(Ljava/util/Collection;)[I
     364: astore        5
     366: aload         5
     368: arraylength
     369: sipush        330
     372: invokestatic  #117                // Method java/lang/Math.min:(II)I
     375: istore        6
     377: iload         6
     379: aload         5
     381: invokedynamic #137,  0            // InvokeDynamic #0:invoke:(I[I)Lkotlin/jvm/functions/Function1;
     386: astore        7
     388: iconst_0
     389: aload         5
     391: arraylength
     392: iload         6
     394: idiv
     395: invokestatic  #143                // Method kotlin/ranges/RangesKt.until:(II)Lkotlin/ranges/IntRange;
     398: checkcast     #59                 // class java/lang/Iterable
     401: astore        9
     403: iconst_0
     404: istore        10
     406: aload         9
     408: astore        11
     410: new           #61                 // class java/util/ArrayList
     413: dup
     414: aload         9
     416: bipush        10
     418: invokestatic  #67                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     421: invokespecial #70                 // Method java/util/ArrayList."<init>":(I)V
     424: checkcast     #72                 // class java/util/Collection
     427: astore        12
     429: iconst_0
     430: istore        13
     432: aload         11
     434: invokeinterface #76,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     439: astore        14
     441: aload         14
     443: invokeinterface #81,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     448: ifeq          480
     451: aload         14
     453: invokeinterface #85,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     458: astore        15
     460: aload         12
     462: aload         7
     464: aload         15
     466: invokeinterface #147,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
     471: invokeinterface #95,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     476: pop
     477: goto          441
     480: aload         12
     482: checkcast     #97                 // class java/util/List
     485: nop
     486: checkcast     #72                 // class java/util/Collection
     489: astore        9
     491: nop
     492: iconst_0
     493: istore        10
     495: aload         9
     497: astore        11
     499: aload         11
     501: iconst_0
     502: anewarray     #149                // class kotlin/Pair
     505: invokeinterface #153,  2          // InterfaceMethod java/util/Collection.toArray:([Ljava/lang/Object;)[Ljava/lang/Object;
     510: checkcast     #155                // class "[Lkotlin/Pair;"
     513: astore        8
     515: lconst_0
     516: lstore        33
     518: iconst_0
     519: istore        10
     521: iload         10
     523: iload         4
     525: if_icmpge     1217
     528: iload         10
     530: istore        11
     532: iconst_0
     533: istore        12
     535: invokestatic  #37                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
     538: dup
     539: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     542: checkcast     #51                 // class java/lang/CharSequence
     545: iconst_1
     546: newarray       char
     548: astore        13
     550: aload         13
     552: iconst_0
     553: bipush        32
     555: castore
     556: aload         13
     558: iconst_0
     559: iconst_0
     560: bipush        6
     562: aconst_null
     563: invokestatic  #57                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
     566: astore        14
     568: aload         14
     570: iconst_0
     571: invokeinterface #101,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     576: checkcast     #87                 // class java/lang/String
     579: astore        13
     581: aload         13
     583: ldc           #157                // String Q
     585: invokestatic  #161                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     588: ifeq          1014
     591: aload         14
     593: new           #163                // class kotlin/ranges/IntRange
     596: dup
     597: iconst_1
     598: iconst_2
     599: invokespecial #166                // Method kotlin/ranges/IntRange."<init>":(II)V
     602: invokestatic  #170                // Method kotlin/collections/CollectionsKt.slice:(Ljava/util/List;Lkotlin/ranges/IntRange;)Ljava/util/List;
     605: checkcast     #59                 // class java/lang/Iterable
     608: astore        15
     610: iconst_0
     611: istore        16
     613: aload         15
     615: astore        17
     617: new           #61                 // class java/util/ArrayList
     620: dup
     621: aload         15
     623: bipush        10
     625: invokestatic  #67                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     628: invokespecial #70                 // Method java/util/ArrayList."<init>":(I)V
     631: checkcast     #72                 // class java/util/Collection
     634: astore        18
     636: iconst_0
     637: istore        19
     639: aload         17
     641: invokeinterface #76,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     646: astore        20
     648: aload         20
     650: invokeinterface #81,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     655: ifeq          702
     658: aload         20
     660: invokeinterface #85,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     665: astore        21
     667: aload         18
     669: aload         21
     671: checkcast     #87                 // class java/lang/String
     674: astore        22
     676: astore        23
     678: iconst_0
     679: istore        24
     681: aload         22
     683: invokestatic  #49                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     686: nop
     687: invokestatic  #91                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     690: aload         23
     692: swap
     693: invokeinterface #95,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     698: pop
     699: goto          648
     702: aload         18
     704: checkcast     #97                 // class java/util/List
     707: nop
     708: astore        25
     710: aload         25
     712: iconst_0
     713: invokeinterface #101,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     718: checkcast     #103                // class java/lang/Number
     721: invokevirtual #107                // Method java/lang/Number.intValue:()I
     724: istore        15
     726: aload         25
     728: iconst_1
     729: invokeinterface #101,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     734: checkcast     #103                // class java/lang/Number
     737: invokevirtual #107                // Method java/lang/Number.intValue:()I
     740: istore        16
     742: iload         15
     744: iload         6
     746: iadd
     747: iconst_2
     748: isub
     749: iload         6
     751: idiv
     752: istore        17
     754: iload         16
     756: iload         6
     758: idiv
     759: istore        18
     761: iconst_1
     762: istore        19
     764: iload         15
     766: iconst_1
     767: isub
     768: iload         6
     770: irem
     771: ifgt          781
     774: iload         17
     776: iload         18
     778: if_icmplt     853
     781: iload         17
     783: iload         18
     785: if_icmplt     793
     788: iload         16
     790: goto          798
     793: iload         17
     795: iload         6
     797: imul
     798: istore        20
     800: iload         15
     802: istore        21
     804: iload         21
     806: iload         20
     808: if_icmpgt     853
     811: lload         33
     813: iload         19
     815: iload         21
     817: iload         15
     819: isub
     820: iconst_1
     821: iadd
     822: imul
     823: aload         5
     825: iload         21
     827: iconst_1
     828: isub
     829: iaload
     830: imul
     831: i2l
     832: ladd
     833: lstore        33
     835: iload         19
     837: ineg
     838: istore        19
     840: iload         21
     842: iload         20
     844: if_icmpeq     853
     847: iinc          21, 1
     850: goto          811
     853: iload         17
     855: istore        20
     857: iload         20
     859: iload         18
     861: if_icmpge     937
     864: lload         33
     866: iload         19
     868: i2l
     869: aload         8
     871: iload         20
     873: aaload
     874: astore        22
     876: lstore        26
     878: lstore        28
     880: iconst_0
     881: istore        24
     883: aload         22
     885: invokevirtual #173                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     888: checkcast     #103                // class java/lang/Number
     891: invokevirtual #177                // Method java/lang/Number.longValue:()J
     894: iload         20
     896: iload         6
     898: imul
     899: iload         15
     901: isub
     902: iconst_1
     903: iadd
     904: i2l
     905: aload         22
     907: invokevirtual #180                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     910: checkcast     #103                // class java/lang/Number
     913: invokevirtual #177                // Method java/lang/Number.longValue:()J
     916: lmul
     917: ladd
     918: lstore        30
     920: lload         28
     922: lload         26
     924: lload         30
     926: nop
     927: lmul
     928: ladd
     929: lstore        33
     931: iinc          20, 1
     934: goto          857
     937: iload         16
     939: iload         6
     941: irem
     942: ifle          1210
     945: iload         17
     947: iload         18
     949: if_icmpge     1210
     952: iload         18
     954: iload         6
     956: imul
     957: iconst_1
     958: iadd
     959: istore        20
     961: iload         20
     963: istore        21
     965: iload         21
     967: iload         16
     969: if_icmpgt     1210
     972: lload         33
     974: iload         19
     976: iload         21
     978: iload         15
     980: isub
     981: iconst_1
     982: iadd
     983: imul
     984: aload         5
     986: iload         21
     988: iconst_1
     989: isub
     990: iaload
     991: imul
     992: i2l
     993: ladd
     994: lstore        33
     996: iload         19
     998: ineg
     999: istore        19
    1001: iload         21
    1003: iload         16
    1005: if_icmpeq     1210
    1008: iinc          21, 1
    1011: goto          972
    1014: aload         13
    1016: ldc           #182                // String U
    1018: invokestatic  #161                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
    1021: ifeq          1210
    1024: aload         14
    1026: new           #163                // class kotlin/ranges/IntRange
    1029: dup
    1030: iconst_1
    1031: iconst_2
    1032: invokespecial #166                // Method kotlin/ranges/IntRange."<init>":(II)V
    1035: invokestatic  #170                // Method kotlin/collections/CollectionsKt.slice:(Ljava/util/List;Lkotlin/ranges/IntRange;)Ljava/util/List;
    1038: checkcast     #59                 // class java/lang/Iterable
    1041: astore        15
    1043: iconst_0
    1044: istore        16
    1046: aload         15
    1048: astore        17
    1050: new           #61                 // class java/util/ArrayList
    1053: dup
    1054: aload         15
    1056: bipush        10
    1058: invokestatic  #67                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
    1061: invokespecial #70                 // Method java/util/ArrayList."<init>":(I)V
    1064: checkcast     #72                 // class java/util/Collection
    1067: astore        18
    1069: iconst_0
    1070: istore        19
    1072: aload         17
    1074: invokeinterface #76,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1079: astore        20
    1081: aload         20
    1083: invokeinterface #81,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1088: ifeq          1135
    1091: aload         20
    1093: invokeinterface #85,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1098: astore        21
    1100: aload         18
    1102: aload         21
    1104: checkcast     #87                 // class java/lang/String
    1107: astore        22
    1109: astore        23
    1111: iconst_0
    1112: istore        24
    1114: aload         22
    1116: invokestatic  #49                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
    1119: nop
    1120: invokestatic  #91                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1123: aload         23
    1125: swap
    1126: invokeinterface #95,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
    1131: pop
    1132: goto          1081
    1135: aload         18
    1137: checkcast     #97                 // class java/util/List
    1140: nop
    1141: astore        25
    1143: aload         25
    1145: iconst_0
    1146: invokeinterface #101,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
    1151: checkcast     #103                // class java/lang/Number
    1154: invokevirtual #107                // Method java/lang/Number.intValue:()I
    1157: istore        15
    1159: aload         25
    1161: iconst_1
    1162: invokeinterface #101,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
    1167: checkcast     #103                // class java/lang/Number
    1170: invokevirtual #107                // Method java/lang/Number.intValue:()I
    1173: istore        16
    1175: iload         15
    1177: iconst_1
    1178: isub
    1179: iload         6
    1181: idiv
    1182: istore        17
    1184: aload         5
    1186: iload         15
    1188: iconst_1
    1189: isub
    1190: iload         16
    1192: iastore
    1193: aload         8
    1195: iload         17
    1197: aload         7
    1199: iload         17
    1201: invokestatic  #91                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1204: invokeinterface #147,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
    1209: aastore
    1210: nop
    1211: iinc          10, 1
    1214: goto          521
    1217: new           #184                // class java/lang/StringBuilder
    1220: dup
    1221: invokespecial #186                // Method java/lang/StringBuilder."<init>":()V
    1224: ldc           #188                // String Case #
    1226: invokevirtual #192                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
    1229: iload_1
    1230: invokevirtual #195                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
    1233: ldc           #197                // String :
    1235: invokevirtual #192                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
    1238: lload         33
    1240: invokevirtual #200                // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
    1243: invokevirtual #203                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
    1246: getstatic     #207                // Field java/lang/System.out:Ljava/io/PrintStream;
    1249: swap
    1250: invokevirtual #212                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
    1253: iload_1
    1254: iload_2
    1255: if_icmpeq     1264
    1258: iinc          1, 1
    1261: goto          76
    1264: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #265                // Method main:()V
       3: return

  private static final kotlin.Pair main$lambda$4(int, int[], int);
    Code:
       0: lconst_0
       1: lstore_3
       2: lconst_0
       3: lstore        5
       5: iconst_1
       6: istore        7
       8: iconst_0
       9: istore        8
      11: iload         8
      13: iload_0
      14: if_icmpge     65
      17: lload_3
      18: iload         7
      20: iload         8
      22: iconst_1
      23: iadd
      24: imul
      25: aload_1
      26: iload_2
      27: iload_0
      28: imul
      29: iload         8
      31: iadd
      32: iaload
      33: imul
      34: i2l
      35: ladd
      36: lstore_3
      37: lload         5
      39: iload         7
      41: aload_1
      42: iload_2
      43: iload_0
      44: imul
      45: iload         8
      47: iadd
      48: iaload
      49: imul
      50: i2l
      51: ladd
      52: lstore        5
      54: iload         7
      56: ineg
      57: istore        7
      59: iinc          8, 1
      62: goto          11
      65: lload_3
      66: invokestatic  #272                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      69: lload         5
      71: invokestatic  #272                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      74: invokestatic  #278                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      77: areturn
}


package tools.graph

class FloydWarshall(size: Int, weights: Map<Pair<Int, Int>, Double>) {
    val indices = 0 until size
    val dist = Array(size) { DoubleArray(size) { Double.POSITIVE_INFINITY } }
    val prev = Array(size) { Array<Int?>(size) { null } }

    override fun toString() = dist.toList().map { it.toList() }.toString()

    init {
        weights.forEach { (u, v), w ->
            dist[u][v] = w
            prev[u][v] = u
        }
        for (v in indices) {
            dist[v][v] = 0.0
            prev[v][v] = v
        }
        for (k in indices)
            for (i in indices)
                for (j in indices) {
                    val d = dist[i][k] + dist[k][j]
                    if (dist[i][j] > d) {
                        dist[i][j] = d
                        prev[i][j] = prev[k][j]
                    }
                }
    }

    fun path(u: Int, v: Int) = path(u, v, emptyList())

    private tailrec fun path(u: Int, v: Int?, path: List<Int>): List<Int> = when {
        v == null -> emptyList()
        u == v -> path
        else -> path(u, prev[u][v], listOf(v) + path)
    }
}

Compiled from "FloydWarshall.kt"
public final class tools.graph.FloydWarshall {
  private final kotlin.ranges.IntRange indices;

  private final double[][] dist;

  private final java.lang.Integer[][] prev;

  public tools.graph.FloydWarshall(int, java.util.Map<kotlin.Pair<java.lang.Integer, java.lang.Integer>, java.lang.Double>);
    Code:
       0: aload_2
       1: ldc           #10                 // String weights
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokespecial #19                 // Method java/lang/Object."<init>":()V
      10: aload_0
      11: iconst_0
      12: iload_1
      13: invokestatic  #25                 // Method kotlin/ranges/RangesKt.until:(II)Lkotlin/ranges/IntRange;
      16: putfield      #29                 // Field indices:Lkotlin/ranges/IntRange;
      19: aload_0
      20: iconst_0
      21: istore_3
      22: iload_1
      23: anewarray     #31                 // class "[D"
      26: astore        4
      28: astore        14
      30: iload_3
      31: iload_1
      32: if_icmpge     91
      35: iload_3
      36: istore        5
      38: aload         4
      40: iload         5
      42: iconst_0
      43: istore        6
      45: iload_1
      46: newarray       double
      48: astore        7
      50: istore        16
      52: astore        15
      54: iload         6
      56: iload_1
      57: if_icmpge     78
      60: iload         6
      62: istore        8
      64: aload         7
      66: iload         8
      68: ldc2_w        #32                 // double Infinityd
      71: dastore
      72: iinc          6, 1
      75: goto          54
      78: aload         15
      80: iload         16
      82: aload         7
      84: aastore
      85: iinc          3, 1
      88: goto          30
      91: aload         14
      93: aload         4
      95: putfield      #37                 // Field dist:[[D
      98: aload_0
      99: iconst_0
     100: istore_3
     101: iload_1
     102: anewarray     #39                 // class "[Ljava/lang/Integer;"
     105: astore        4
     107: astore        14
     109: iload_3
     110: iload_1
     111: if_icmpge     169
     114: iload_3
     115: istore        5
     117: aload         4
     119: iload         5
     121: iconst_0
     122: istore        6
     124: iload_1
     125: anewarray     #41                 // class java/lang/Integer
     128: astore        7
     130: istore        16
     132: astore        15
     134: iload         6
     136: iload_1
     137: if_icmpge     156
     140: iload         6
     142: istore        8
     144: aload         7
     146: iload         8
     148: aconst_null
     149: aastore
     150: iinc          6, 1
     153: goto          134
     156: aload         15
     158: iload         16
     160: aload         7
     162: aastore
     163: iinc          3, 1
     166: goto          109
     169: aload         14
     171: aload         4
     173: putfield      #45                 // Field prev:[[Ljava/lang/Integer;
     176: nop
     177: aload_2
     178: aload_0
     179: invokedynamic #65,  0             // InvokeDynamic #0:invoke:(Ltools/graph/FloydWarshall;)Lkotlin/jvm/functions/Function2;
     184: invokedynamic #76,  0             // InvokeDynamic #1:accept:(Lkotlin/jvm/functions/Function2;)Ljava/util/function/BiConsumer;
     189: invokeinterface #82,  2           // InterfaceMethod java/util/Map.forEach:(Ljava/util/function/BiConsumer;)V
     194: aload_0
     195: getfield      #29                 // Field indices:Lkotlin/ranges/IntRange;
     198: astore_3
     199: aload_3
     200: invokevirtual #88                 // Method kotlin/ranges/IntRange.getFirst:()I
     203: istore        4
     205: aload_3
     206: invokevirtual #91                 // Method kotlin/ranges/IntRange.getLast:()I
     209: istore        5
     211: iload         4
     213: iload         5
     215: if_icmpgt     257
     218: aload_0
     219: getfield      #37                 // Field dist:[[D
     222: iload         4
     224: aaload
     225: iload         4
     227: dconst_0
     228: dastore
     229: aload_0
     230: getfield      #45                 // Field prev:[[Ljava/lang/Integer;
     233: iload         4
     235: aaload
     236: iload         4
     238: iload         4
     240: invokestatic  #95                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     243: aastore
     244: iload         4
     246: iload         5
     248: if_icmpeq     257
     251: iinc          4, 1
     254: goto          218
     257: aload_0
     258: getfield      #29                 // Field indices:Lkotlin/ranges/IntRange;
     261: astore_3
     262: aload_3
     263: invokevirtual #88                 // Method kotlin/ranges/IntRange.getFirst:()I
     266: istore        4
     268: aload_3
     269: invokevirtual #91                 // Method kotlin/ranges/IntRange.getLast:()I
     272: istore        5
     274: iload         4
     276: iload         5
     278: if_icmpgt     445
     281: aload_0
     282: getfield      #29                 // Field indices:Lkotlin/ranges/IntRange;
     285: astore        6
     287: aload         6
     289: invokevirtual #88                 // Method kotlin/ranges/IntRange.getFirst:()I
     292: istore        7
     294: aload         6
     296: invokevirtual #91                 // Method kotlin/ranges/IntRange.getLast:()I
     299: istore        8
     301: iload         7
     303: iload         8
     305: if_icmpgt     432
     308: aload_0
     309: getfield      #29                 // Field indices:Lkotlin/ranges/IntRange;
     312: astore        9
     314: aload         9
     316: invokevirtual #88                 // Method kotlin/ranges/IntRange.getFirst:()I
     319: istore        10
     321: aload         9
     323: invokevirtual #91                 // Method kotlin/ranges/IntRange.getLast:()I
     326: istore        11
     328: iload         10
     330: iload         11
     332: if_icmpgt     419
     335: aload_0
     336: getfield      #37                 // Field dist:[[D
     339: iload         7
     341: aaload
     342: iload         4
     344: daload
     345: aload_0
     346: getfield      #37                 // Field dist:[[D
     349: iload         4
     351: aaload
     352: iload         10
     354: daload
     355: dadd
     356: dstore        12
     358: aload_0
     359: getfield      #37                 // Field dist:[[D
     362: iload         7
     364: aaload
     365: iload         10
     367: daload
     368: dload         12
     370: dcmpl
     371: ifle          406
     374: aload_0
     375: getfield      #37                 // Field dist:[[D
     378: iload         7
     380: aaload
     381: iload         10
     383: dload         12
     385: dastore
     386: aload_0
     387: getfield      #45                 // Field prev:[[Ljava/lang/Integer;
     390: iload         7
     392: aaload
     393: iload         10
     395: aload_0
     396: getfield      #45                 // Field prev:[[Ljava/lang/Integer;
     399: iload         4
     401: aaload
     402: iload         10
     404: aaload
     405: aastore
     406: iload         10
     408: iload         11
     410: if_icmpeq     419
     413: iinc          10, 1
     416: goto          335
     419: iload         7
     421: iload         8
     423: if_icmpeq     432
     426: iinc          7, 1
     429: goto          308
     432: iload         4
     434: iload         5
     436: if_icmpeq     445
     439: iinc          4, 1
     442: goto          281
     445: nop
     446: return

  public final kotlin.ranges.IntRange getIndices();
    Code:
       0: aload_0
       1: getfield      #29                 // Field indices:Lkotlin/ranges/IntRange;
       4: areturn

  public final double[][] getDist();
    Code:
       0: aload_0
       1: getfield      #37                 // Field dist:[[D
       4: areturn

  public final java.lang.Integer[][] getPrev();
    Code:
       0: aload_0
       1: getfield      #45                 // Field prev:[[Ljava/lang/Integer;
       4: areturn

  public java.lang.String toString();
    Code:
       0: aload_0
       1: getfield      #37                 // Field dist:[[D
       4: checkcast     #118                // class "[Ljava/lang/Object;"
       7: invokestatic  #124                // Method kotlin/collections/ArraysKt.toList:([Ljava/lang/Object;)Ljava/util/List;
      10: checkcast     #126                // class java/lang/Iterable
      13: astore_1
      14: iconst_0
      15: istore_2
      16: aload_1
      17: astore_3
      18: new           #128                // class java/util/ArrayList
      21: dup
      22: aload_1
      23: bipush        10
      25: invokestatic  #134                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      28: invokespecial #137                // Method java/util/ArrayList."<init>":(I)V
      31: checkcast     #139                // class java/util/Collection
      34: astore        4
      36: iconst_0
      37: istore        5
      39: aload_3
      40: invokeinterface #143,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      45: astore        6
      47: aload         6
      49: invokeinterface #149,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      54: ifeq          97
      57: aload         6
      59: invokeinterface #153,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      64: astore        7
      66: aload         4
      68: aload         7
      70: checkcast     #31                 // class "[D"
      73: astore        8
      75: astore        10
      77: iconst_0
      78: istore        9
      80: aload         8
      82: invokestatic  #156                // Method kotlin/collections/ArraysKt.toList:([D)Ljava/util/List;
      85: aload         10
      87: swap
      88: invokeinterface #160,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      93: pop
      94: goto          47
      97: aload         4
      99: checkcast     #162                // class java/util/List
     102: nop
     103: invokevirtual #164                // Method java/lang/Object.toString:()Ljava/lang/String;
     106: areturn

  public final java.util.List<java.lang.Integer> path(int, int);
    Code:
       0: aload_0
       1: iload_1
       2: iload_2
       3: invokestatic  #95                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       6: invokestatic  #182                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
       9: invokespecial #185                // Method path:(ILjava/lang/Integer;Ljava/util/List;)Ljava/util/List;
      12: areturn

  private final java.util.List<java.lang.Integer> path(int, java.lang.Integer, java.util.List<java.lang.Integer>);
    Code:
       0: aload_0
       1: astore        4
       3: aload_2
       4: ifnonnull     13
       7: invokestatic  #182                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
      10: goto          78
      13: iload_1
      14: aload_2
      15: invokevirtual #190                // Method java/lang/Integer.intValue:()I
      18: if_icmpne     25
      21: aload_3
      22: goto          78
      25: aload         4
      27: astore        5
      29: iload_1
      30: istore        6
      32: aload         4
      34: getfield      #45                 // Field prev:[[Ljava/lang/Integer;
      37: iload_1
      38: aaload
      39: aload_2
      40: invokevirtual #190                // Method java/lang/Integer.intValue:()I
      43: aaload
      44: astore        7
      46: aload_2
      47: invokestatic  #194                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      50: checkcast     #139                // class java/util/Collection
      53: aload_3
      54: checkcast     #126                // class java/lang/Iterable
      57: invokestatic  #198                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
      60: astore        8
      62: aload         5
      64: astore        4
      66: iload         6
      68: istore_1
      69: aload         7
      71: astore_2
      72: aload         8
      74: astore_3
      75: goto          3
      78: areturn

  private static final kotlin.Unit _init_$lambda$1(tools.graph.FloydWarshall, kotlin.Pair, java.lang.Double);
    Code:
       0: aload_1
       1: ldc           #202                // String <destruct>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #204                // String w
       9: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_1
      13: invokevirtual #209                // Method kotlin/Pair.component1:()Ljava/lang/Object;
      16: checkcast     #211                // class java/lang/Number
      19: invokevirtual #212                // Method java/lang/Number.intValue:()I
      22: istore_3
      23: aload_1
      24: invokevirtual #215                // Method kotlin/Pair.component2:()Ljava/lang/Object;
      27: checkcast     #211                // class java/lang/Number
      30: invokevirtual #212                // Method java/lang/Number.intValue:()I
      33: istore        4
      35: aload_0
      36: getfield      #37                 // Field dist:[[D
      39: iload_3
      40: aaload
      41: iload         4
      43: aload_2
      44: invokevirtual #221                // Method java/lang/Double.doubleValue:()D
      47: dastore
      48: aload_0
      49: getfield      #45                 // Field prev:[[Ljava/lang/Integer;
      52: iload_3
      53: aaload
      54: iload         4
      56: iload_3
      57: invokestatic  #95                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      60: aastore
      61: getstatic     #227                // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
      64: areturn

  private static final void _init_$lambda$2(kotlin.jvm.functions.Function2, java.lang.Object, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: aload_2
       3: invokeinterface #233,  3          // InterfaceMethod kotlin/jvm/functions/Function2.invoke:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
       8: pop
       9: return
}


package tools.graph

fun <Node : Any> shortPath(
    start: Node,
    end: Node,
    cost: (origin: Node, destination: Node) -> Double = { _, _ -> 1.0 },
    estimatedEndCost: (Node) -> Double = { 0.0 }, // A*
    neighbors: (Node) -> List<Node>
) = shortPath(start, isEnd = { this == end }, cost, estimatedEndCost, neighbors)

fun <Node : Any> shortPath(
    start: Node,
    isEnd: Node.() -> Boolean,
    cost: (origin: Node, destination: Node) -> Double = { _, _ -> 1.0 },
    toEndMinimalCost: (Node) -> Double = { 0.0 }, // A*
    neighbors: Node.() -> List<Node>
): List<Node> {
    val spStart = ShortPathNode(start, 0.0, toEndMinimalCost(start), true)
    val spNodes = mutableMapOf(start to spStart)
    val todo = mutableListOf(spStart)
    while (true) {
        val currentSPNode = todo.removeFirstOrNull() ?: return emptyList()
        currentSPNode.todo = false
        if (currentSPNode.node.isEnd())
            return generateSequence(currentSPNode) { it.predecessor }.map { it.node }.toList().reversed()
        neighbors(currentSPNode.node).forEach { nextNode ->
            val newFromStartCost = currentSPNode.fromStartCost + cost(currentSPNode.node, nextNode)
            val nextSPNode = spNodes[nextNode]
            when {
                nextSPNode == null -> {
                    val spNode = ShortPathNode(
                        nextNode,
                        newFromStartCost,
                        newFromStartCost + toEndMinimalCost(nextNode),
                        true
                    )
                    spNode.predecessor = currentSPNode
                    spNodes[nextNode] = spNode
                    todo.add(-todo.binarySearch(spNode) - 1, spNode)
                }

                newFromStartCost < nextSPNode.fromStartCost -> {
                    var toIndex = todo.size
                    if (nextSPNode.todo) {
                        toIndex = todo.binarySearch(nextSPNode)
                        todo.removeAt(toIndex)
                    }
                    nextSPNode.predecessor = currentSPNode
                    nextSPNode.minimalCost -= nextSPNode.fromStartCost - newFromStartCost
                    nextSPNode.fromStartCost = newFromStartCost
                    nextSPNode.todo = true
                    todo.add(-todo.binarySearch(nextSPNode, toIndex = toIndex) - 1, nextSPNode)
                }
            }
        }
    }
}

private data class ShortPathNode<Node : Any>(
    val node: Node,
    var fromStartCost: Double,
    var minimalCost: Double,
    var todo: Boolean
) : Comparable<ShortPathNode<Node>> {
    var predecessor: ShortPathNode<Node>? = null

    override fun compareTo(other: ShortPathNode<Node>): Int {
        val compare = minimalCost.compareTo(other.minimalCost)
        return if (compare != 0) compare else id.compareTo(other.id)
    }

    private val id = nextId++

    companion object {
        private var nextId = Int.MIN_VALUE
    }
}

Compiled from "ShortPath.kt"
public final class tools.graph.ShortPathKt {
  public static final <Node> java.util.List<Node> shortPath(Node, Node, kotlin.jvm.functions.Function2<? super Node, ? super Node, java.lang.Double>, kotlin.jvm.functions.Function1<? super Node, java.lang.Double>, kotlin.jvm.functions.Function1<? super Node, ? extends java.util.List<? extends Node>>);
    Code:
       0: aload_0
       1: ldc           #10                 // String start
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #18                 // String end
       9: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_2
      13: ldc           #20                 // String cost
      15: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      18: aload_3
      19: ldc           #22                 // String estimatedEndCost
      21: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      24: aload         4
      26: ldc           #24                 // String neighbors
      28: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      31: aload_0
      32: aload_1
      33: invokedynamic #44,  0             // InvokeDynamic #0:invoke:(Ljava/lang/Object;)Lkotlin/jvm/functions/Function1;
      38: aload_2
      39: aload_3
      40: aload         4
      42: invokestatic  #47                 // Method shortPath:(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/util/List;
      45: areturn

  public static java.util.List shortPath$default(java.lang.Object, java.lang.Object, kotlin.jvm.functions.Function2, kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function1, int, java.lang.Object);
    Code:
       0: iload         5
       2: iconst_4
       3: iand
       4: ifeq          13
       7: invokedynamic #64,  0             // InvokeDynamic #1:invoke:()Lkotlin/jvm/functions/Function2;
      12: astore_2
      13: iload         5
      15: bipush        8
      17: iand
      18: ifeq          27
      21: invokedynamic #74,  0             // InvokeDynamic #2:invoke:()Lkotlin/jvm/functions/Function1;
      26: astore_3
      27: aload_0
      28: aload_1
      29: aload_2
      30: aload_3
      31: aload         4
      33: invokestatic  #76                 // Method shortPath:(Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/util/List;
      36: areturn

  public static final <Node> java.util.List<Node> shortPath(Node, kotlin.jvm.functions.Function1<? super Node, java.lang.Boolean>, kotlin.jvm.functions.Function2<? super Node, ? super Node, java.lang.Double>, kotlin.jvm.functions.Function1<? super Node, java.lang.Double>, kotlin.jvm.functions.Function1<? super Node, ? extends java.util.List<? extends Node>>);
    Code:
       0: aload_0
       1: ldc           #10                 // String start
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #79                 // String isEnd
       9: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_2
      13: ldc           #20                 // String cost
      15: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      18: aload_3
      19: ldc           #81                 // String toEndMinimalCost
      21: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      24: aload         4
      26: ldc           #24                 // String neighbors
      28: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      31: new           #83                 // class tools/graph/ShortPathNode
      34: dup
      35: aload_0
      36: dconst_0
      37: aload_3
      38: aload_0
      39: invokeinterface #87,  2           // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
      44: checkcast     #89                 // class java/lang/Number
      47: invokevirtual #93                 // Method java/lang/Number.doubleValue:()D
      50: iconst_1
      51: invokespecial #97                 // Method tools/graph/ShortPathNode."<init>":(Ljava/lang/Object;DDZ)V
      54: astore        5
      56: iconst_1
      57: anewarray     #99                 // class kotlin/Pair
      60: astore        7
      62: aload         7
      64: iconst_0
      65: aload_0
      66: aload         5
      68: invokestatic  #105                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      71: aastore
      72: aload         7
      74: invokestatic  #111                // Method kotlin/collections/MapsKt.mutableMapOf:([Lkotlin/Pair;)Ljava/util/Map;
      77: astore        6
      79: iconst_1
      80: anewarray     #83                 // class tools/graph/ShortPathNode
      83: astore        8
      85: aload         8
      87: iconst_0
      88: aload         5
      90: aastore
      91: aload         8
      93: invokestatic  #117                // Method kotlin/collections/CollectionsKt.mutableListOf:([Ljava/lang/Object;)Ljava/util/List;
      96: astore        7
      98: nop
      99: aload         7
     101: invokestatic  #121                // Method kotlin/collections/CollectionsKt.removeFirstOrNull:(Ljava/util/List;)Ljava/lang/Object;
     104: checkcast     #83                 // class tools/graph/ShortPathNode
     107: dup
     108: ifnonnull     116
     111: pop
     112: invokestatic  #125                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
     115: areturn
     116: astore        8
     118: aload         8
     120: iconst_0
     121: invokevirtual #129                // Method tools/graph/ShortPathNode.setTodo:(Z)V
     124: aload_1
     125: aload         8
     127: invokevirtual #133                // Method tools/graph/ShortPathNode.getNode:()Ljava/lang/Object;
     130: invokeinterface #87,  2           // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
     135: checkcast     #135                // class java/lang/Boolean
     138: invokevirtual #139                // Method java/lang/Boolean.booleanValue:()Z
     141: ifeq          172
     144: aload         8
     146: invokedynamic #146,  0            // InvokeDynamic #3:invoke:()Lkotlin/jvm/functions/Function1;
     151: invokestatic  #152                // Method kotlin/sequences/SequencesKt.generateSequence:(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
     154: invokedynamic #159,  0            // InvokeDynamic #4:invoke:()Lkotlin/jvm/functions/Function1;
     159: invokestatic  #163                // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
     162: invokestatic  #167                // Method kotlin/sequences/SequencesKt.toList:(Lkotlin/sequences/Sequence;)Ljava/util/List;
     165: checkcast     #169                // class java/lang/Iterable
     168: invokestatic  #173                // Method kotlin/collections/CollectionsKt.reversed:(Ljava/lang/Iterable;)Ljava/util/List;
     171: areturn
     172: aload         4
     174: aload         8
     176: invokevirtual #133                // Method tools/graph/ShortPathNode.getNode:()Ljava/lang/Object;
     179: invokeinterface #87,  2           // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
     184: checkcast     #169                // class java/lang/Iterable
     187: astore        9
     189: iconst_0
     190: istore        10
     192: aload         9
     194: invokeinterface #177,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     199: astore        11
     201: aload         11
     203: invokeinterface #182,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     208: ifeq          480
     211: aload         11
     213: invokeinterface #185,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     218: astore        12
     220: aload         12
     222: astore        13
     224: iconst_0
     225: istore        14
     227: aload         8
     229: invokevirtual #188                // Method tools/graph/ShortPathNode.getFromStartCost:()D
     232: aload_2
     233: aload         8
     235: invokevirtual #133                // Method tools/graph/ShortPathNode.getNode:()Ljava/lang/Object;
     238: aload         13
     240: invokeinterface #192,  3          // InterfaceMethod kotlin/jvm/functions/Function2.invoke:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     245: checkcast     #89                 // class java/lang/Number
     248: invokevirtual #93                 // Method java/lang/Number.doubleValue:()D
     251: dadd
     252: dstore        15
     254: aload         6
     256: aload         13
     258: invokeinterface #197,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     263: checkcast     #83                 // class tools/graph/ShortPathNode
     266: astore        17
     268: nop
     269: aload         17
     271: ifnonnull     354
     274: new           #83                 // class tools/graph/ShortPathNode
     277: dup
     278: aload         13
     280: dload         15
     282: dload         15
     284: aload_3
     285: aload         13
     287: invokeinterface #87,  2           // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
     292: checkcast     #89                 // class java/lang/Number
     295: invokevirtual #93                 // Method java/lang/Number.doubleValue:()D
     298: dadd
     299: iconst_1
     300: invokespecial #97                 // Method tools/graph/ShortPathNode."<init>":(Ljava/lang/Object;DDZ)V
     303: astore        18
     305: aload         18
     307: aload         8
     309: invokevirtual #201                // Method tools/graph/ShortPathNode.setPredecessor:(Ltools/graph/ShortPathNode;)V
     312: aload         6
     314: aload         13
     316: aload         18
     318: invokeinterface #204,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     323: pop
     324: aload         7
     326: aload         7
     328: aload         18
     330: checkcast     #206                // class java/lang/Comparable
     333: iconst_0
     334: iconst_0
     335: bipush        6
     337: aconst_null
     338: invokestatic  #210                // Method kotlin/collections/CollectionsKt.binarySearch$default:(Ljava/util/List;Ljava/lang/Comparable;IIILjava/lang/Object;)I
     341: ineg
     342: iconst_1
     343: isub
     344: aload         18
     346: invokeinterface #216,  3          // InterfaceMethod java/util/List.add:(ILjava/lang/Object;)V
     351: goto          475
     354: dload         15
     356: aload         17
     358: invokevirtual #188                // Method tools/graph/ShortPathNode.getFromStartCost:()D
     361: dcmpg
     362: ifge          475
     365: aload         7
     367: invokeinterface #220,  1          // InterfaceMethod java/util/List.size:()I
     372: istore        18
     374: aload         17
     376: invokevirtual #223                // Method tools/graph/ShortPathNode.getTodo:()Z
     379: ifeq          409
     382: aload         7
     384: aload         17
     386: checkcast     #206                // class java/lang/Comparable
     389: iconst_0
     390: iconst_0
     391: bipush        6
     393: aconst_null
     394: invokestatic  #210                // Method kotlin/collections/CollectionsKt.binarySearch$default:(Ljava/util/List;Ljava/lang/Comparable;IIILjava/lang/Object;)I
     397: istore        18
     399: aload         7
     401: iload         18
     403: invokeinterface #227,  2          // InterfaceMethod java/util/List.remove:(I)Ljava/lang/Object;
     408: pop
     409: aload         17
     411: aload         8
     413: invokevirtual #201                // Method tools/graph/ShortPathNode.setPredecessor:(Ltools/graph/ShortPathNode;)V
     416: aload         17
     418: aload         17
     420: invokevirtual #230                // Method tools/graph/ShortPathNode.getMinimalCost:()D
     423: aload         17
     425: invokevirtual #188                // Method tools/graph/ShortPathNode.getFromStartCost:()D
     428: dload         15
     430: dsub
     431: dsub
     432: invokevirtual #234                // Method tools/graph/ShortPathNode.setMinimalCost:(D)V
     435: aload         17
     437: dload         15
     439: invokevirtual #237                // Method tools/graph/ShortPathNode.setFromStartCost:(D)V
     442: aload         17
     444: iconst_1
     445: invokevirtual #129                // Method tools/graph/ShortPathNode.setTodo:(Z)V
     448: aload         7
     450: aload         7
     452: aload         17
     454: checkcast     #206                // class java/lang/Comparable
     457: iconst_0
     458: iload         18
     460: iconst_2
     461: aconst_null
     462: invokestatic  #210                // Method kotlin/collections/CollectionsKt.binarySearch$default:(Ljava/util/List;Ljava/lang/Comparable;IIILjava/lang/Object;)I
     465: ineg
     466: iconst_1
     467: isub
     468: aload         17
     470: invokeinterface #216,  3          // InterfaceMethod java/util/List.add:(ILjava/lang/Object;)V
     475: nop
     476: nop
     477: goto          201
     480: nop
     481: goto          98

  public static java.util.List shortPath$default(java.lang.Object, kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function2, kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function1, int, java.lang.Object);
    Code:
       0: iload         5
       2: iconst_4
       3: iand
       4: ifeq          13
       7: invokedynamic #262,  0            // InvokeDynamic #5:invoke:()Lkotlin/jvm/functions/Function2;
      12: astore_2
      13: iload         5
      15: bipush        8
      17: iand
      18: ifeq          27
      21: invokedynamic #267,  0            // InvokeDynamic #6:invoke:()Lkotlin/jvm/functions/Function1;
      26: astore_3
      27: aload_0
      28: aload_1
      29: aload_2
      30: aload_3
      31: aload         4
      33: invokestatic  #47                 // Method shortPath:(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/util/List;
      36: areturn

  private static final double shortPath$lambda$0(java.lang.Object, java.lang.Object);
    Code:
       0: aload_0
       1: ldc_w         #269                // String <unused var>
       4: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_1
       8: ldc_w         #269                // String <unused var>
      11: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      14: dconst_1
      15: dreturn

  private static final double shortPath$lambda$1(java.lang.Object);
    Code:
       0: aload_0
       1: ldc_w         #271                // String it
       4: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: dconst_0
       8: dreturn

  private static final boolean shortPath$lambda$2(java.lang.Object, java.lang.Object);
    Code:
       0: aload_1
       1: ldc_w         #273                // String $this$shortPath
       4: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_1
       8: aload_0
       9: invokestatic  #276                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      12: ireturn

  private static final double shortPath$lambda$3(java.lang.Object, java.lang.Object);
    Code:
       0: aload_0
       1: ldc_w         #269                // String <unused var>
       4: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_1
       8: ldc_w         #269                // String <unused var>
      11: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      14: dconst_1
      15: dreturn

  private static final double shortPath$lambda$4(java.lang.Object);
    Code:
       0: aload_0
       1: ldc_w         #271                // String it
       4: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: dconst_0
       8: dreturn

  private static final tools.graph.ShortPathNode shortPath$lambda$5(tools.graph.ShortPathNode);
    Code:
       0: aload_0
       1: ldc_w         #271                // String it
       4: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: invokevirtual #281                // Method tools/graph/ShortPathNode.getPredecessor:()Ltools/graph/ShortPathNode;
      11: areturn

  private static final java.lang.Object shortPath$lambda$6(tools.graph.ShortPathNode);
    Code:
       0: aload_0
       1: ldc_w         #271                // String it
       4: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: invokevirtual #133                // Method tools/graph/ShortPathNode.getNode:()Ljava/lang/Object;
      11: areturn
}


package tools.math

fun Long.times(multiplier: Long, modulo: Long): Long {
    if (this < 0) error("negative multiplicand")
    if (multiplier < 0) error("negative multiplier")
    if (modulo < 0) error("negative modulo")
    var res = 0L
    var a = this % modulo
    var b = multiplier % modulo
    while (true) {
        if (b and 1L > 0L) res = (res + a) % modulo
        b = b shr 1
        if (b == 0L) return res
        a = a shl 1
        if (a < 0) error("overflow")
        a %= modulo
    }
}

fun Long.inv(modulo: Long): Long = inverse(this, modulo, 1, 0).let { if (it < 0) it + modulo else it }
private tailrec fun inverse(v: Long, n: Long, s: Long, t: Long): Long =
    if (v == 1L) s else inverse(n % v, v, t - n / v * s, s)

fun chineseRemainder(input: List<Pair<Long, Long>>): Long {
    val np = input.fold(1L) { acc, i -> acc * i.second }
    return input.fold(0L) { acc, (v, n) -> (np / n).let { (acc + v.times(it.inv(n), np).times(it, np)).mod(np) } }
}

Compiled from "Modulo.kt"
public final class tools.math.ModuloKt {
  public static final long times(long, long, long);
    Code:
       0: lload_0
       1: lconst_0
       2: lcmp
       3: ifge          19
       6: new           #8                  // class java/lang/IllegalStateException
       9: dup
      10: ldc           #10                 // String negative multiplicand
      12: invokevirtual #14                 // Method java/lang/Object.toString:()Ljava/lang/String;
      15: invokespecial #18                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      18: athrow
      19: lload_2
      20: lconst_0
      21: lcmp
      22: ifge          38
      25: new           #8                  // class java/lang/IllegalStateException
      28: dup
      29: ldc           #20                 // String negative multiplier
      31: invokevirtual #14                 // Method java/lang/Object.toString:()Ljava/lang/String;
      34: invokespecial #18                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      37: athrow
      38: lload         4
      40: lconst_0
      41: lcmp
      42: ifge          58
      45: new           #8                  // class java/lang/IllegalStateException
      48: dup
      49: ldc           #22                 // String negative modulo
      51: invokevirtual #14                 // Method java/lang/Object.toString:()Ljava/lang/String;
      54: invokespecial #18                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      57: athrow
      58: lconst_0
      59: lstore        6
      61: lload_0
      62: lload         4
      64: lrem
      65: lstore        8
      67: lload_2
      68: lload         4
      70: lrem
      71: lstore        10
      73: nop
      74: lload         10
      76: lconst_1
      77: land
      78: lconst_0
      79: lcmp
      80: ifle          93
      83: lload         6
      85: lload         8
      87: ladd
      88: lload         4
      90: lrem
      91: lstore        6
      93: lload         10
      95: iconst_1
      96: lshr
      97: lstore        10
      99: lload         10
     101: lconst_0
     102: lcmp
     103: ifne          109
     106: lload         6
     108: lreturn
     109: lload         8
     111: iconst_1
     112: lshl
     113: lstore        8
     115: lload         8
     117: lconst_0
     118: lcmp
     119: ifge          135
     122: new           #8                  // class java/lang/IllegalStateException
     125: dup
     126: ldc           #24                 // String overflow
     128: invokevirtual #14                 // Method java/lang/Object.toString:()Ljava/lang/String;
     131: invokespecial #18                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     134: athrow
     135: lload         8
     137: lload         4
     139: lrem
     140: lstore        8
     142: goto          73

  public static final long inv(long, long);
    Code:
       0: lload_0
       1: lload_2
       2: lconst_1
       3: lconst_0
       4: invokestatic  #37                 // Method inverse:(JJJJ)J
       7: lstore        4
       9: iconst_0
      10: istore        6
      12: lload         4
      14: lconst_0
      15: lcmp
      16: ifge          26
      19: lload         4
      21: lload_2
      22: ladd
      23: goto          28
      26: lload         4
      28: nop
      29: lreturn

  private static final long inverse(long, long, long, long);
    Code:
       0: lload_0
       1: lconst_1
       2: lcmp
       3: ifne          11
       6: lload         4
       8: goto          51
      11: lload_2
      12: lload_0
      13: lrem
      14: lstore        8
      16: lload_0
      17: lstore        10
      19: lload         6
      21: lload_2
      22: lload_0
      23: ldiv
      24: lload         4
      26: lmul
      27: lsub
      28: lstore        12
      30: lload         4
      32: lstore        14
      34: lload         8
      36: lstore_0
      37: lload         10
      39: lstore_2
      40: lload         12
      42: lstore        4
      44: lload         14
      46: lstore        6
      48: goto          0
      51: lreturn

  public static final long chineseRemainder(java.util.List<kotlin.Pair<java.lang.Long, java.lang.Long>>);
    Code:
       0: aload_0
       1: ldc           #51                 // String input
       3: invokestatic  #57                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #59                 // class java/lang/Iterable
      10: astore_3
      11: lconst_1
      12: lstore        4
      14: iconst_0
      15: istore        6
      17: lload         4
      19: lstore        7
      21: aload_3
      22: invokeinterface #63,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      27: astore        9
      29: aload         9
      31: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      36: ifeq          81
      39: aload         9
      41: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      46: astore        10
      48: lload         7
      50: aload         10
      52: checkcast     #75                 // class kotlin/Pair
      55: astore        11
      57: lstore        12
      59: iconst_0
      60: istore        14
      62: lload         12
      64: aload         11
      66: invokevirtual #78                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
      69: checkcast     #80                 // class java/lang/Number
      72: invokevirtual #84                 // Method java/lang/Number.longValue:()J
      75: lmul
      76: lstore        7
      78: goto          29
      81: lload         7
      83: lstore_1
      84: aload_0
      85: checkcast     #59                 // class java/lang/Iterable
      88: astore_3
      89: lconst_0
      90: lstore        4
      92: iconst_0
      93: istore        6
      95: lload         4
      97: lstore        7
      99: aload_3
     100: invokeinterface #63,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     105: astore        9
     107: aload         9
     109: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     114: ifeq          228
     117: aload         9
     119: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     124: astore        10
     126: lload         7
     128: aload         10
     130: checkcast     #75                 // class kotlin/Pair
     133: astore        11
     135: lstore        12
     137: iconst_0
     138: istore        14
     140: aload         11
     142: invokevirtual #87                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
     145: checkcast     #80                 // class java/lang/Number
     148: invokevirtual #84                 // Method java/lang/Number.longValue:()J
     151: lstore        15
     153: aload         11
     155: invokevirtual #90                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
     158: checkcast     #80                 // class java/lang/Number
     161: invokevirtual #84                 // Method java/lang/Number.longValue:()J
     164: lstore        17
     166: lload_1
     167: lload         17
     169: ldiv
     170: lstore        19
     172: iconst_0
     173: istore        21
     175: lload         12
     177: lload         15
     179: lload         19
     181: lload         17
     183: invokestatic  #92                 // Method inv:(JJ)J
     186: lload_1
     187: invokestatic  #94                 // Method times:(JJJ)J
     190: lload         19
     192: lload_1
     193: invokestatic  #94                 // Method times:(JJJ)J
     196: ladd
     197: lload_1
     198: lrem
     199: lstore        22
     201: lload         22
     203: lload_1
     204: lload         22
     206: lload_1
     207: lxor
     208: lload         22
     210: lload         22
     212: lneg
     213: lor
     214: land
     215: bipush        63
     217: lshr
     218: land
     219: ladd
     220: nop
     221: nop
     222: nop
     223: lstore        7
     225: goto          107
     228: lload         7
     230: lreturn
}


package org.eln2.mc.data

/**
 * Represents the numeric range of a segment.
 * @param min The lower boundary of this segment.
 * @param max The upper boundary of this segment.
 * [min] must be smaller than [max].
 * */
data class ClosedInterval(val min: Double, val max: Double) {
    val range get() = min..max

    init {
        if (min >= max) {
            error("Invalid interval $min -> $max")
        }
    }

    fun contains(point: Double) = point in range
}

data class SegmentTreeNode<T>(
    val range: ClosedInterval,
    val data: T?,
    private val l: SegmentTreeNode<T>?,
    private val r: SegmentTreeNode<T>?,
) {
    constructor(range: ClosedInterval, segment: T?) : this(range, segment, null, null)

    /**
     * @return True if this segment's [range] contains the [point]. Otherwise, false.
     * */
    fun contains(point: Double): Boolean {
        return range.contains(point)
    }

    /**
     * Recursively queries until a leaf node containing [point] is found.
     * @param point A point contained within a segment. If this segment does not contain the point, an error will be produced.
     * @return The data value associated with the leaf node that contains [point]. An error will be produced if segment continuity is broken (parent contains point but the child nodes do not).
     * */
    fun query(point: Double): T? {
        if (!contains(point)) {
            return null
        }

        val left = l
        val right = r

        if (left != null && left.contains(point)) {
            return left.query(point)
        }

        if (right != null && right.contains(point)) {
            return right.query(point)
        }

        return data
    }
}

class SegmentTree<T>(private val root: SegmentTreeNode<T>) {
    fun queryOrNull(point: Double): T? {
        if (!root.contains(point)) {
            return null
        }

        return root.query(point)
    }

    /**
     * Finds the segment with the specified range. Time complexity is O(log n)
     * */
    fun query(point: Double): T {
        return root.query(point) ?: error("$point not found")
    }
}

class SegmentTreeBuilder<TSegment> {
    private data class PendingSegment<T>(val segment: T, val range: ClosedInterval)

    private val pending = ArrayList<PendingSegment<TSegment>>()

    /**
     * Inserts a segment into the pending set. If its range is not sorted with the previously inserted segments,
     * [sort] must be called before attempting to [build].
     * */
    fun insert(segment: TSegment, range: ClosedInterval) {
        pending.add(PendingSegment(segment, range))
    }

    /**
     * Sorts the segments by range.
     * */
    fun sort() {
        pending.sortBy { it.range.min }
    }

    /**
     * Builds a [SegmentTree] from the pending set.
     * If segment continuity is broken, an error will be produced.
     * */
    fun build(): SegmentTree<TSegment> {
        if (pending.isEmpty()) {
            error("Tried to build empty segment tree")
        }

        /* if (pending.size > 1) {
             for (i in 1 until pending.size) {
                 val previous = pending[i - 1]
                 val current = pending[i]

                 if (previous.range.max != current.range.min) {
                     error("Segment tree continuity error")
                 }
             }
         }*/

        return SegmentTree(buildSegment(0, pending.size - 1))
    }

    private fun buildSegment(leftIndex: Int, rightIndex: Int): SegmentTreeNode<TSegment> {
        if (leftIndex == rightIndex) {
            val data = pending[leftIndex]

            return SegmentTreeNode(data.range, data.segment)
        }

        val mid = leftIndex + (rightIndex - leftIndex) / 2

        return SegmentTreeNode(
            ClosedInterval(pending[leftIndex].range.min, pending[rightIndex].range.max),
            data = null,
            l = buildSegment(leftIndex, mid),
            r = buildSegment(mid + 1, rightIndex)
        )
    }
}

Compiled from "SegmentTree.kt"
public final class org.eln2.mc.data.SegmentTree<T> {
  private final org.eln2.mc.data.SegmentTreeNode<T> root;

  public org.eln2.mc.data.SegmentTree(org.eln2.mc.data.SegmentTreeNode<T>);
    Code:
       0: aload_1
       1: ldc           #11                 // String root
       3: invokestatic  #17                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokespecial #20                 // Method java/lang/Object."<init>":()V
      10: aload_0
      11: aload_1
      12: putfield      #23                 // Field root:Lorg/eln2/mc/data/SegmentTreeNode;
      15: return

  public final T queryOrNull(double);
    Code:
       0: aload_0
       1: getfield      #23                 // Field root:Lorg/eln2/mc/data/SegmentTreeNode;
       4: dload_1
       5: invokevirtual #35                 // Method org/eln2/mc/data/SegmentTreeNode.contains:(D)Z
       8: ifne          13
      11: aconst_null
      12: areturn
      13: aload_0
      14: getfield      #23                 // Field root:Lorg/eln2/mc/data/SegmentTreeNode;
      17: dload_1
      18: invokevirtual #38                 // Method org/eln2/mc/data/SegmentTreeNode.query:(D)Ljava/lang/Object;
      21: areturn

  public final T query(double);
    Code:
       0: aload_0
       1: getfield      #23                 // Field root:Lorg/eln2/mc/data/SegmentTreeNode;
       4: dload_1
       5: invokevirtual #38                 // Method org/eln2/mc/data/SegmentTreeNode.query:(D)Ljava/lang/Object;
       8: dup
       9: ifnonnull     43
      12: pop
      13: new           #42                 // class java/lang/IllegalStateException
      16: dup
      17: new           #44                 // class java/lang/StringBuilder
      20: dup
      21: invokespecial #45                 // Method java/lang/StringBuilder."<init>":()V
      24: dload_1
      25: invokevirtual #49                 // Method java/lang/StringBuilder.append:(D)Ljava/lang/StringBuilder;
      28: ldc           #51                 // String  not found
      30: invokevirtual #54                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      33: invokevirtual #58                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      36: invokevirtual #59                 // Method java/lang/Object.toString:()Ljava/lang/String;
      39: invokespecial #62                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      42: athrow
      43: areturn
}