結果

問題 No.1890 Many Sequences Sum Queries
ユーザー simansiman
提出日時 2022-06-19 00:33:21
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 967 bytes
コンパイル時間 475 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 13,184 KB
最終ジャッジ日時 2024-04-19 00:38:01
合計ジャッジ時間 6,304 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 435 ms
12,928 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 84 ms
12,416 KB
testcase_05 WA -
testcase_06 AC 83 ms
12,416 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 83 ms
12,416 KB
testcase_18 AC 84 ms
12,160 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 91 ms
12,288 KB
testcase_25 AC 85 ms
12,160 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:66: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

N, Q = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
S = Q.times.map { gets.to_i }

B = []
C = []

A.each do |a|
  if C.empty?
    C << a
  else
    C << C.last + a
  end

  if B.empty?
    B << a * (1 + a) / 2
  else
    B << B.last + a * (1 + a) / 2
  end
end

def f(s)
  return -1 if B[-1] < s
  return 0 if B[0] >= s

  ok = N - 1
  ng = 0

  while (ok - ng).abs >= 2
    x = (ok + ng) / 2

    if B[x] >= s
      ok = x
    else
      ng = x
    end
  end

  ok
end

def g(s, a, offset = 0)
  return 1 if offset + 1 >= s

  ng = 1
  ok = a

  while (ok - ng).abs >= 2
    x = (ok + ng) / 2
    b = offset + x * (1 + x) / 2

    if b >= s
      ok = x
    else
      ng = x
    end
  end

  ok
end

S.each do |s|
  idx = f(s)

  if idx == -1
    puts -1
  else
    res = if idx == 0
            g(s, A[0])
          else
            g(s, A[idx], A[idx - 1])
          end

    if idx == 0
      puts res
    else
      puts C[idx - 1] + res
    end
  end
end
0