답안 #544007

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544007 2022-03-31T19:42:33 Z Victor Radio (COCI22_radio) C++17
0 / 110
893 ms 117316 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 pos, int x, int y) {
        if (hi <= pos || pos < lo) return;
        if (hi - lo == 1) {
            stops[x] += y;
            if (!stops[x]) stops.erase(x);
            val = stops.begin()->first;
            return;
        }

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

vi prime_fac[1'000'005];
bitset<1'000'005> broadcast, used;
void sieve() {
    bitset<1'000'005> bs;
    rep(i, 2, 1'000'005) {
        if (bs[i]) continue;

        for (int j = i; j < 1'000'005; j += i) {
            if (used[j]) {
                prime_fac[j].pb(i);
                bs[j] = 1;
            }
        }
    }
}

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

void toggle(int factor, int freq) {
    int mul = broadcast[freq] ? -1 : 1;

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

    auto it = factors[factor].upper_bound(freq);

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

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

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

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

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

    scanf("%d %d",&n,&q);
    char types[200000];
    int val[200000][2];

    rep(i,0,q) {
        scanf("%c %d",&types[i],&val[i][0]);
        if(types[i]=='C') scanf(" %d",&val[i][1]);
        else used[val[i][0]]=1;
    }

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

    rep(i,0,q) {
        char type=types[i];

        if (type == 'S') {
            int freq=val[i][0];
            trav(prime, prime_fac[freq]) toggle(prime, freq);
            broadcast[freq] = !broadcast[freq];

        } else {
            int lo=val[i][0], hi=val[i][1];

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

            if (val <= hi)
                printf("DA\n");
            else
                printf("NE\n");
        }
        // cout << endl;
    }
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:129:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |     scanf("%d %d",&n,&q);
      |     ~~~~~^~~~~~~~~~~~~~~
Main.cpp:134:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |         scanf("%c %d",&types[i],&val[i][0]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:135:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |         if(types[i]=='C') scanf(" %d",&val[i][1]);
      |                           ~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 55 ms 72652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 893 ms 117316 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 55 ms 72652 KB Output isn't correct
2 Halted 0 ms 0 KB -