Submission #543993

# Submission time Handle Problem Language Result Execution time Memory
543993 2022-03-31T18:59:20 Z Victor Radio (COCI22_radio) C++17
0 / 110
1500 ms 199692 KB
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)

#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl

#define umap unordered_map
#define uset unordered_set

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const int INF = 1'000'000'007;

struct Node {
    Node *l, *r;
    int val, lo, hi;
    map<int, int> stops;

    Node(int L, int R) : lo(L), hi(R) {
        if (hi - lo == 1) {
            ++stops[INF];
            val = INF;

        } else {
            int mid = (lo + hi) / 2;
            l = new Node(lo, mid);
            r = new Node(mid, hi);
            val = min(l->val, r->val);
        }
    }

    int query(int L, int R) {
        if (hi <= L || R <= lo) return INF;
        if (L <= lo && hi <= R) return val;

        return min(l->query(L, R), r->query(L, R));
    }

    void update(int L, int R, int x, int y) {
        if (hi <= L || R <= lo) return;
        if (hi - lo == 1) {
            stops[x] += y;
            if (!stops[x]) stops.erase(x);
            val = stops.begin()->first;
            return;
        }

        l->update(L, R, x, y), r->update(L, R, x, y);
        val = min(l->val, r->val);
    }
};

vi primes;
void sieve() {
    bitset<1005> bs;
    rep(i, 2, 1005) {
        if (bs[i]) continue;
        ;
        primes.pb(i);
        for (int j = i * i; j < 1005; j += i) bs[i] = 1;
    }
}

set<int> factors[1'000'005];
bitset<1'000'005> broadcast;
Node *segtree;
int n, q;

void toggle(int factor, int freq) {
    int mul = broadcast[factor] ? -1 : 1;
    set<int> &cur = factors[factor];

    if (broadcast[factor])
        cur.erase(freq);
    else
        cur.insert(freq);

    auto it = cur.upper_bound(freq);

    //cout << "factor = " << factor << " freq = " << freq << " broadcast = " << broadcast[factor] << endl;

    int hi = -1, lo = -1;
    if (it != cur.end()) {
        hi = *it;
        segtree->update(freq, freq + 1, hi, 1 * mul);
        // cout<<"WA1"<<endl;
    }

    if (!broadcast[factor]) --it;
    if (it != cur.begin()) {
        --it;
        lo = *it;
        segtree->update(lo, lo + 1, freq, 1 * mul);
        // cout<<"WA2"<<endl;
    }

    if (lo != -1 && hi != -1) {
        segtree->update(lo, lo + 1, hi, -1 * mul);
        //cout << "WA3" << endl;
    }

    broadcast[factor] = !broadcast[factor];
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    sieve();
    cin >> n >> q;
    segtree = new Node(0, n + 5);

    while (q--) {
        char type;
        cin >> type;

        if (type == 'S') {
            int freq;
            cin >> freq;

            int val = freq;
            trav(prime, primes) {
                if (prime * prime > val) break;

                if (val % prime == 0) toggle(prime, freq);
                while (val % prime == 0) val /= prime;
            }
            if (val != 1) toggle(val, freq);

        } else {
            int lo, hi;
            cin >> lo >> hi;

            int val = segtree->query(lo, hi + 1);
            //cout << "lo = " << lo << " hi = " << hi << " val = " << val << endl;

            if (val <= hi)
                cout << "DA" << endl;
            else
                cout << "NE" << endl;
        }
        //cout << endl;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 47188 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 942 ms 90532 KB Output is correct
2 Execution timed out 1608 ms 199692 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 47188 KB Output isn't correct
2 Halted 0 ms 0 KB -