QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#613642#9250. Max GCDucup-team1001TL 553ms140944kbC++202.5kb2024-10-05 14:24:072024-10-05 14:24:11

Judging History

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

  • [2024-10-05 14:24:11]
  • 评测
  • 测评结果:TL
  • 用时:553ms
  • 内存:140944kb
  • [2024-10-05 14:24:07]
  • 提交

answer

#include "bits/stdc++.h"

using namespace std;

void solve() {
    //求 1e6所以的因子
    int lim = 1000000;
    vector<vector<int>> f(lim + 1);

    for (int i = 1; i <= lim; i++) {
        for (int j = i; j <= lim; j += i) {
            f[j].push_back(i);
        }
    }
    vector<vector<int>> pos(lim + 1);
    int n, q;
    cin >> n >> q;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        for (auto &j: f[a[i]]) {
            pos[j].push_back(i);
        }
    }

    auto check = [&](int x, vector<int> &a) {
        if (a.empty()) return -1;
        if (a.back() < x) return -1;
        int l = 0, r = a.size() - 1;
        while (l < r) {
            int mid = (l + r) >> 1;
            if (a[mid] >= x) r = mid;
            else l = mid + 1;
        }
        return a[l];
    };

    // 记录更新节点
    vector<vector<pair<int, int>>> add(n + 1);
    for (int i = 1; i <= n; i++) {
        //对于每个因子
        for (auto &j: f[a[i]]) {

            if (j == 1) continue;
            //找到下个位置
            int next = check(i + 1, pos[j]);
            if (next == -1) {
                continue;
            }
            int dis = next - i;
            next = check(next + dis, pos[j]);
            if (next == -1) {
                continue;
            }
            add[i].emplace_back(next, j);
        }
        reverse(add[i].begin(), add[i].end());
    }

    vector<int> fiw(n + 1, 1);
    auto update = [&](int x, int y) {
        for (int i = x; i <= n; i += i & -i) {
            if (fiw[i] >= y) break;
            fiw[i] = y;
//            fiw[i] = max(fiw[i], y);
        }
    };

    auto query = [&](int x) {
        int res = 0;
        for (int i = x; i; i -= i & -i) {
            res = max(res, fiw[i]);
        }
        return res;
    };




    vector<int> ans(q);
    vector<vector<pair<int, int>>> querys(n + 1);
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        querys[l].emplace_back(r, i);
    }

    for (int i = n; i >= 1; i--) {
        for (auto [r, v]: add[i]) {
            update(r, v);
        }

        for (auto [r, id]: querys[i]) {
            ans[id] = query(r);
        }
    }

    for (auto i: ans) {
        cout << i << endl;
    }

}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }

}

詳細信息

Test #1:

score: 100
Accepted
time: 553ms
memory: 140944kb

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: