답안 #994184

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
994184 2024-06-07T08:27:04 Z LOLOLO Radio (COCI22_radio) C++17
110 / 110
736 ms 158800 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (ll)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 1e6 + 100;
int seg[N * 4], lst[N], is[N];
multiset <int> nxt[N];
set <int> st[N];
int n;

void build() {
    for (int i = 0; i < N; i++) {
        lst[i] = i;
    }

    for (int i = 2; i < N; i++) {
        if (lst[i] == i) {
            for (int j = i; j < N; j += i)
                lst[j] = min(lst[j], i);
        }
    }
}

void upd(int id, int l, int r, int p, int v) {
    if (l == r) {
        seg[id] = v;
        return;
    }

    int m = (l + r) / 2;
    if (m >= p) {
        upd(id * 2, l, m, p, v);
    } else {
        upd(id * 2 + 1, m + 1, r, p, v);
    }

    seg[id] = min(seg[id * 2], seg[id * 2 + 1]);
}

void upd(int p) {
    if (nxt[p].empty()) {
        upd(1, 1, n, p, 1e9);
    } else {
        upd(1, 1, n, p, *nxt[p].begin());
    }
}

int get(int id, int l, int r, int u, int v) {
    if (l > v || r < u)
        return 1e9;

    if (l >= u && r <= v)
        return seg[id];

    int m = (l + r) / 2;
    return min(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}

void add(int x, int p) {
    while (x > 1) {
        int d = lst[x];
        while (lst[x] == d) {
            x /= d;
        }

        auto it = st[d].lower_bound(p);
        int ss = 0;
        if (it != st[d].end()) {
            nxt[p].insert(*it);
            ss = *it;
            upd(p);
        }

        if (it != st[d].begin()) {
            it = prev(it);
            nxt[*it].insert(p);
            if (ss) {
                nxt[*it].erase(nxt[*it].find(ss));
            }
            upd(*it);
        } 
        st[d].insert(p);
    }
}

void er(int x, int p) {
    while (x > 1) {
        int d = lst[x];
        while (lst[x] == d) {
            x /= d;
        }

        auto it = st[d].lower_bound(p);
        int ss = 0;
        if (next(it) != st[d].end()) {
            nxt[p].erase(nxt[p].find(*next(it)));
            ss = *next(it);
            upd(p);
        }

        if (it != st[d].begin()) {
            it = prev(it);
            nxt[*it].erase(nxt[*it].find(p));
            if (ss) {
                nxt[*it].insert(ss);
            }
            upd(*it);
        }
        st[d].erase(p);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    build();
    mem(seg, 0x3f);
    int q;
    cin >> n >> q;

    for (int i = 1; i <= q; i++) {
        char c;
        cin >> c;
        if (c == 'S') {
            int x;
            cin >> x;
            if (is[x]) {
                er(x, x);
            } else {
                add(x, x);
            }

            is[x] ^= 1;
        } else {
            int l, r;
            cin >> l >> r;
            int t = get(1, 1, n, l, r);
            if (t <= r) {
                cout << "DA\n";
            } else {
                cout << "NE\n";
            }
        }
    }

    return 0;
}
   
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 115800 KB Output is correct
2 Correct 22 ms 115804 KB Output is correct
3 Correct 22 ms 115804 KB Output is correct
4 Correct 22 ms 115800 KB Output is correct
5 Correct 22 ms 115844 KB Output is correct
6 Correct 22 ms 115804 KB Output is correct
7 Correct 22 ms 115724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 295 ms 127552 KB Output is correct
2 Correct 455 ms 146768 KB Output is correct
3 Correct 568 ms 153892 KB Output is correct
4 Correct 25 ms 115800 KB Output is correct
5 Correct 34 ms 117880 KB Output is correct
6 Correct 49 ms 119124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 115800 KB Output is correct
2 Correct 22 ms 115804 KB Output is correct
3 Correct 22 ms 115804 KB Output is correct
4 Correct 22 ms 115800 KB Output is correct
5 Correct 22 ms 115844 KB Output is correct
6 Correct 22 ms 115804 KB Output is correct
7 Correct 22 ms 115724 KB Output is correct
8 Correct 295 ms 127552 KB Output is correct
9 Correct 455 ms 146768 KB Output is correct
10 Correct 568 ms 153892 KB Output is correct
11 Correct 25 ms 115800 KB Output is correct
12 Correct 34 ms 117880 KB Output is correct
13 Correct 49 ms 119124 KB Output is correct
14 Correct 186 ms 117316 KB Output is correct
15 Correct 350 ms 123472 KB Output is correct
16 Correct 736 ms 158800 KB Output is correct
17 Correct 530 ms 151632 KB Output is correct
18 Correct 622 ms 155472 KB Output is correct
19 Correct 608 ms 155208 KB Output is correct
20 Correct 48 ms 119124 KB Output is correct
21 Correct 45 ms 119376 KB Output is correct
22 Correct 45 ms 119376 KB Output is correct
23 Correct 48 ms 119376 KB Output is correct