QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#178350#6405. Barkleyucup-team1600#WA 3ms3644kbC++204.8kb2023-09-13 21:31:072023-09-13 21:31:08

Judging History

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

  • [2023-09-13 21:31:08]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3644kb
  • [2023-09-13 21:31:07]
  • 提交

answer

//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#ifdef LOCAL
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <random>
#include <deque>
#include <functional>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <complex>
#include <numeric>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <thread>
#else
#include <bits/stdc++.h>
#endif

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

template<typename T>
inline bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<typename T>
inline bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef LOCAL
#define D for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define D while (false)
#define LOG(...)
#endif // LOCAL

const int max_n = -1, inf = 1000111222;


struct node {
public:
    ll val;
    node (ll x = 0) : val(x) {}
};

inline node comb (node a, node b) {
    return node(gcd(a.val, b.val));
}

struct RMQ {
    /// init O(n*logn) time and memory
    /// query O(1)
public:
    int n, k;
    vector <vector <node> > up;
    vector <int> f;
    vector <ll> a;

    RMQ() {}

    RMQ (int n, vector <ll> a) : n(n), a(a) {
        k = 0;
        while ((1 << k) <= n) {
            ++k;
        }
        init();
    }

    inline void init () {
        up.assign(k, vector <node> (n));
        for (int st = 0; st < k; st++) {
            int len = 1 << st;
            for (int c = len; c < n + len; c += len * 2) {
                if (c < n) {
                    up[st][c] = node(a[c]);
                }
                if (c - 1 < n) {
                    up[st][c - 1] = node(a[c - 1]);
                }
                for (int i = c + 1; i < c + len; i++) {
                    if (i >= n) break;
                    up[st][i] = comb(up[st][i - 1], node(a[i]));
                }
                for (int i = c - 2; i >= c - len; i--) {
                    if (i >= n) continue;
                    if (i + 1 < n) {
                        up[st][i] = comb(node(a[i]), up[st][i + 1]);
                    }
                    else {
                        up[st][i] = node(a[i]);
                    }
                }
            }
        }
        f.resize(n + n);
        for (int i = 2; i < n + n; i++) {
            f[i] = f[i / 2] + 1;
        }
    }

    node query (int l, int r) {
        if (l == r) return node(a[r]);
        int t = f[l ^ r];
        return comb(up[t][l], up[t][r]);
    };

};


int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q;
    cin >> n >> q;
    vector <ll> a(n);
    for (auto &i : a) {
        cin >> i;
    }
    RMQ t(n, a);
    auto fnd_nxt = [&] (ll g, int st, int r) {
        if (st > r) {
            return n;
        }
        if (g == 0) {
            return st;
        }
        int L = st, R = r;
        while (L < R) {
            int mid = (L + R) >> 1;
            if (t.query(st, mid).val >= g) {
                L = mid + 1;
            }
            else {
                R = mid;
            }
        }
        if (t.query(st, R).val >= g) {
            return n;
        }
        return R;
    };
    function <ll(int, int, int)> solve = [&] (int l, int r, int k) -> ll {
//        LOG(l, r, k);
        if (l > r) {
            return 0;
        }
        if (k == 0) {
            return t.query(l, r).val;
        }
        ll cur = 0;
        int pos = fnd_nxt(cur, l, r);
        ll ans = 0;
        while (pos <= r) {
            LOG(cur, pos, l, r, k, a[pos], t.query(l, pos).val);
            umax(ans, gcd(cur, solve(pos + 1, r, k - 1)));
            cur = gcd(cur, a[pos]);
            pos = fnd_nxt(cur, l, r);
        }
        umax(ans, cur);
        return ans;
    };
    for (int i = 0, l, r, k; i < q; i++) {
        cin >> l >> r >> k;
        --l, --r;
        cout << solve(l, r, k) << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3ms
memory: 3600kb

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
1
1
1
2
2
1
1
1
21
1
1
1
1
1
19
1
1
1
21
1
1
1
1
1
1
1
1
1
1
1
1
1
3
1
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 945th numbers differ - expected: '2', found: '1'