QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#186480 | #6405. Barkley | ucup-team228 | WA | 5ms | 7960kb | C++20 | 2.9kb | 2023-09-23 22:33:07 | 2023-09-23 22:33:07 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T>
struct sparse_table{
static const int N = 1e5 + 10, L = 20;
T a[N];
T d[N][L], rem[N];
void build(int n) {
for (int i = 1; i <= n; i++) { d[i][0] = i; }
for (int j = 1; j <= L - 1; j++) {
for (int i = 1; i <= n; i++) {
if (i + (1 << j) - 1 > n) { continue; }
d[i][j] = __gcd(a[d[i][j - 1]], a[d[i + (1 << (j - 1))][j - 1]]);
}
}
for (int i = 1; i <= n; i++) { rem[i] = log2(i); }
}
T get(int l, int r) {
int lg = rem[r - l + 1];
return __gcd(a[d[l][lg]], a[d[r - (1 << lg) + 1][lg]]);
}
};
sparse_table<ll> table;
const int N = 1e5 + 10;
ll a[N];
vector<pair<int, ll>> rig[N];
void precalc(int n) {
for (int i = 1; i <= n; i++) {
table.a[i] = a[i];
}
table.build(n);
rig[n] = {{n, a[n]}};
for (int l = n - 1; l >= 1; l--) {
rig[l] = {{l, a[l]}};
for (auto [r, val] : rig[l + 1]) {
val = __gcd(val, a[l]);
if (val == rig[l].back().second) {
rig[l].back().first = r;
} else {
rig[l].emplace_back(r, val);
}
}
}
}
vector<ll> get(int l, int r, int k) {
assert(r - l + 1 >= k);
if (k == r - l + 1) {
return {0};
} else if (k == 0) {
return {table.get(l, r)};
} else {
vector<int> cur;
for (auto [x, val] : rig[l]) {
for (int i = max(l - 1, x - 3); i <= x + 1; i++) {
if (i + 1 <= r - k + 1) {
cur.push_back(i + 1);
}
}
}
sort(cur.begin(), cur.end());
cur.erase(unique(cur.begin(), cur.end()), cur.end());
vector<ll> res;
for (int i = 0, j = 0; i < cur.size(); i++) {
ll val = 0;
if (cur[i] >= l + 1) {
while (rig[l][j].first < cur[i] - 1) {
j++;
}
val = rig[l][j].second;
}
auto nxt = get(cur[i] + 1, r, k - 1);
for (ll s : nxt) {
res.push_back(__gcd(val, s));
}
}
return res;
}
}
ll solve(int l, int r, int k) {
auto res = get(l, r, k);
ll ans = 0;
for (ll x : res) {
ans = max(ans, x);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
precalc(n);
while (q--) {
int l, r, k;
cin >> l >> r >> k;
cout << solve(l, r, k) << "\n";
}
#ifdef LOCAL
cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 7960kb
input:
4 4 3 2 6 4 1 3 1 2 4 1 1 4 2 1 4 3
output:
3 2 3 6
result:
ok 4 number(s): "3 2 3 6"
Test #2:
score: -100
Wrong Answer
time: 5ms
memory: 7692kb
input:
100 10000 7 25 33 17 13 33 24 29 11 1 3 19 2 20 33 23 14 24 15 12 3 1 5 13 6 35 15 21 10 34 31 19 7 33 17 26 26 1 19 21 31 5 29 20 18 32 19 18 19 31 11 26 6 19 2 26 23 1 4 2 31 21 29 30 1 14 20 23 14 32 4 34 13 29 5 26 24 29 28 5 26 26 21 19 2 33 2 31 30 3 23 24 26 32 36 21 21 11 5 9 56 57 1 90 97 1...
output:
26 7 13 1 7 1 3 7 31 7 7 24 24 12 26 24 1 13 1 7 7 29 7 24 24 1 7 7 17 13 24 7 4 24 2 7 24 7 7 24 24 13 2 0 7 7 25 2 7 24 24 21 3 24 24 0 7 19 7 7 24 3 13 7 1 1 1 7 24 1 13 7 1 1 7 7 0 7 1 7 7 24 13 7 1 7 33 7 1 7 24 1 7 24 7 7 26 24 13 7 7 1 1 7 33 2 24 7 33 7 7 7 24 24 7 7 24 26 1 7 3 5 1 7 7 1 13...
result:
wrong answer 2nd numbers differ - expected: '1', found: '7'