結果
| 問題 |
No.885 アマリクエリ
|
| コンテスト | |
| ユーザー |
qwewe
|
| 提出日時 | 2025-05-14 13:22:30 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 112 ms / 2,000 ms |
| コード長 | 3,134 bytes |
| コンパイル時間 | 924 ms |
| コンパイル使用メモリ | 96,236 KB |
| 実行使用メモリ | 6,272 KB |
| 最終ジャッジ日時 | 2025-05-14 13:24:29 |
| 合計ジャッジ時間 | 3,219 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <cmath>
// Fast I/O
void fast_io() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
}
int main() {
fast_io();
int N;
std::cin >> N;
std::vector<long long> A(N);
for (int i = 0; i < N; ++i) {
std::cin >> A[i];
}
int Q_count;
std::cin >> Q_count;
std::vector<int> queries_X(Q_count); // X_i up to 10^9, fits in int
for (int i = 0; i < Q_count; ++i) {
std::cin >> queries_X[i];
}
int B = static_cast<int>(sqrt(N));
if (B == 0) { // Handles N=0, though N >= 1 from constraints. If N=1, B=1.
B = 1;
}
int num_blocks = (N + B - 1) / B;
std::vector<long long> block_sum(num_blocks);
std::vector<long long> block_max_val(num_blocks);
// Lambda to initialize/recompute a block's sum and max value
auto recompute_block_info = [&](int block_idx) {
long long current_sum_for_block = 0;
long long current_max_for_block = 0;
int start_idx = block_idx * B;
int end_idx = std::min((block_idx + 1) * B, N); // Correctly handle last block
for (int j = start_idx; j < end_idx; ++j) {
current_sum_for_block += A[j];
if (A[j] > current_max_for_block) {
current_max_for_block = A[j];
}
}
block_sum[block_idx] = current_sum_for_block;
block_max_val[block_idx] = current_max_for_block;
};
// Initial computation of block summaries
for (int i = 0; i < num_blocks; ++i) {
recompute_block_info(i);
}
std::vector<long long> results;
results.reserve(Q_count); // Pre-allocate space for results
for (int qi = 0; qi < Q_count; ++qi) {
long long current_X = queries_X[qi]; // Use long long for X in case it's used with A[j] extensively, though int is fine.
// Modulo operand X_i constraint is int.
long long current_total_sum_for_query = 0;
for (int i = 0; i < num_blocks; ++i) {
if (block_max_val[i] < current_X) {
// All elements in this block are < X. No change to A[j] or block_sum.
current_total_sum_for_query += block_sum[i];
} else {
// At least one element in block MIGHT be >= X (or max_val itself is).
// Iterate through this block, update elements, and recompute sum/max for block.
int start_idx = i * B;
int end_idx = std::min((i + 1) * B, N);
for (int j = start_idx; j < end_idx; ++j) {
if (A[j] >= current_X) {
A[j] = A[j] % current_X;
}
}
// Recompute sum and max for this modified block
recompute_block_info(i);
current_total_sum_for_query += block_sum[i];
}
}
results.push_back(current_total_sum_for_query);
}
for (long long res : results) {
std::cout << res << "\n";
}
return 0;
}
qwewe