QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#555499 | #9250. Max GCD | ucup-team3924 | TL | 279ms | 124048kb | C++23 | 4.5kb | 2024-09-10 01:15:49 | 2024-09-10 01:15:49 |
Judging History
answer
#include <bits/stdc++.h>
#include <bits/extc++.h>
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const int MAX_E = 1000000;
struct chash{
const uint64_t C = (long long)(4e18 * acos(0)) | 71;
long long operator()(long long x)const { return __builtin_bswap64(x*C); }
};
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);
__gnu_pbds::gp_hash_table<long long, __gnu_pbds::gp_hash_table<long long, bool, chash>, chash> in_set;
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: 279ms
memory: 124048kb
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...