結果
問題 | No.1864 Shortest Paths Counting |
ユーザー |
|
提出日時 | 2022-03-15 22:13:13 |
言語 | Scala(Beta) (3.6.2) |
結果 |
TLE
|
実行時間 | - |
コード長 | 4,371 bytes |
コンパイル時間 | 14,689 ms |
コンパイル使用メモリ | 270,216 KB |
実行使用メモリ | 66,952 KB |
最終ジャッジ日時 | 2024-09-22 14:33:31 |
合計ジャッジ時間 | 29,163 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 5 TLE * 1 -- * 17 |
ソースコード
import java.io.PrintWriterimport scala.collection.mutable.*import scala.io.StdIn.*import scala.util.chaining.*import scala.math.*import scala.reflect.ClassTagimport scala.util.*import scala.annotation.tailrecimport scala.collection.mutableobject ModInt:opaque type ModInt = Longinline def zero: ModInt = 0inline def one: ModInt = 1extension (value: Long)inline def toModInt(using inline mod: Int): ModInt = value % modinline def unsafeAsModInt: ModInt = valueextension (value: Int)inline def toModInt(using inline mod: Int): ModInt = value % modinline def unsafeAsModInt: ModInt = valuetrait Converter[T]:inline def convert(value: T)(using inline mod: Int): ModIntinline given Converter[Int] withoverride inline def convert(value: Int)(using inline mod: Int) = value.toModIntinline given Converter[Long] withoverride inline def convert(value: Long)(using inline mod: Int) = value.toModIntinline given Converter[ModInt] withoverride inline def convert(value: ModInt)(using inline mod: Int) = valueextension (modInt: ModInt)inline def value(using inline mod: Int): Long = (modInt + mod) % modinline def rawValue: Long = modIntinline def inverse(using inline mod: Int): ModInt =var x = modIntvar y = mod.toLongvar a = 1Lvar b = 0Lvar c = 0Lvar d = 1Lwhile y != 0 doval q = x / yval e = a - c * qval f = b - d * qa = cb = dc = ed = fx = yy = c * modInt + d * modrequire(x.abs == 1L)if x == 1 then a % mod else -a % modinline def pow(exp: Long)(using inline mod: Int): ModInt =var result = 1Lvar base = modIntvar e = expwhile e > 0 doif (e & 1) == 1 thenresult = result * base % modbase = base * base % mode >>= 1resultinline def pow(exp: Int)(using inline mod: Int): ModInt = pow(exp.toLong)inline def powMod[B: Converter] (base: B, exp: Int)(using inline mod: Int): ModInt = summon[Converter[B]].convert(base).pow(exp)inline def powMod[B: Converter] (base: B, exp: Long)(using inline mod: Int): ModInt = summon[Converter[B]].convert(base).pow(exp)extension [L: Converter, R: Converter] (left: L)inline def +(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) + summon[Converter[R]].convert(right)).toModIntinline def -(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) - summon[Converter[R]].convert(right)).toModIntinline def *(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) * summon[Converter[R]].convert(right)).toModIntinline def /(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) * summon[Converter[R]].convert(right).inverse).toModIntclass Bit(size: Int):import ModInt.*private val vec = Array.fill(size){ModInt.zero}inline def get(position: Int)(using inline mod: Int): ModInt =var pos = positionvar result = ModInt.zerowhile 0 <= pos doresult += vec(pos)pos -= ~pos & (pos + 1)resultinline def add(position: Int, value: ModInt)(using inline mod: Int) =var pos = positionwhile pos < size dovec(pos) += valuepos += ~pos & (pos + 1)@main def main =inline given MOD: Int = 998244353val n = readLine().toIntval points = Array.fill(n){val Array(x, y) = readLine().split(' ').map(_.toInt)x -> y}val (startX, startY) = points.headval (goalX, goalY) = points.lastval sx = if startX + startY <= goalX + goalY then 1 else -1val sy = if startY - startX <= goalY - goalX then 1 else -1val normalized = points.map{case (x, y) => (x + y - - startX - startY) * sx -> (y - x + startX - startY) * sy}val yRank = normalized.map(_._2).distinct.sorted.zipWithIndex.toMapval bit = Bit(yRank.size)var result = ModInt.zerofor i <- normalized.indices.sorted(using Ordering.by[Int, Int](i => normalized(i)._1).orElseBy(i => normalized(i)._2)) doval (_, y) = normalized(i)if i == 0 thenbit.add(yRank(y), ModInt.one)else if i == n - 1 thenresult = bit.get(yRank(y))elseval pattern = bit.get(yRank(y))bit.add(yRank(y), pattern)println(result.value)