結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
![]() |
提出日時 | 2020-04-25 17:09:12 |
言語 | Nim (2.2.0) |
結果 |
AC
|
実行時間 | 708 ms / 2,000 ms |
コード長 | 1,485 bytes |
コンパイル時間 | 5,372 ms |
コンパイル使用メモリ | 66,076 KB |
実行使用メモリ | 35,840 KB |
最終ジャッジ日時 | 2024-09-16 13:48:58 |
合計ジャッジ時間 | 21,703 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
import strutils, sequtils, mathtype SegmentTree[T] = objectn: intdat: seq[T]e: Tmultiply: proc(a, b: T): Tproc initSegmentTree[T](n: int, e: T, f: proc(a, b: T): T): SegmentTree[T] =let nn = nextPowerOfTwo(n)var dat = newSeqWith(nn * 2 - 1, e)return SegmentTree[T](n: nn, dat: dat, e: e, multiply: f)proc get[T](this: SegmentTree[T], i: int): T =return this.dat[i + this.n - 1]proc update[T](this: var SegmentTree[T], i: int, x: T) =var k = i + this.n - 1this.dat[k] = xwhile k > 0:k = (k - 1) div 2this.dat[k] = this.multiply(this.dat[k * 2 + 1], this.dat[k * 2 + 2])proc query[T](this: SegmentTree[T], ql, qr, i, il, ir: int): T =if ql <= il and ir <= qr:return this.dat[i]if qr <= il or ir <= ql:return this.elet m = (il + ir) div 2return this.multiply(query(this, ql, qr, i * 2 + 1, il, m),query(this, ql, qr, i * 2 + 2, m, ir))proc query[T](this: SegmentTree[T], ql, qr: int): T =return query(this, ql, qr, 0, 0, this.n)let read = iterator: string {.closure.} =for s in stdin.readAll.split:yield sproc main() =letn = read().parseInta = newSeqWith(n, read().parseBiggestInt)var seg = initSegmentTree[int64](n,0,proc(x, y: int64): int64 = gcd(x, y))for i in 0..<n:seg.update(i, a[i])varans: int64 = 0le = 0for ri in 1..n:while le < ri and seg.query(le, ri) == 1:ans += n - ri + 1le += 1echo ansmain()