QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#738629 | #9250. Max GCD | SHOJIG | AC ✓ | 2382ms | 611400kb | C++20 | 2.9kb | 2024-11-12 19:35:31 | 2024-11-12 19:35:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
// #define int long long
#define pb push_back
#define endl '\n'
using ll = long long;
const ll mod = 1000000007;
const ll INF = 1ll << 60;
const int inf = 1 << 30;
const int N = 150010;
const int B = 388;
int n, q, a[N];
void solve() {
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
const int M = *max_element(a + 1, a + n + 1);
vector<vector<int>> d(M + 1);
for (int i = 1; i <= M; ++i) {
for (int j = i; j <= M; j += i) {
d[j].pb(i);
}
}
vector<vector<int>> pos(M + 1);
for (int i = 1; i <= n; ++i) {
for (auto x : d[a[i]]) { // x | a[i]
pos[x].pb(i);
}
}
vector<vector<array<int, 2>>> e(n + 1); // e[l]: (r, gcd)
for (int x = 1; x <= M; ++x) {
if (pos[x].size() < 3) continue;
for (int j = 1; j < pos[x].size(); ++j) {
// k >= 2 * j - i
auto it = lower_bound(all(pos[x]), 2 * pos[x][j] - pos[x][j - 1]);
if (it != pos[x].end()) {
e[pos[x][j - 1]].pb({*it, x});
}
}
}
vector<array<int, 3>> ask;
for (int i = 1; i <= q; ++i) {
int l, r; cin >> l >> r;
ask.pb({l, r, i});
}
sort(all(ask), greater<array<int, 3>>());
vector<int> ans(q + 1);
vector<int> mx(n + 1), id(n + 1), L(n + 1), R(n + 1);
for (int i = 1; i <= n; ++i) {
id[i] = (i - 1) / B + 1;
if (id[i] != id[i - 1]) {
L[id[i]] = i;
R[id[i]] = min(n, i + B - 1);
}
}
vector<int> blk(id[n] + 1);
auto modify = [&](int x, int v) { // O(1)
// assert(x <= n);
mx[x] = max(mx[x], v);
blk[id[x]] = max(blk[id[x]], v);
};
auto bf_query = [&](int l, int r) {
int ans = 0;
for (int i = l; i <= r; ++i) ans = max(ans, mx[i]);
return ans;
};
auto query = [&](int l, int r) { // max(1...x) O(sqrt(n))
int ans = 0;
if (id[l] == id[r]) {
return bf_query(l, r);
}
ans = max(ans, bf_query(l, R[id[l]]));
ans = max(ans, bf_query(L[id[r]], r));
for (int i = id[l] + 1; i <= id[r] - 1; ++i) {
ans = max(ans, blk[i]);
}
return ans;
};
for (int j = n; auto [ql, qr, qid] : ask) {
while (j >= ql) {
for (auto [r, d] : e[j]) {
modify(r, d);
}
j -= 1;
}
ans[qid] = query(ql, qr);
}
for (int i = 1; i <= q; ++i) {
cout << ans[i] << endl;
}
}
signed main(void) {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int Test = 1;
// cin >> Test;
while (Test--) solve();
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
input:
8 8 8 24 4 6 6 7 3 3 1 5 2 6 3 7 5 8 4 8 1 3 2 5 3 8
output:
4 2 3 1 3 4 2 3
result:
ok 8 tokens
Test #2:
score: 0
Accepted
time: 2382ms
memory: 611400kb
input:
150000 100000 982800 554400 665280 982800 997920 720720 786240 831600 997920 982800 786240 982800 942480 831600 887040 665280 831600 960960 786240 982800 786240 942480 665280 831600 942480 665280 982800 960960 960960 997920 720720 960960 960960 665280 982800 665280 982800 942480 786240 997920 554400...
output:
997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920...
result:
ok 100000 tokens
Test #3:
score: 0
Accepted
time: 830ms
memory: 182620kb
input:
150000 100000 716844 340255 836453 422971 389959 56166 837543 724949 336855 860372 579302 812222 849774 845846 999555 136871 624002 100905 529143 187215 582397 95964 363772 534762 258007 132867 753342 300681 770692 654005 397230 267857 21953 347450 248776 397101 768172 868404 612257 885884 270063 45...
output:
962054 997805 890994 997805 302526 997805 997805 977481 977481 962054 997805 977481 187969 962054 389779 753782 385422 267290 299894 219398 73821 400069 997805 722583 58309 962054 977481 997805 963049 997805 413562 994191 997805 426490 997805 962054 962054 753782 484075 997805 994191 299879 761650 7...
result:
ok 100000 tokens
Test #4:
score: 0
Accepted
time: 1395ms
memory: 362680kb
input:
150000 100000 956340 841620 591480 585312 733440 381480 918540 326160 154980 826272 873054 789264 642330 261576 504450 328500 462720 872040 463590 387720 240768 31320 774090 997668 380250 496692 858420 294060 286000 896448 987690 780216 945744 159432 825804 776880 946110 673530 583596 547400 335880 ...
output:
999999 999999 957720 999999 999460 952770 999432 999999 999999 999999 999999 999999 999999 999999 991380 999960 999999 999999 384480 999999 999432 999999 999999 994410 999999 999120 999999 999680 999460 999999 997704 151470 999999 999999 999999 999960 232190 999804 999960 999936 999460 998424 999999...
result:
ok 100000 tokens
Test #5:
score: 0
Accepted
time: 567ms
memory: 140600kb
input:
200 100 489060 226800 933660 924000 939120 291060 702240 913920 979440 778050 505440 611520 837900 662400 207900 829920 814320 194040 991200 463320 638400 288288 544320 609840 425040 855360 859320 327600 772200 918000 502320 693000 841500 502320 617760 371280 823680 900900 757680 823680 710640 59976...
output:
207900 823680 823680 131670 823680 19950 137700 55440 823680 823680 823680 48600 207900 207900 823680 823680 137700 231840 5796 1260 823680 231840 111384 540960 131670 823680 823680 207900 27720 823680 205920 231840 32760 207900 231840 207900 39312 207900 0 207900 151200 823680 31680 231840 207900 2...
result:
ok 100 tokens
Test #6:
score: 0
Accepted
time: 567ms
memory: 141584kb
input:
200 100 571200 926640 748800 936000 561600 601920 820800 571200 423360 514800 428400 995904 899640 899640 982080 993600 730800 803880 914760 687960 556920 458640 950400 884520 686400 873180 514800 411840 463680 327600 811440 875160 974400 485100 529200 963900 491400 785400 952560 611520 823680 68544...
output:
963900 963900 81900 237600 792000 85680 71400 458640 229320 212520 963900 4620 317520 52920 963900 383040 35700 52920 57120 383040 51480 914760 137280 963900 766080 21420 237600 85680 32760 317520 914760 914760 914760 191520 963900 154440 963900 963900 237600 963900 237600 766080 33600 963900 914760...
result:
ok 100 tokens
Test #7:
score: 0
Accepted
time: 1879ms
memory: 562140kb
input:
150000 100000 669240 987000 959400 760320 619080 715680 651000 297000 611520 816480 529200 936000 660660 856800 993720 678300 316800 951048 716040 748800 619080 939120 733590 974610 382200 870240 750750 423360 846300 750120 907200 355320 844800 582120 547200 986580 792792 291060 989520 696150 546000...
output:
999600 999600 999600 999600 999600 992160 999600 999600 999600 999600 999600 999600 999600 995904 997920 999600 999600 999600 999600 999600 999600 999600 999180 999600 999600 999600 999600 999180 999600 999600 999600 999600 999600 999600 999600 999600 999600 999600 999600 999600 999600 999600 999600...
result:
ok 100000 tokens
Extra Test:
score: 0
Extra Test Passed