結果

問題 No.1890 Many Sequences Sum Queries
ユーザー trineutron
提出日時 2022-04-04 22:36:21
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 772 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,206 ms
コンパイル使用メモリ 209,928 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-06-25 16:38:18
合計ジャッジ時間 2,522 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

int main() {
	long n, q, len = 0;
	scanf("%ld%ld", &n, &q);
	long a[100];
	for (long i = 0; i < n; i++) {
		scanf("%ld", &a[i]);
		len += a[i];
	}
	long c_idx[101], c[101];
	c_idx[0] = 0;
	c[0] = 0;
	for (long i = 0; i < n; i++) {
		c_idx[i + 1] = c_idx[i] + a[i];
		c[i + 1] = std::min(1000000000L, c[i] + a[i] * (a[i] + 1) / 2);
	}
	for (long i = 0; i < q; i++) {
		long s;
		scanf("%ld", &s);
		long l_idx = std::lower_bound(c, c + n + 1, s) - c;
		long l = c_idx[l_idx - 1], r = c_idx[l_idx];
		while (r - l > 1) {
			long mid = (l + r) / 2;
			long d = mid - c_idx[l_idx - 1];
			if (c[l_idx - 1] + d * (d + 1) / 2 >= s) {
				r = mid;
			} else {
				l = mid;
			}
		}
		if (c[n] < s or r > len) {
			r = -1;
		}
		printf("%ld\n", r);
	}
}
0