QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#555501 | #9250. Max GCD | ucup-team3924 | TL | 294ms | 123968kb | C++23 | 4.2kb | 2024-09-10 01:16:28 | 2024-09-10 01:16:29 |
Judging History
answer
#include <bits/stdc++.h>
const int MAX_E = 1000000;
struct Fenwick {
std::vector<int> T;
Fenwick(int n): T(1 + n) {}
static inline int lsb(int x) { return x & (-x); }
void update(int pos, int val) {
while (pos < T.size() && val > T[pos]) {
T[pos] = val;
pos += lsb(pos);
}
}
int query(int pos) {
int res = 0;
while (pos > 0) {
res = std::max(res, T[pos]);
pos -= lsb(pos);
}
return res;
}
};
const int BUFF = 1 << 14;
char buff[BUFF];
int curs = BUFF - 1;
char getchr() {
curs++;
if (curs == BUFF) {
fread(buff, 1, BUFF, stdin);
curs = 0;
}
return buff[curs];
}
int getnr() {
int nr = 0;
char chr = getchr();
while (!isdigit(chr)) chr = getchr();
while (isdigit(chr)) {
nr = nr * 10 + chr - '0';
chr = getchr();
}
return nr;
}
std::tuple<int, int, int> aux[300 * 150000];
std::tuple<int, int, int> to_left[300 * 150000];
int main() {
int n, q;
n = getnr();
q = getnr();
std::vector<int> cnt(1 + MAX_E, 0);
for (int d = 1; d <= MAX_E; d++) {
for (int i = d; i <= MAX_E; i += d)
cnt[i]++;
}
std::vector<std::vector<int>> divs(1 + MAX_E);
for (int i = 0; i <= MAX_E; i++)
divs[i].reserve(cnt[i]);
for (int d = 1; d <= MAX_E; d++)
for (int i = d; i <= MAX_E; i += d)
divs[i].push_back(d);
std::vector<int> input(1 + n);
std::vector<std::vector<int>> pos(1 + MAX_E);
for (int i = 0; i <= MAX_E; i++)
cnt[i] = 0;
std::pair<int, int> largest = {0, 0};
for (int i = 1; i <= n; i++) {
input[i] = getnr();
for (auto it: divs[input[i]])
cnt[it]++;
}
for (int i = 1; i <= MAX_E; i++)
pos[i].reserve(cnt[i]);
for (int i = 1; i <= n; i++) {
for (auto it: divs[input[i]])
pos[it].push_back(i);
}
for (int i = 1; i <= MAX_E; i++)
largest = std::max(largest, std::make_pair((int)divs[i].size(), i));
std::vector<std::vector<std::pair<int, int>>> to_left(1 + n);
std::vector<std::vector<std::pair<int, int>>> queries(1 + n);
std::unordered_map<int, std::unordered_map<int, bool>> in_set(1 + n);
int top_aux = 0;
for (int d = MAX_E; d >= 1; d--) {
int last_p = 1;
for (int i = 1; i < pos[d].size(); i++) {
int first = pos[d][i - 1];
int l = i, r = pos[d].size();
while (r - l > 1) {
int mid = (l + r) / 2;
if (pos[d][i] - pos[d][i - 1] <= pos[d][mid] - pos[d][i])
r = mid;
else
l = mid;
}
last_p = r;
if (last_p < pos[d].size()) {
int last = pos[d][last_p];
if (in_set[first].find(last) == in_set[first].end()) {
//if (in_set.find({first, last}) == in_set.end()) {
//aux[top_aux++] = {first, last, d};
to_left[last].push_back({d, first});
in_set[first][last] = true;
}
}
}
}
//std::cerr << "set elements: " << in_set.size() << "\n";
std::mt19937 rng(1);
std::vector<int> res(q);
for (int i = 0; i < q; i++) {
int l, r;
l = getnr();
r = getnr();
queries[r].push_back({i, l});
}
Fenwick T(n);
int last_update = 0;
for (int i = 1; i <= n; i++) {
//while (last_update < top_aux && std::get<2>(to_left[last_update]) == i) {
// T.update(n + 1 - std::get<1>(to_left[last_update]), std::get<0>(to_left[last_update]));
// last_update++;
//}
//
for (auto it: to_left[i]) {
T.update(n + 1 - it.second, it.first);
}
for (auto it: queries[i]) {
int id = it.first;
int pos = it.second;
res[id] = T.query(n + 1 - pos);
}
}
for (int i = 0; i < q; i++)
std::cout << res[i] << "\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 294ms
memory: 123968kb
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...
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...