結果

問題 No.1890 Many Sequences Sum Queries
ユーザー simansiman
提出日時 2022-06-19 00:38:03
言語 Ruby
(3.3.0)
結果
AC  
実行時間 454 ms / 2,000 ms
コード長 967 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-04-19 00:52:07
合計ジャッジ時間 6,060 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 454 ms
13,056 KB
testcase_01 AC 347 ms
12,928 KB
testcase_02 AC 91 ms
12,288 KB
testcase_03 AC 91 ms
12,160 KB
testcase_04 AC 91 ms
12,160 KB
testcase_05 AC 90 ms
12,160 KB
testcase_06 AC 91 ms
12,416 KB
testcase_07 AC 223 ms
12,672 KB
testcase_08 AC 119 ms
12,288 KB
testcase_09 AC 126 ms
12,288 KB
testcase_10 AC 301 ms
13,056 KB
testcase_11 AC 194 ms
12,544 KB
testcase_12 AC 327 ms
12,672 KB
testcase_13 AC 348 ms
12,672 KB
testcase_14 AC 227 ms
12,416 KB
testcase_15 AC 90 ms
12,160 KB
testcase_16 AC 90 ms
12,160 KB
testcase_17 AC 91 ms
12,160 KB
testcase_18 AC 92 ms
12,160 KB
testcase_19 AC 91 ms
12,032 KB
testcase_20 AC 93 ms
12,160 KB
testcase_21 AC 91 ms
12,416 KB
testcase_22 AC 92 ms
12,288 KB
testcase_23 AC 91 ms
12,160 KB
testcase_24 AC 93 ms
12,160 KB
testcase_25 AC 92 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], B[idx - 1])
          end

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