QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#551781#9250. Max GCDucup-team4435#WA 1574ms230272kbC++202.6kb2024-09-07 18:03:372024-09-07 18:03:37

Judging History

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

  • [2024-09-07 18:03:37]
  • 评测
  • 测评结果:WA
  • 用时:1574ms
  • 内存:230272kb
  • [2024-09-07 18:03:37]
  • 提交

answer

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

using ll = long long;
using ld = long double;

#define all(a) begin(a), end(a)
#define len(a) int((a).size())

#ifdef LOCAL
    #include "debug.h"
#else
    #define dbg(...)
    #define dprint(...)
    #define debug if constexpr (false)
    #define draw_tree(...)
    #define draw_array(...)
#endif // LOCAL

constexpr int LG = 25;

struct info {
    array<int, LG> a;
    int st = 0, length = 0;

    void push(int x) {
        if (length + 2 == LG) {
            length--;
        }
        length++;
        st = (st + LG - 1) % LG;
        a[st] = x;
    }

    int solve(int l) {
        if (length <= 1) {
            return 1e9;
        }
        int nxt = a[st];
        int lim = 2 * nxt - l;

        int lb = 0, rb = length;
        while (rb - lb > 1) {
            int mid = (lb + rb) / 2;
            (a[(st + mid) % LG] >= lim ? rb : lb) = mid;
        }
        if (rb != length) {
            return a[(st + rb) % LG];
        }
        return 1e9;
    }
};

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (auto &x : a) {
        cin >> x;
    }

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

    const int N = *max_element(all(a)) + 3;
    vector<vector<int>> divs(N);
    for (int x = 1; x < N; x++) {
        for (int y = x; y < N; y += x) {
            divs[y].push_back(x);
        }
    }

    const int B = sqrt(n);
    const int nblocks = (n + B - 1) / B;
    vector<int> best(n), block_best(nblocks);

    auto update = [&](int r, int value) {
        best[r] = max(best[r], value);
        int block = r / B;
        block_best[block] = max(block_best[block], value);
    };

    vector<info> data(N);
    for (int l = n - 1; l >= 0; l--) {
        for (auto d : divs[a[l]]) {
            int min_r = data[d].solve(l);
            if (min_r < n) {
                update(min_r, d);
            }
            data[d].push(l);
        }
        for (auto [r, i] : queries[l]) {
            while (r >= 0 && r % B != B - 1) {
                ans[i] = max(ans[i], best[r]);
                r--;
            }
            for (int j = 0; j <= r / B; j++) {
                ans[i] = max(ans[i], block_best[j]);
            }
        }
    }

    for (auto x : ans) {
        cout << x << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1574ms
memory: 229804kb

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: 750ms
memory: 230272kb

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 83865th words differ - expected: '222', found: '907'