結果

問題 No.1890 Many Sequences Sum Queries
ユーザー trineutrontrineutron
提出日時 2022-04-04 21:50:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 760 bytes
コンパイル時間 2,182 ms
コンパイル使用メモリ 201,404 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-04 06:53:59
合計ジャッジ時間 3,439 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
6,812 KB
testcase_01 AC 45 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 16 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 5 ms
6,940 KB
testcase_10 AC 38 ms
6,944 KB
testcase_11 AC 20 ms
6,944 KB
testcase_12 AC 29 ms
6,944 KB
testcase_13 AC 31 ms
6,944 KB
testcase_14 AC 17 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int main() {
	int n, q, len = 0;
	scanf("%d%d", &n, &q);
	int a[100];
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		len += a[i];
	}
	long c_idx[101], c[101];
	c_idx[0] = 0;
	c[0] = 0;
	for (int i = 0; i < n; i++) {
		c_idx[i + 1] = c_idx[i] + a[i];
		c[i + 1] = c[i] + a[i] * (a[i] + 1) / 2;
	}

	auto total = [&](int idx) {
		int k = std::upper_bound(c_idx, c_idx + n + 1, idx) - c_idx;
		long d = idx - c_idx[k - 1];
		return c[k - 1] + d * (d + 1) / 2;
	};

	for (int i = 0; i < q; i++) {
		int s;
		scanf("%d", &s);
		int l = 0, r = len + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if (total(mid) >= s) {
				r = mid;
			} else {
				l = mid;
			}
		}
		if (r > len) {
			r = -1;
		}
		printf("%d\n", r);
	}
}
0