QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#679281 | #9250. Max GCD | louhao088# | TL | 2ms | 10092kb | C++23 | 2.2kb | 2024-10-26 17:16:37 | 2024-10-26 17:16:38 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
struct query {
int l, r, id;
}qs[N];
struct opra {
int i, k, d;
};
vector <opra> f;
int n, q, a[N];
const int M = 1e6 + 100;
vector <int> b[M];
int sta[N], val[N], ans[N];
bool cmp1(query x, query y) {
return x.r < y.r;
}
bool cmp2(opra x, opra y) {
return x.k < y.k;
}
struct SZSZ {
int f[N];
void add(int x, int d) {
for (; x; x -= (x & (-x))) f[x] = max(f[x], d);
}
int query(int x) {
int re = 0;
for (; x <= n; x += (x & (-x))) re = max(re, f[x]);
return re;
}
}T;
int main() {
scanf("%d %d", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
for (int j = 1; j * j <= a[i]; j++)
if (a[i] % j == 0) {
b[j].push_back(i);
if (j * j != a[i]) b[a[i] / j].push_back(i);
}
}
for (int i = 1; i <= q; i++) {
scanf("%d %d", &qs[i].l, &qs[i].r);
qs[i].id = i;
}
sort(qs + 1, qs + q + 1, cmp1);
// return 0;
for (int d = 1; d <= 1000000; d++) {
sta[0] = 0;
for (int x = 0; x < (int)b[d].size() - 1; x++) {
int i = b[d][x], j = b[d][x + 1];
int dis = 2 * j - i;
while (sta[0] && dis <= val[sta[0]]) sta[0]--;
sta[++sta[0]] = i;
val[sta[0]] = dis;
}
int zz = 0;
for (int x = 0; x < b[d].size(); x++) {
int k = b[d][x];
while (zz < sta[0] && val[zz + 1] <= k) zz++;
if (zz && k >= val[zz]) {
f.push_back((opra){sta[zz], k, d});
}
}
}
sort(f.begin(), f.end(), cmp2);
// return 0;
int zz = 0, zzq = 1;
for (int i = 1; i <= n; i++) {
while (zz < f.size() && f[zz].k == i) {
T.add(f[zz].i, f[zz].d);
zz++;
}
while (zzq <= q && qs[zzq].r == i) {
ans[qs[zzq].id] = T.query(qs[zzq].l);
zzq++;
}
}
for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 10092kb
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: -100
Time Limit Exceeded
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...