QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#186447 | #6405. Barkley | ucup-team228 | WA | 1ms | 8532kb | C++20 | 3.0kb | 2023-09-23 20:27:39 | 2023-09-23 20:27:39 |
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<ll> res;
{
auto nxt = get(l + 1, r, k - 1);
for (ll x : nxt) {
res.push_back(x);
}
}
{
auto nxt = get(l, r - 1, k - 1);
for (ll x : nxt) {
res.push_back(x);
}
}
for (int i = 0; i < rig[l].size(); i++) {
auto [x, val] = rig[l][i];
int y = min(x + 1, r - k + 1);
for (int j = i; j >= 0; j--) {
if (rig[l][j].first >= y - 1) {
val = rig[l][j].second;
break;
}
}
auto nxt = get(y + 1, r, k - 1);
for (auto nxt_val : nxt) {
res.push_back(__gcd(val, nxt_val));
}
}
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
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7780kb
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: 0ms
memory: 8532kb
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 24 13 1 24 7 31 7 7 24 24 12 26 24 1 13 7 7 7 29 7 24 24 1 7 7 25 13 24 7 7 24 6 7 24 7 7 24 24 13 7 3 7 7 25 2 7 24 24 21 3 24 24 1 7 19 7 7 24 33 13 7 7 1 1 7 24 24 13 7 1 7 24 7 7 7 24 7 7 24 13 7 1 7 33 7 1 7 24 13 33 24 7 7 26 24 13 7 7 1 1 7 33 7 24 7 33 24 7 7 24 24 7 25 24 26 24 7 7 ...
result:
wrong answer 2nd numbers differ - expected: '1', found: '7'