Submission #558856

# Submission time Handle Problem Language Result Execution time Memory
558856 2022-05-08T19:30:30 Z Olympia Brunhilda’s Birthday (BOI13_brunhilda) C++17
0 / 100
183 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;
template<class T>
class SegmentTree {
public:

    SegmentTree (int N) {
        N = (1 << ((int)floor(log2(N - 1)) + 1));
        this->N = N;
        val.assign(2 * N, ID);
    }

    void update (int x, T y) {
        x += N - 1;
        val[x] = y;
        while (x != 0) {
            x = (x - 1)/2;
            val[x] = merge(val[2 * x + 1], val[2 * x + 2]);
        }
    }

    T query (int ind, const int l, const int r, int tl, int tr) {
        if (tl >= l && tr <= r) {
            return val[ind];
        }
        if (tr < l || tl > r) {
            return ID;
        }
        return merge(query(2 * ind + 1, l, r, tl, (tl + tr)/2), query(2 * ind + 2, l, r, (tl + tr)/2 + 1, tr));
    }

    T query (int l, int r) {
        return query(0, l, r, 0, N - 1);
    }
private:
    vector<T> val;
    T ID = 1e9;
    T merge (T x, T y) {
        return min(x, y);
    }
    int N;
};
 
int main() {
    int M, Q; cin >> M >> Q;
    vector<int> p(M);
    SegmentTree<int> st(M);
    map<int,int> primes;
    for (int i = 0; i < M; i++) {
        cin >> p[i];
        st.update(i, 0);
    }
    sort(p.begin(), p.end());
    for (int i = 0; i < M; i++) {
        primes[p[i]] = i;
        st.update(i, 0);
    } 
    int mx = (int)1e7 + 1;
    int res[mx];
    bool isPrime[mx];
    for (int i = 0; i < mx; i++) {
        isPrime[i] = true;
    }
    isPrime[0] = isPrime[1] = false;
    vector<int> fact[mx];
    for (int i = 2; i < mx; i++) {
        if (isPrime[i]) {
            for (int j = 2 * i; j < mx; j += i) {
                isPrime[j] = false;
                fact[j].push_back(i);
            }
            fact[i].push_back(i);
        }
    }
    for (int i = 1; i < mx; i++) {
        int ans = 1e9;
        set<int> invalid;
        for (int j: fact[i]) {
            if (primes.count(j)) {
                invalid.insert(primes[j]);
            }
        }
        invalid.insert(-1);
        invalid.insert(p.size());
        for (int j: invalid) {
            auto it = invalid.upper_bound(j);
            if (it == invalid.end()) continue;
            if (j + 1 <= *it - 1) {
                ans = min(ans, st.query(j + 1, *it - 1) + 1);
            }
        }
        invalid.erase(-1), invalid.erase(p.size());
        for (int j: invalid) {
            st.update(j, ans);
        }
        //cout << i << " < - > " << ans << '\n';
        res[i] = ans;
    }
    while (Q--) {
        int x;
        cin >> x;
        if (res[x] == (int)1e9) {
            cout << "oo\n";
        } else {
            cout << res[x] << '\n';
        }
    }
}
# Verdict Execution time Memory Grader output
1 Runtime error 120 ms 262144 KB Execution killed with signal 9
2 Runtime error 122 ms 262144 KB Execution killed with signal 9
3 Runtime error 129 ms 262144 KB Execution killed with signal 9
4 Runtime error 115 ms 262144 KB Execution killed with signal 9
5 Runtime error 106 ms 262144 KB Execution killed with signal 9
6 Runtime error 114 ms 262144 KB Execution killed with signal 9
7 Runtime error 114 ms 262144 KB Execution killed with signal 9
8 Runtime error 122 ms 262144 KB Execution killed with signal 9
9 Runtime error 103 ms 262144 KB Execution killed with signal 9
10 Runtime error 107 ms 262144 KB Execution killed with signal 9
11 Runtime error 109 ms 262144 KB Execution killed with signal 9
12 Runtime error 105 ms 262144 KB Execution killed with signal 9
13 Runtime error 110 ms 262144 KB Execution killed with signal 9
14 Runtime error 109 ms 262144 KB Execution killed with signal 9
15 Runtime error 111 ms 262144 KB Execution killed with signal 9
16 Runtime error 115 ms 262144 KB Execution killed with signal 9
17 Runtime error 118 ms 262144 KB Execution killed with signal 9
18 Runtime error 110 ms 262144 KB Execution killed with signal 9
# Verdict Execution time Memory Grader output
1 Runtime error 112 ms 262144 KB Execution killed with signal 9
2 Runtime error 183 ms 262144 KB Execution killed with signal 9
3 Runtime error 145 ms 262144 KB Execution killed with signal 9
4 Runtime error 111 ms 262144 KB Execution killed with signal 9
5 Runtime error 152 ms 262144 KB Execution killed with signal 9
6 Runtime error 107 ms 262144 KB Execution killed with signal 9
7 Runtime error 113 ms 262144 KB Execution killed with signal 9
8 Runtime error 111 ms 262144 KB Execution killed with signal 9
9 Runtime error 171 ms 262144 KB Execution killed with signal 9
10 Runtime error 137 ms 262144 KB Execution killed with signal 9
11 Runtime error 135 ms 262144 KB Execution killed with signal 9
12 Runtime error 110 ms 262144 KB Execution killed with signal 9
13 Runtime error 118 ms 262144 KB Execution killed with signal 9
14 Runtime error 104 ms 262144 KB Execution killed with signal 9
15 Runtime error 124 ms 262144 KB Execution killed with signal 9
16 Runtime error 168 ms 262144 KB Execution killed with signal 9
17 Runtime error 105 ms 262144 KB Execution killed with signal 9
18 Runtime error 159 ms 262144 KB Execution killed with signal 9
# Verdict Execution time Memory Grader output
1 Runtime error 137 ms 262144 KB Execution killed with signal 9
2 Runtime error 133 ms 262144 KB Execution killed with signal 9
3 Runtime error 125 ms 262144 KB Execution killed with signal 9
4 Runtime error 111 ms 262144 KB Execution killed with signal 9
5 Runtime error 160 ms 262144 KB Execution killed with signal 9
6 Runtime error 121 ms 262144 KB Execution killed with signal 9
7 Runtime error 173 ms 262144 KB Execution killed with signal 9
8 Runtime error 133 ms 262144 KB Execution killed with signal 9
9 Runtime error 133 ms 262144 KB Execution killed with signal 9
10 Runtime error 117 ms 262144 KB Execution killed with signal 9
11 Runtime error 110 ms 262144 KB Execution killed with signal 9
12 Runtime error 112 ms 262144 KB Execution killed with signal 9
13 Runtime error 118 ms 262144 KB Execution killed with signal 9
14 Runtime error 103 ms 262144 KB Execution killed with signal 9
15 Runtime error 110 ms 262144 KB Execution killed with signal 9
16 Runtime error 109 ms 262144 KB Execution killed with signal 9
17 Runtime error 127 ms 262144 KB Execution killed with signal 9
18 Runtime error 138 ms 262144 KB Execution killed with signal 9
19 Runtime error 108 ms 262144 KB Execution killed with signal 9
20 Runtime error 130 ms 262144 KB Execution killed with signal 9
21 Runtime error 104 ms 262144 KB Execution killed with signal 9
22 Runtime error 161 ms 262144 KB Execution killed with signal 9
23 Runtime error 126 ms 262144 KB Execution killed with signal 9
24 Runtime error 105 ms 262144 KB Execution killed with signal 9
25 Runtime error 106 ms 262144 KB Execution killed with signal 9
26 Runtime error 118 ms 262144 KB Execution killed with signal 9
27 Runtime error 161 ms 262144 KB Execution killed with signal 9
28 Runtime error 108 ms 262144 KB Execution killed with signal 9
29 Runtime error 166 ms 262144 KB Execution killed with signal 9
30 Runtime error 146 ms 262144 KB Execution killed with signal 9
31 Runtime error 106 ms 262144 KB Execution killed with signal 9
32 Runtime error 102 ms 262144 KB Execution killed with signal 9
33 Runtime error 106 ms 262144 KB Execution killed with signal 9
34 Runtime error 161 ms 262144 KB Execution killed with signal 9
35 Runtime error 106 ms 262144 KB Execution killed with signal 9
36 Runtime error 163 ms 262144 KB Execution killed with signal 9
37 Runtime error 160 ms 262144 KB Execution killed with signal 9
38 Runtime error 112 ms 262144 KB Execution killed with signal 9
39 Runtime error 107 ms 262144 KB Execution killed with signal 9
40 Runtime error 106 ms 262144 KB Execution killed with signal 9
41 Runtime error 164 ms 262144 KB Execution killed with signal 9
42 Runtime error 107 ms 262144 KB Execution killed with signal 9