結果

問題 No.675 ドットちゃんたち
ユーザー むらためむらため
提出日時 2019-02-03 22:10:11
言語 Nim
(2.0.2)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 3,382 bytes
コンパイル時間 2,574 ms
コンパイル使用メモリ 70,200 KB
実行使用メモリ 6,416 KB
最終ジャッジ日時 2023-09-14 03:22:04
合計ジャッジ時間 4,159 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 34 ms
6,412 KB
testcase_06 AC 39 ms
6,396 KB
testcase_07 AC 36 ms
6,060 KB
testcase_08 AC 37 ms
6,416 KB
testcase_09 AC 42 ms
6,344 KB
testcase_10 AC 39 ms
6,392 KB
testcase_11 AC 39 ms
6,288 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport]

ソースコード

diff #

import sequtils,times
proc printf(formatstr: cstring){.header: "<stdio.h>", varargs.}
template stopwatch(body) = (let t1 = cpuTime();body;echo "TIME:",(cpuTime() - t1) * 1000,"ms")
template stopwatch(body) = body
GC_disableMarkAndSweep()

template useMatrix =
  type Matrix[T] = ref object
    w,h:int
    data: seq[T]
  proc `[]`[T](m:Matrix[T],x,y:int):T {.inline.} = m.data[x + y * m.w]
  proc `[]=`[T](m:var Matrix[T],x,y:int,val:T) {.inline.} = m.data[x + y * m.w] = val
  proc newMatrix[T](w,h:int):Matrix[T] =
    new(result)
    result.w = w
    result.h = h
    result.data = newSeq[T](w * h)
  proc identityMatrix[T](n:int):Matrix[T] =
    result = newMatrix[T](n,n)
    for i in 0..<n: result[i,i] = 1
  proc newMatrix[T](arr:seq[seq[T]]):Matrix[T] =
    new(result)
    result.w = arr[0].len
    result.h = arr.len
    result.data = newSeq[T](result.w * result.h)
    for x in 0..<result.w:
      for y in 0..<result.h:
        result[x,y] = arr[y][x]
  proc `*`[T](a,b:Matrix[T]): Matrix[T] =
    assert a.w == b.h
    result = newMatrix[T](a.h,b.w)
    for y in 0..<a.h:
      for x in 0..<b.w:
        var n : T
        for k in 0..<a.w:
          n += a[k,y] * b[x,k]
        result[x,y] = n
  var unsafeMat = identityMatrix[int](3)
  proc `*=`[T](a:var Matrix[T],b:Matrix[T]) =
    assert a.w == b.h
    for y in 0..<a.h:
      for x in 0..<b.w:
        var n : T
        for k in 0..<a.w:
          n += a[k,y] * b[x,k]
        unsafeMat[x,y] = n
    swap(unsafeMat,a)

  proc `^`[T](m:Matrix[T],n:int) : Matrix[T] =
    assert m.w == m.h
    if n <= 0 : return identityMatrix[T](m.w)
    if n == 1 : return m
    let m2 = m^(n div 2)
    if n mod 2 == 0 : return m2 * m2
    return m2 * m2 * m
  proc `$`[T](m:Matrix[T]) : string =
    result = ""
    for y in 0..<m.h:
      result &= "["
      for x in 0..<m.w:
        result &= $m[x,y]
        if x != m.w - 1 : result &= ","
      result &= "]"
      if y != m.h - 1 : result &= "\n"
    result &= "\n"
  proc `*`[T](a:Matrix,b:seq[T]):seq[T] =
    assert a.w == b.len
    result = newSeq[T](b.len)
    for x in 0..<a.h:
      var n : T
      for k in 0..<a.w:
        n += a[k,x] * b[k]
      result[x] = n


useMatrix()

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  var minus = false
  block:
    let k = getchar_unlocked()
    if k == '-' : minus = true
    else: result = 10 * result + k.ord - '0'.ord
  while true:
    let k = getchar_unlocked()
    if k < '0' or k > '9': break
    result = 10 * result + k.ord - '0'.ord
  if minus: result *= -1

stopwatch:
  let n = scan()
  let px = scan()
  let py = scan()
  var C = newSeq[tuple[q,v:int]](n)
  for i in 0..<n:
    C[i].q = scan()
    if C[i].q == 3 : continue
    C[i].v = scan()
  var M = identityMatrix[int](3)
  var answers = newSeq[tuple[x,y:int]](n)
  let m90 = newMatrix(@[@[0,1,0],@[-1,0,0],@[0,0,1]])
  var my  = newMatrix(@[@[1,0,0],@[0,1,9],@[0,0,1]])
  var mx  = newMatrix(@[@[1,0,9],@[0,1,0],@[0,0,1]])
stopwatch:
  let p = @[px,py,1]
  for i in (n-1).countdown(0):
    let (q,v) = C[i]
    if q == 3: M *= m90
    elif q == 2:
      my[2,1] = v
      M *= my
    else:
      mx[2,0] = v
      M *= mx
    answers[i] = (
      x:M[0,0] * px + M[1,0] * py + M[2,0],
      y:M[0,1] * px + M[1,1] * py + M[2,1]
    )
stopwatch:
  for ans in answers: printf("%d %d\n",ans.x,ans.y)
0