QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#649792#9250. Max GCDFDUdululuTL 2337ms447536kbC++202.1kb2024-10-18 10:23:132024-10-18 10:23:14

Judging History

你现在查看的是最新测评结果

  • [2024-10-18 10:23:14]
  • 评测
  • 测评结果:TL
  • 用时:2337ms
  • 内存:447536kb
  • [2024-10-18 10:23:13]
  • 提交

answer

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

const int N = 1e6 + 5;

int n, q;
int a[N];
struct seq {
    int r, id;
};
vector<seq> s[N], t[N];
int maxn, ans[N], vis[N];
vector<int> d[N], pos;

int c[N];
int lowbit(int n) {
    return n & (-n);
}
void modify(int pos, int x) {
    while (pos <= n) {
        c[pos] = max(c[pos], x);
        pos += lowbit(pos);
    }
}
int query(int pos) {
    int ans = 0;
    while (pos) {
        ans = max(ans, c[pos]);
        pos -= lowbit(pos);
    }
    return ans;
}

void solve() {
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        maxn = max(maxn, a[i]);
    }
    for (int i = 1; i <= q; i++) {
        int l, r;
        cin >> l >> r;
        s[l].push_back({r, i});
        ans[i] = (r - l > 1);
    }

    for (int i = 2; i <= maxn; i++) {
        for (int j = i; j <= maxn; j += i)
            d[j].push_back(i);
    }
    for (int i = 2; i <= n; i++) {
        for (auto j : d[a[i]])
            vis[j] = 1;
    }
    for (int i = 2; i <= maxn; i++) {
        if (!vis[i])
            continue;
        pos.clear();
        for (int j = 1; j <= n; j++)
            if (a[j] % i == 0)
                pos.push_back(j);
        for (int j = 1; j < pos.size() - 1; j++) {
            int a = j - 1, b = j, c = 0;
            int val = 2 * pos[b] - pos[a];
            c = lower_bound(pos.begin() + b, pos.end(), val) - pos.begin();
            if (c == pos.size())
                continue;
            t[pos[a]].push_back({pos[c], i});
        }
    }
    for (int i = n; i >= 1; i--) {
        for (auto [r, val] : t[i])
            modify(r, val);
        for (auto [r, id] : s[i])
            ans[id] = max(ans[id], query(r));
    }
    for (int i = 1; i <= q; i++)
        cout << ans[i] << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 9828kb

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: 2337ms
memory: 447536kb

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
Time Limit Exceeded

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:


result: