Submission #747115

# Submission time Handle Problem Language Result Execution time Memory
747115 2023-05-23T16:06:49 Z Olympia Radio (COCI22_radio) C++17
0 / 110
413 ms 18620 KB
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <map>
#include <set>
using namespace std;
struct SegmentTreeMin {
    vector<int> vec;
    const int id = (int)1e9;
    int merge (int x, int y) {
        return min(x, y);
    }
    void update (int x, int y) {
        x += (int)vec.size()/2 - 1;
        vec[x] = y;
        while (x != 0) {
            x = (x - 1)/2;
            vec[x] = merge(vec[2 * x + 1], vec[2 * x + 2]);
        }
    }
    int query (int dum, int tl, int tr, int l, int r) {
        if (tl >= l and tr <= r) {
            return vec[dum];
        }
        if (tl > r or tr < l) {
            return id;
        }
        return merge(query(2 * dum + 1, tl, (tl + tr)/2, l, r), query(2 * dum + 2, (tl + tr)/2 + 1, tr, l, r));
    }
    int query (int l, int r) {
        return query(0, 0, (int)vec.size()/2 - 1, l, r);
    }
    void resz (int n) {
        n = (1 << ((int)log2(n) + 1));
        vec.assign(2 * n, id);
    }
};
const int MAX = (int)1e5;
//int prev[MAX + 1];
set<int> oc[MAX + 1];
bool on[MAX + 1];
SegmentTreeMin st;
vector<int> factors[MAX + 1];
void update (int x) {
    if (!on[x]) {
        st.update(x, (int)1e9);
        return;
    }
    int nxt = (int)1e9;
    for (int i: factors[x]) {
        auto it = oc[i].upper_bound(x);
        if (it != oc[i].end()) {
            nxt = min(nxt, *it);
        }
    }
    assert(nxt != x);
    st.update(x, nxt);
}
int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N, Q;
    cin >> N >> Q;
    for (int i = 1; i <= N; i++) {
        on[i] = false;
    }
    for (int i = 2; i <= N; i++) {
        if (factors[i].empty()) {
            for (int j = i; j <= N; j += i) {
                factors[j].push_back(i);
            }
        }
    }
    st.resz(N + 1);
    int prev[N + 1];
    for (int i = 0; i <= N; i++) {
        prev[i] = -1;
    }
    while (Q--) {
        char c;
        cin >> c;
        if (c == 'S') {
            int x;
            cin >> x;
            vector<int> u = {x};
            if (on[x]) {
                for (int i: factors[x]) {
                    oc[i].erase(x);
                    auto it = oc[i].upper_bound(x);
                    if (it != oc[i].begin()) {
                        u.push_back(*(--it));
                    }
                }
            } else {
                for (int i: factors[x]) {
                    auto it = oc[i].upper_bound(x);
                    if (it != oc[i].begin()) {
                        u.push_back(*(--it));
                    }
                    oc[i].insert(x);
                }
            }
            for (int i: u) {
                update(i);
            }
            on[x] = not on[x];
        } else {
            int l, r;
            cin >> l >> r;
            cout << ((st.query(l, r) <= r) ? "DA" : "NE") << '\n';
        }
    }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:76:9: warning: variable 'prev' set but not used [-Wunused-but-set-variable]
   76 |     int prev[N + 1];
      |         ^~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 7252 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 413 ms 18620 KB Output is correct
2 Runtime error 18 ms 18084 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 7252 KB Output isn't correct
2 Halted 0 ms 0 KB -