QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#691850 | #9250. Max GCD | YipChip | TL | 2ms | 5648kb | C++23 | 2.3kb | 2024-10-31 13:14:01 | 2024-10-31 13:14:03 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, q, len;
int a[N], w[N], sum[N];
vector<int> factor[N];
struct Node
{
int op, l, r, d, id;
};
vector<Node> segment;
bool cmp1(Node x, Node y)
{
if (x.l == y.l) return x.op < y.op;
return x.l < y.l;
}
bool cmp2(Node x, Node y)
{
if (x.op == y.op) return x.id < y.id;
return x.op < y.op;
}
int get(int x)
{
return (x - 1) / len;
}
int query(int l, int r)
{
int res = 0;
if (get(l) == get(r))
for (int i = l; i <= r; i ++ )
res = max(res, w[i]);
else
{
int i = l, j = r;
while (get(i) == get(l)) res = max(res, w[i]), i ++ ;
while (get(j) == get(r)) res = max(res, w[j]), j -- ;
for (i = get(i); i <= get(j); i ++ ) res = max(res, sum[i]);
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> n >> q;
int maxx = 0;
for (int i = 1; i <= n; i ++ ) cin >> a[i], maxx = max(maxx, a[i]);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j * j <= a[i]; j ++ )
if (a[i] % j == 0)
if (j * j != a[i]) factor[j].push_back(i), factor[a[i] / j].push_back(i);
else factor[j].push_back(i);
for (int i = 1; i <= q; i ++ )
{
int l, r;
cin >> l >> r;
segment.push_back({0, l, r, 0, i});
}
for (int i = 1; i <= maxx; i ++ )
{
int last = factor[i].size();
for (int l = last - 3, r = last - 1; l >= 0; r -- )
{
while (l >= 0 && factor[i][r] - factor[i][l + 1] < factor[i][l + 1] - factor[i][l]) l -- ;
if (l < 0) break;
segment.push_back({1, factor[i][l], factor[i][r], i, 0});
}
}
len = sqrt(n);
sort(segment.begin(), segment.end(), cmp1);
for (int i = segment.size() - 1; i >= 0; i -- )
{
if (segment[i].op)
{
sum[get(segment[i].r)] = max(sum[get(segment[i].r)], segment[i].d);
w[segment[i].r] = max(w[segment[i].r], segment[i].d);
}
else segment[i].d = query(segment[i].l, segment[i].r);
}
sort(segment.begin(), segment.end(), cmp2);
for (int i = 0; i < q; i ++ ) cout << segment[i].d << "\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 5648kb
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...