Submission #558864

# Submission time Handle Problem Language Result Execution time Memory
558864 2022-05-08T19:40:06 Z Olympia Brunhilda’s Birthday (BOI13_brunhilda) C++17
20 / 100
952 ms 134832 KB
#include <bits/stdc++.h>
using namespace std;
template<class T> struct Seg { // comb(ID,b) = b
	const T ID = 1e9; T comb(T a, T b) { return min(a,b); }
	int n; vector<T> seg;
	void init(int _n) { n = _n; seg.assign(2*n,ID); }
	void pull(int p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
	void upd(int p, T val) { // set val at position p
		seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
	T query(int l, int r) {	// min on interval [l, r]
		T ra = ID, rb = ID;
		for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
			if (l&1) ra = comb(ra,seg[l++]);
			if (r&1) rb = comb(seg[--r],rb);
		}
		return comb(ra,rb);
	}
};
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int M, Q; cin >> M >> Q;
    vector<int> p(M);
    Seg<int> st;
    st.init(M + 1);
    map<int,int> primes;
    for (int i = 0; i < M; i++) {
        cin >> p[i];
        st.upd(i, 0);
    }
    sort(p.begin(), p.end());
    for (int i = 0; i < M; i++) {
        primes[p[i]] = i;
        st.upd(i, 0);
    } 
    int mx = 1000000;
    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;
                if (primes.count(i)) fact[j].push_back(primes[i]);
            }
            if (primes.count(i)) fact[i].push_back(primes[i]);
        }
    }
    for (int i = 1; i < mx; i++) {
        int ans = 1e9;
        vector<int> invalid;
        invalid.push_back(-1);
        for (int j: fact[i]) {
            invalid.push_back(j);
        }
        invalid.push_back(p.size());
        for (int j = 0; j < (int)invalid.size() - 1; j++) {
            if (invalid[j] + 1 <= invalid[j + 1] - 1) {
                ans = min(ans, st.query(invalid[j] + 1, invalid[j + 1] - 1) + 1);
            }
        }
        for (int j: invalid) {
            if (j >= 0 && j < (int)p.size()) {
                st.upd(j, ans);
            }
        }
        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 Correct 101 ms 33864 KB Output is correct
2 Correct 226 ms 51328 KB Output is correct
3 Correct 151 ms 48172 KB Output is correct
4 Correct 119 ms 31024 KB Output is correct
5 Correct 126 ms 36820 KB Output is correct
6 Correct 102 ms 33956 KB Output is correct
7 Correct 153 ms 48204 KB Output is correct
8 Correct 181 ms 52100 KB Output is correct
9 Correct 239 ms 54404 KB Output is correct
10 Correct 280 ms 55220 KB Output is correct
11 Correct 237 ms 50632 KB Output is correct
12 Correct 108 ms 30384 KB Output is correct
13 Correct 555 ms 58212 KB Output is correct
14 Correct 575 ms 58204 KB Output is correct
15 Correct 221 ms 50672 KB Output is correct
16 Correct 214 ms 51436 KB Output is correct
17 Correct 167 ms 36800 KB Output is correct
18 Correct 113 ms 30996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 207 ms 69988 KB Execution killed with signal 11
2 Runtime error 288 ms 81812 KB Execution killed with signal 11
3 Runtime error 860 ms 130324 KB Execution killed with signal 11
4 Runtime error 265 ms 84792 KB Execution killed with signal 11
5 Runtime error 532 ms 116920 KB Execution killed with signal 11
6 Runtime error 248 ms 96200 KB Execution killed with signal 11
7 Runtime error 205 ms 69948 KB Execution killed with signal 11
8 Runtime error 240 ms 81604 KB Execution killed with signal 11
9 Runtime error 665 ms 123988 KB Execution killed with signal 11
10 Runtime error 869 ms 130412 KB Execution killed with signal 11
11 Runtime error 836 ms 124556 KB Execution killed with signal 11
12 Runtime error 393 ms 106936 KB Execution killed with signal 11
13 Runtime error 203 ms 73912 KB Execution killed with signal 11
14 Runtime error 275 ms 84904 KB Execution killed with signal 11
15 Runtime error 687 ms 122900 KB Execution killed with signal 11
16 Runtime error 278 ms 81784 KB Execution killed with signal 11
17 Runtime error 632 ms 117160 KB Execution killed with signal 11
18 Runtime error 717 ms 127720 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Runtime error 711 ms 125052 KB Execution killed with signal 11
2 Runtime error 857 ms 126988 KB Execution killed with signal 11
3 Runtime error 849 ms 126760 KB Execution killed with signal 11
4 Runtime error 421 ms 108932 KB Execution killed with signal 11
5 Runtime error 303 ms 82356 KB Execution killed with signal 11
6 Runtime error 597 ms 115700 KB Execution killed with signal 11
7 Runtime error 542 ms 115956 KB Execution killed with signal 11
8 Runtime error 717 ms 125084 KB Execution killed with signal 11
9 Runtime error 718 ms 125056 KB Execution killed with signal 11
10 Runtime error 452 ms 104884 KB Execution killed with signal 11
11 Runtime error 371 ms 97736 KB Execution killed with signal 11
12 Runtime error 573 ms 114980 KB Execution killed with signal 11
13 Runtime error 772 ms 121864 KB Execution killed with signal 11
14 Runtime error 330 ms 113152 KB Execution killed with signal 11
15 Runtime error 623 ms 117800 KB Execution killed with signal 11
16 Runtime error 700 ms 118684 KB Execution killed with signal 11
17 Runtime error 594 ms 119164 KB Execution killed with signal 11
18 Runtime error 850 ms 127004 KB Execution killed with signal 11
19 Runtime error 235 ms 85160 KB Execution killed with signal 11
20 Runtime error 848 ms 126728 KB Execution killed with signal 11
21 Runtime error 415 ms 115948 KB Execution killed with signal 11
22 Runtime error 903 ms 133296 KB Execution killed with signal 11
23 Runtime error 277 ms 78692 KB Execution killed with signal 11
24 Runtime error 178 ms 69820 KB Execution killed with signal 11
25 Runtime error 456 ms 107272 KB Execution killed with signal 11
26 Runtime error 422 ms 108896 KB Execution killed with signal 11
27 Runtime error 952 ms 134832 KB Execution killed with signal 11
28 Runtime error 211 ms 80492 KB Execution killed with signal 11
29 Runtime error 701 ms 129636 KB Execution killed with signal 11
30 Runtime error 590 ms 124236 KB Execution killed with signal 11
31 Runtime error 253 ms 83060 KB Execution killed with signal 11
32 Runtime error 282 ms 89324 KB Execution killed with signal 11
33 Runtime error 166 ms 64860 KB Execution killed with signal 11
34 Runtime error 530 ms 115880 KB Execution killed with signal 11
35 Runtime error 236 ms 82048 KB Execution killed with signal 11
36 Runtime error 845 ms 131720 KB Execution killed with signal 11
37 Runtime error 301 ms 82388 KB Execution killed with signal 11
38 Runtime error 604 ms 115688 KB Execution killed with signal 11
39 Runtime error 202 ms 73460 KB Execution killed with signal 11
40 Runtime error 525 ms 113228 KB Execution killed with signal 11
41 Runtime error 525 ms 120524 KB Execution killed with signal 11
42 Runtime error 661 ms 118256 KB Execution killed with signal 11