QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#613362 | #9250. Max GCD | ucup-team1001 | TL | 648ms | 141044kb | C++23 | 3.0kb | 2024-10-05 13:57:42 | 2024-10-05 13:59:49 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
//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
void solve() {
//求 1e6所以的因子
int lim = 1000000;
vector<vector<int>> f(lim + 1);
for (int i = 1; i <= lim; i++) {
for (int j = i; j <= lim; j += i) {
f[j].push_back(i);
}
}
vector<vector<int>> pos(lim + 1);
int n, q;
cin >> n >> q;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
// cerr << a[i] << " ";
for (auto &j: f[a[i]]) {
pos[j].push_back(i);
}
}
// cerr << "pos" << endl;
auto check = [&](int x, vector<int> &a) {
if (a.empty()) return -1;
if (a.back() < x) return -1;
int l = 0, r = a.size() - 1;
while (l < r) {
int mid = (l + r) >> 1;
if (a[mid] >= x) r = mid;
else l = mid + 1;
}
return a[l];
};
// 记录更新节点
vector<vector<pair<int, int>>> add(n + 1);
for (int i = 1; i <= n; i++) {
//对于每个因子
for (auto &j: f[a[i]]) {
if (j == 1) continue;
//找到下个位置
int next = check(i + 1, pos[j]);
if (next == -1) {
continue;
}
int dis = next - i;
next = check(next + dis, pos[j]);
if (next == -1) {
continue;
}
// cerr << i << " " << next << " " << j << endl;
add[i].emplace_back(next, j);
}
reverse(add[i].begin(), add[i].end());
}
vector<int> fiw(n + 1, 1);
auto update = [&](int x, int y) {
for (int i = x; i <= n; i += i & -i) {
if (fiw[i] >= y) break;
fiw[i] = y;
// fiw[i] = max(fiw[i], y);
}
};
auto query = [&](int x) {
int res = 0;
for (int i = x; i; i -= i & -i) {
res = max(res, fiw[i]);
}
return res;
};
vector<int> ans(q);
vector<vector<pair<int, int>>> querys(n + 1);
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
// cerr << l << " " << r << endl;
querys[l].emplace_back(r, i);
}
for (int i = n; i >= 1; i--) {
for (auto [r, v]: add[i]) {
// cerr<< "update " << i << " " << r << " " << v << endl;
update(r, v);
}
for (auto [r, id]: querys[i]) {
// cerr << "query " << i << " " << r << " " << id << endl;
ans[id] = query(r);
}
}
for (auto i: ans) {
cout << i << endl;
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
// freopen("C:\\Users\\DELLPC\\Desktop\\code\\T\\input.txt", "r", stdin);
// freopen("C:\\Users\\DELLPC\\Desktop\\code\\T\\output.txt", "w", stdout);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 648ms
memory: 141044kb
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...