結果
| 問題 |
No.1890 Many Sequences Sum Queries
|
| ユーザー |
trineutron
|
| 提出日時 | 2022-04-04 22:37:46 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 753 bytes |
| コンパイル時間 | 2,165 ms |
| コンパイル使用メモリ | 191,928 KB |
| 最終ジャッジ日時 | 2025-01-28 15:07:45 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 23 WA * 2 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:5:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
5 | scanf("%ld%ld", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:8:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
8 | scanf("%ld", &a[i]);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:20:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
20 | scanf("%ld", &s);
| ~~~~~^~~~~~~~~~~
ソースコード
#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] = c[i] + a[i] * (a[i] + 1) / 2;
}
for (long i = 0; i < q; i++) {
long s;
scanf("%ld", &s);
long l_idx = std::upper_bound(c, c + n + 1, s) - c;
long l = c_idx[l_idx - 1] - 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);
}
}
trineutron