QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#552069#9250. Max GCDucup-team3734#TL 594ms120192kbC++233.2kb2024-09-07 20:17:042024-09-07 20:17:05

Judging History

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

  • [2024-09-07 20:17:05]
  • 评测
  • 测评结果:TL
  • 用时:594ms
  • 内存:120192kb
  • [2024-09-07 20:17:04]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long u64;
typedef double lf;

const int maxv = 1000500;

vector<int> divs[maxv];
vector<int> occ[maxv];

vector<int> ans;

struct event {
    int type;
    int x, y;
    int idx;
    int val;

    bool operator<(const event& other) const {
        return make_pair(x, type) > make_pair(other.x, other.type);
    }
};

vector<event> events;

const int maxn = 150500;
const int hsz = 1 << 18;
const int tsz = 1 << 19;

int t[tsz];

void put(int v, int tl, int tr, int l, int r, int val) {
    if (l >= r) {
        return;
    }
    if (l == tl && r == tr) {
        t[v] = max(t[v], val);
        return;
    }
    int tm = (tl + tr) / 2;
    put(v * 2, tl, tm, l, min(r, tm), val);
    put(v * 2 + 1, tm, tr, max(l, tm), r, val);
}

int get(int v, int tl, int tr, int pos) {
    if (tl + 1 == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    if (pos < tm) {
        return max(t[v], get(v * 2, tl, tm, pos));
    } else {
        return max(t[v], get(v * 2 + 1, tm, tr, pos));
    }
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    
    for (int i = 1; i < maxv; i++) {
        for (int j = i; j < maxv; j += i) {
            divs[j].push_back(i);
        }
    }
    set<int, greater<int>> ds;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        for (int d : divs[a[i]]) {
            ds.insert(d);
            occ[d].push_back(i);
        }
    }
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        events.push_back({
            .type = 0,
            .x = l,
            .y = r,
            .idx = i,
            .val = 0,
        });
    }
    ans.resize(q);
    
    for (int d : ds) {
        const auto& cur = occ[d];
        for (size_t i = 0; i + 1 < cur.size(); ++i) {
            int a = cur[i];
            int b = cur[i + 1];
            int c = 2 * b - a;
            auto it = lower_bound(cur.begin(), cur.end(), c);
            if (it != cur.end()) {
                events.push_back({
                    .type = 1,
                    .x = a,
                    .y = *it,
                    .idx = -1,
                    .val = d,
                });
            }
        }
    }

    fill(t, t + tsz, 0);
    sort(events.begin(), events.end());
    for (const auto& e : events) {
        if (e.type == 0) {
            ans[e.idx] = get(1, 0, hsz, e.y);
            // cerr << "get " << e.y << " " << ans[e.idx] << "\n";
        } else {
            put(1, 0, hsz, e.y, hsz, e.val);
            // cerr << "put " << e.y << " " << e.val << "\n";
            // for (int i = 0; i < n; ++i) {
            //     cerr << get(1, 0, hsz, i) << " ";
            // }
            // cerr << endl;
        }
    }

    for (int i = 0; i < q; ++i) {
        cout << ans[i] << "\n";
    }
}

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false);
    int t = 1;
    // cin >> t;
    for (int i = 0; i < t; i++) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 594ms
memory: 120192kb

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:


result: