QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#186487#6405. Barkleyucup-team228WA 2ms8784kbC++202.9kb2023-09-23 22:45:492023-09-23 22:45:49

Judging History

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

  • [2023-09-23 22:45:49]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:8784kb
  • [2023-09-23 22:45:49]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct sparse_table{
    static const int N = 1e5 + 10, L = 20;
    ll a[N];
    ll d[N][L], rem[N];
    void build(int n) {
        for (int i = 1; i <= n; i++) { d[i][0] = a[i]; }
        for (int j = 1; j <= L - 1; j++) {
            for (int i = 1; i <= n; i++) {
                if (i + (1 << j) - 1 > n) { continue; }
                d[i][j] = __gcd(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]);
            }
        }
        for (int i = 1; i <= n; i++) { rem[i] = __lg(i); }
    }
    ll get(int l, int r) {
        int lg = rem[r - l + 1];
        return __gcd(d[l][lg], d[r - (1 << lg) + 1][lg]);
    }
};

sparse_table table;

const int N = 1e5 + 10;
ll a[N];
vector<pair<int, ll>> rig[N];

void precalc(int n) {
    for (int i = 1; i <= n; i++) {
        table.a[i] = a[i];
    }
    table.build(n);

    rig[n] = {{n, a[n]}};

    for (int l = n - 1; l >= 1; l--) {
        rig[l] = {{l, a[l]}};
        for (auto [r, val] : rig[l + 1]) {
            val = __gcd(val, a[l]);
            if (val == rig[l].back().second) {
                rig[l].back().first = r;
            } else {
                rig[l].emplace_back(r, val);
            }
        }
    }
}

vector<ll> get(int l, int r, int k) {
    assert(r - l + 1 >= k);
    if (k == r - l + 1) {
        return {0};
    } else if (k == 0) {
        return {table.get(l, r)};
    } else {
        vector<int> cur;
        for (auto [x, val] : rig[l]) {
            for (int i = max(l - 1, x - 6); i <= x + 2; i++) {
                if (i + 1 <= r - k + 1) {
                    cur.push_back(i + 1);
                }
            }
        }
        sort(cur.begin(), cur.end());
        cur.erase(unique(cur.begin(), cur.end()), cur.end());
        vector<ll> res;
        for (int i = 0, j = 0; i < cur.size(); i++) {
            ll val = 0;
            if (cur[i] >= l + 1) {
                while (rig[l][j].first < cur[i] - 1) {
                    j++;
                }
                val = rig[l][j].second;
            }
            auto nxt = get(cur[i] + 1, r, k - 1);
            for (ll s : nxt) {
                res.push_back(__gcd(val, s));
            }
        }
        return res;
    }
}

ll solve(int l, int r, int k) {
    auto res = get(l, r, k);
    ll ans = 0;
    for (ll x : res) {
        ans = max(ans, x);
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    precalc(n);

    while (q--) {
        int l, r, k;
        cin >> l >> r >> k;
        cout << solve(l, r, k) << "\n";
    }

#ifdef LOCAL
    cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 8088kb

input:

4 4
3 2 6 4
1 3 1
2 4 1
1 4 2
1 4 3

output:

3
2
3
6

result:

ok 4 number(s): "3 2 3 6"

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 8784kb

input:

100 10000
7 25 33 17 13 33 24 29 11 1 3 19 2 20 33 23 14 24 15 12 3 1 5 13 6 35 15 21 10 34 31 19 7 33 17 26 26 1 19 21 31 5 29 20 18 32 19 18 19 31 11 26 6 19 2 26 23 1 4 2 31 21 29 30 1 14 20 23 14 32 4 34 13 29 5 26 24 29 28 5 26 26 21 19 2 33 2 31 30 3 23 24 26 32 36 21 21 11 5 9
56 57 1
90 97 1...

output:

26
1
1
1
1
1
1
1
31
1
1
1
1
1
26
1
1
1
1
1
1
29
1
1
1
1
1
1
4
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
2
2
1
1
1
21
1
1
1
0
1
19
1
1
1
21
1
1
1
1
1
1
1
1
1
1
1
1
1
3
0
1
1
1
1
1
1
1
1
1
4
1
1
1
1
1
3
1
2
1
26
1
1
1
1
1
1
1
7
1
1
1
33
1
1
1
1
1
1
2
1
26
1
1
1
2
1
1
1
1
1
1
26
1
1
1
1
31
1
1
2
1
4
29
1
2
1
1...

result:

wrong answer 44th numbers differ - expected: '1', found: '0'