QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#696476#9250. Max GCDllleiWA 1344ms982476kbC++202.7kb2024-10-31 22:48:232024-10-31 22:48:23

Judging History

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

  • [2024-10-31 22:48:23]
  • 评测
  • 测评结果:WA
  • 用时:1344ms
  • 内存:982476kb
  • [2024-10-31 22:48:23]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using LL = long long;



int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    constexpr int N = 1e6 + 1;
    vector<vector<int>> pos(N);
    vector<int> lp(N, 0);
    vector<array<int, 3>> a;

    auto handle = [&](int x) {
        if (pos[x].size() <= 2) {
            return;
        }
        bool flag = false;
        while (pos[x].back() - pos[x][lp[x] + 1] >= pos[x][lp[x] + 1] - pos[x][lp[x]]) {
            flag = true;
            lp[x]++;
        }
        if (lp[x] >= 1) {
            a.push_back({pos[x][lp[x] - 1], pos[x].back(), x});
        }
    };

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        for (int j = 1; j * j <= x; ++j) {
            if (x % j == 0) {
                pos[j].push_back(i);
                handle(j);
                if (j * j != x) {
                    pos[x / j].push_back(i);
                    handle(x / j);
                }
            }
        }
    }

    int S = sqrt(n);
    if (S <= 0) {
        S = 1;
    }
    int cnt = (n + S - 1) / S;
    vector<int> L(cnt), R(cnt), belong(n);
    vector<int> blk(cnt), c(n);
    for (int i= 0; i < n; ++i) {
        belong[i] = i / S;
    }
    for (int i = 0; i < cnt; ++i) {
        L[i] = i * S;
        R[i] = min((i + 1) * S - 1, n - 1);
    }

    vector<array<int, 3>> b;
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        b.push_back({l, r, i});
    }
    vector<int> ans(q);

    sort(b.begin(), b.end(), [&](const auto &a, const auto &b) {
        return a[1] < b[1];
    });

    int p = 0;

    auto add = [&](int x, int y) {
        c[x] = max(c[x], y);
        blk[belong[x]] = max(blk[belong[x]], y);
    };

    auto query = [&](int l, int r) {
        int ll = belong[l], rr = belong[r];

        if (ll == rr) {
            int ans = 0;
            for (int i = l; i <= r; ++i) {
                ans = max(ans, c[i]);
            }
            return ans;
        }

        int ans = 0;
        for (int i = l; i <= R[ll]; ++i) {
            ans = max(ans, c[i]);
        }
        for (int i = L[rr]; i <= r; ++i) {
            ans = max(ans, c[i]);
        }

        for (int i = ll + 1; i <= rr - 1; ++i) {
            ans = max(ans, blk[i]);
        }
        return ans;
    };

    for (int i = 0; i < q; ++i) {
        auto [l, r, id] = b[i];

        while (p < int(a.size()) && a[p][1] <= r) {
            add(a[p][0], a[p][2]);
            p++;
        }
        ans[id] = query(l, r);
    }
    for (int i = 0; i < q; ++i) {
        cout << ans[i] << '\n';
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 9ms
memory: 30452kb

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: 1344ms
memory: 982476kb

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'