QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#599947 | #9250. Max GCD | gaofeng | WA | 1456ms | 634892kb | C++20 | 4.2kb | 2024-09-29 13:33:18 | 2024-09-29 13:33:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
const int N=1e6+10;
const int mod=998244353;
const int INF = 0x3f3f3f3f;
const ll INFll = 0x3f3f3f3f3f3f3f3f;
#define endl "\n"
#define x first
#define y second
vector<vector<int>>adj(N);
int a[N];
struct Node{
int l, r, w;
}query[N];
int ans[N];
struct gfseg { // 区间最值
int a[N], _n;
struct Node {
int l, r;
int max;
}tr[N];
void pushup(int u) {
tr[u].max = max(tr[u << 1].max, tr[u << 1 | 1].max);
}
void build(int u, int l, int r) {
if(l == r) {
tr[u] = {l, r, a[l]};
return;
}
int mid = l + r >> 1;
tr[u] = {l, r, -INF};
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void init(vector<int> _a) {
_n = _a.size();
for(int i = 1; i <= _n; i ++) {
a[i] = _a[i - 1];
}
build(1, 1, _n);
}
void modify(int u, int x, int y) {
if(tr[u].l == tr[u].r && tr[u].l == x) {
tr[u].max = max(tr[u].max, y);
return;
}
int mid = tr[u].l + tr[u].r >> 1;
if(x <= mid) modify(u << 1, x, y);
if(x > mid) modify(u << 1 | 1, x, y);
pushup(u);
}
int querymax(int u, int l, int r) {
if(l <= tr[u].l && tr[u].r <= r) {
return tr[u].max;
}
int res = -INF;
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid) res = max(querymax(u << 1, l, r), res);
if(r > mid) res = max(querymax(u << 1 | 1, l, r), res);
return res;
}
void debug() {
for(int i = 1; i <= _n; i ++)
cout << querymax(1, i, i) << " "; cout << endl;
}
}tr;
vector<Node> no2[N];
void solve()
{
int n, q; cin >> n >> q;
for(int i = 1; i <= n; i ++) {
cin >> a[i];
// a[i] = rand();
int x = a[i];
for(int j = 1; j * j <= x; j ++) {
if(x % j == 0) {
adj[j].push_back(i);
if(j * j != x)
adj[x/j].push_back(i);
}
}
}
// for(int i = N; i >= 1; i --) {
// int l = 0, r = 0;
// for(int l = 0; l < adj[i].size(); l ++) {
// while(r <= l + 1 && r < adj[i].size()) r ++;
// while(r < adj[i].size()) {
// if(r < adj[i].size() && adj[i][l + 1] - adj[i][l] <= adj[i][r] - adj[i][l + 1]) {
// no2[adj[i][l]].push_back({adj[i][l], adj[i][r], i});
// break;
// }
// else r ++;
// }
// }
// }
for(int i = 1; i < N; i ++) {
// for(auto id : adj[i]) cout << id << " "; cout << endl;
for(int j = 0; j + 2 < adj[i].size(); j ++) {
if(adj[i][j + 1] - adj[i][j] <= adj[i][j + 2] - adj[i][j + 1])
no2[adj[i][j]].push_back({adj[i][j], adj[i][j + 2], i});
}
}
vector<int>T; for(int i = 1; i <= n + 10; i ++) T.push_back(0); tr.init(T);
for(int i = 0; i < q; i ++) {
cin >> query[i].l >> query[i].r; query[i].w = i;
}
sort(query , query + q, [&](Node a, Node b){
return a.l > b.l;
});
vector<int>mx (n + 10, 0);
int j = 0;
for(int L = n; L >= 0; L --) {
for(auto [x, y, w] : no2[L]){
// cout << x << " " << y << " " << w << endl;
// cout << no[i].l << " " << no[i].r << " " << no[i].w << endl;
if(mx[y] < w) {
tr.modify(1, y, w);
mx[y] = w;
}
}
while(j < q && query[j].l >= L) {
ans[query[j].w] = tr.querymax(1,1,query[j].r);
j ++;
}
}
// tr.debug();
// cout << i << " " << j << endl;
for(int i = 0; i < q; i ++) cout << ans[i] << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cout << setprecision(11) << fixed;
int t;t=1;
//cin>>t;
for(int i=1;i<=t;i++){
//printf("Case %d: ",i);
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 37012kb
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: 0
Accepted
time: 1456ms
memory: 634892kb
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:
ok 100000 tokens
Test #3:
score: -100
Wrong Answer
time: 416ms
memory: 88012kb
input:
150000 100000 716844 340255 836453 422971 389959 56166 837543 724949 336855 860372 579302 812222 849774 845846 999555 136871 624002 100905 529143 187215 582397 95964 363772 534762 258007 132867 753342 300681 770692 654005 397230 267857 21953 347450 248776 397101 768172 868404 612257 885884 270063 45...
output:
962054 997805 890994 997805 302526 997805 997805 977481 977481 962054 997805 977481 187969 962054 389779 753782 385422 267290 299894 219398 73821 400069 997805 722583 58309 962054 977481 997805 963049 997805 413562 994191 997805 426490 997805 962054 962054 753782 484075 997805 994191 299879 761650 7...
result:
wrong answer 3037th words differ - expected: '11283', found: '7183'