QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#555503 | #9250. Max GCD | ucup-team3924 | WA | 1475ms | 333652kb | C++23 | 4.2kb | 2024-09-10 01:17:26 | 2024-09-10 01:17:26 |
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::vector<std::unordered_set<int>> 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];
while (last_p < pos[d].size() && pos[d][i] - pos[d][i - 1] > pos[d][last_p] - pos[d][i])
last_p++;
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].insert(last);
//std::cerr << first << "-" << last << " -> " << d << "\n";
}
}
}
}
//int sum = 0;
//for (auto it: in_set)
// sum += it.size();
//std::cerr << "set elements: " << sum << "\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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 314ms
memory: 123972kb
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
Wrong Answer
time: 1475ms
memory: 333652kb
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...
result:
wrong answer 5108th words differ - expected: '997920', found: '982800'