|
| 1 | +package io.github.acl4s |
| 2 | + |
| 3 | +class FenwickTreeSuite extends munit.FunSuite { |
| 4 | + |
| 5 | + /** |
| 6 | + * @see https://atcoder.jp/contests/practice2/tasks/practice2_b |
| 7 | + */ |
| 8 | + test("AtCoder Library Practice Contest B - Fenwick Tree") { |
| 9 | + val fw = FenwickTree(Array(1L, 2L, 3L, 4L, 5L)) |
| 10 | + |
| 11 | + assertEquals(fw.sum(0, 5), 15L) |
| 12 | + assertEquals(fw.sum(2, 4), 7L) |
| 13 | + |
| 14 | + fw.add(3, 10) |
| 15 | + |
| 16 | + assertEquals(fw.sum(0, 5), 25L) |
| 17 | + assertEquals(fw.sum(0, 3), 6L) |
| 18 | + } |
| 19 | + |
| 20 | + test("zero") { |
| 21 | + { |
| 22 | + val fw = FenwickTree[Long](0) |
| 23 | + assertEquals(fw.sum(0, 0), 0L) |
| 24 | + } |
| 25 | + |
| 26 | + { |
| 27 | + type ModInt = DynamicModInt |
| 28 | + val ModInt = DynamicModInt |
| 29 | + |
| 30 | + val fw = FenwickTree[ModInt](0) |
| 31 | + assertEquals(fw.sum(0, 0), ModInt(0)) |
| 32 | + } |
| 33 | + |
| 34 | + { |
| 35 | + type ModInt = ModInt998244353 |
| 36 | + val ModInt = ModInt998244353 |
| 37 | + |
| 38 | + val fw = FenwickTree[ModInt](0) |
| 39 | + assertEquals(fw.sum(0, 0), ModInt(0)) |
| 40 | + } |
| 41 | + |
| 42 | + { |
| 43 | + type ModInt = ModInt1000000007 |
| 44 | + val ModInt = ModInt1000000007 |
| 45 | + |
| 46 | + val fw = FenwickTree[ModInt](0) |
| 47 | + assertEquals(fw.sum(0, 0), ModInt(0)) |
| 48 | + } |
| 49 | + |
| 50 | + { |
| 51 | + given Modulus[1_000_000_009] = Modulus[1_000_000_009]() |
| 52 | + type ModInt = StaticModInt[1_000_000_009] |
| 53 | + val ModInt = StaticModInt |
| 54 | + |
| 55 | + val fw = FenwickTree[ModInt](0) |
| 56 | + assertEquals(fw.sum(0, 0), ModInt(0)) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + test("naive") { |
| 61 | + (0 to 50).foreach(n => { |
| 62 | + val fw = FenwickTree[Long](n) |
| 63 | + (0 until n).foreach(i => { |
| 64 | + fw.add(i, i.toLong * i) |
| 65 | + }) |
| 66 | + |
| 67 | + for { |
| 68 | + l <- 0 to n |
| 69 | + r <- l to n |
| 70 | + } { |
| 71 | + val sum = (l until r).map(i => i.toLong * i).sum |
| 72 | + assertEquals(fw.sum(l, r), sum) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + test("bound int") { |
| 78 | + val fw = FenwickTree[Int](10) |
| 79 | + |
| 80 | + fw.add(3, Int.MaxValue) |
| 81 | + fw.add(5, Int.MinValue) |
| 82 | + |
| 83 | + assertEquals(fw.sum(0, 10), -1) |
| 84 | + assertEquals(fw.sum(3, 6), -1) |
| 85 | + |
| 86 | + assertEquals(fw.sum(3, 4), Int.MaxValue) |
| 87 | + assertEquals(fw.sum(4, 10), Int.MinValue) |
| 88 | + } |
| 89 | + |
| 90 | + test("bound long") { |
| 91 | + val fw = FenwickTree[Long](10) |
| 92 | + |
| 93 | + fw.add(3, Long.MaxValue) |
| 94 | + fw.add(5, Long.MinValue) |
| 95 | + |
| 96 | + assertEquals(fw.sum(0, 10), -1L) |
| 97 | + assertEquals(fw.sum(3, 6), -1L) |
| 98 | + |
| 99 | + assertEquals(fw.sum(3, 4), Long.MaxValue) |
| 100 | + assertEquals(fw.sum(4, 10), Long.MinValue) |
| 101 | + } |
| 102 | + |
| 103 | + test("overflow") { |
| 104 | + val fw = FenwickTree[Int](20) |
| 105 | + val a = new Array[Long](20) |
| 106 | + (0 until 10).foreach(i => { |
| 107 | + fw.add(i, Int.MaxValue) |
| 108 | + a(i) += Int.MaxValue |
| 109 | + }) |
| 110 | + (10 until 20).foreach(i => { |
| 111 | + fw.add(i, Int.MinValue) |
| 112 | + a(i) += Int.MinValue |
| 113 | + }) |
| 114 | + |
| 115 | + fw.add(5, 11_111) |
| 116 | + a(5) += 11_111 |
| 117 | + |
| 118 | + for { |
| 119 | + l <- 0 to 20 |
| 120 | + r <- l to 20 |
| 121 | + } { |
| 122 | + val sum = (l until r).map(i => a(i)).sum |
| 123 | + val dif = sum - fw.sum(l, r) |
| 124 | + assertEquals(dif % (1L << 32), 0L) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + test("StaticModInt") { |
| 129 | + given Modulus[11] = Modulus[11]() |
| 130 | + type ModInt = StaticModInt[11] |
| 131 | + val ModInt = StaticModInt |
| 132 | + |
| 133 | + (0 to 50).foreach(n => { |
| 134 | + val fw = FenwickTree[ModInt](n) |
| 135 | + (0 until n).foreach(i => { |
| 136 | + fw.add(i, ModInt(i.toLong * i)) |
| 137 | + }) |
| 138 | + |
| 139 | + for { |
| 140 | + l <- 0 to n |
| 141 | + r <- l to n |
| 142 | + } { |
| 143 | + val sum = (l until r).map(i => i.toLong * i).sum |
| 144 | + assertEquals(fw.sum(l, r), ModInt(sum)) |
| 145 | + } |
| 146 | + }) |
| 147 | + } |
| 148 | + |
| 149 | + test("DynamicModInt") { |
| 150 | + type ModInt = DynamicModInt |
| 151 | + val ModInt = DynamicModInt |
| 152 | + ModInt.setMod(11) |
| 153 | + |
| 154 | + (0 to 50).foreach(n => { |
| 155 | + val fw = FenwickTree[ModInt](n) |
| 156 | + (0 until n).foreach(i => { |
| 157 | + fw.add(i, ModInt(i.toLong * i)) |
| 158 | + }) |
| 159 | + |
| 160 | + for { |
| 161 | + l <- 0 to n |
| 162 | + r <- l to n |
| 163 | + } { |
| 164 | + val sum = (l until r).map(i => i.toLong * i).sum |
| 165 | + assertEquals(fw.sum(l, r), ModInt(sum)) |
| 166 | + } |
| 167 | + }) |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments