Submission #769970

# Submission time Handle Problem Language Result Execution time Memory
769970 2023-06-30T15:25:57 Z gun_gan Radio (COCI22_radio) C++17
110 / 110
593 ms 93872 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5;
int n, q, spf[N];
set<int> pos[N];
bool b[N];

int t[4 * N], nxt[N];

void update(int v, int l, int r, int idx, int val) {
      if(l > idx || r < idx) return;
      if(l == r) {
            t[v] = val;
            return;
      }
      int mid = (l + r) / 2;
      update(v * 2, l, mid, idx, val);
      update(v * 2 + 1, mid + 1, r, idx, val);
      t[v] = min(t[v * 2], t[v * 2 + 1]);
}

int query(int v, int l, int r, int ql, int qr) {
      if(qr < l || ql > r) return 1e9;
      if(ql <= l && qr >= r) return t[v];
      int mid = (l + r) / 2;
      return min(query(v * 2, l, mid, ql, qr), query(v * 2 + 1, mid + 1, r, ql, qr));
}

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

      for(int i = 2; i < N; i++) {
            if(!spf[i]) {
                  for(int j = i; j < N; j += i) if(!spf[j]) spf[j] = i;
            }
      }

      for(int i = 1; i < 4 * N; i++) t[i] = 1e9;
      for(int i = 1; i < N; i++) nxt[i] = 1e9;

      cin >> n >> q;
      while(q--) {
            char c;
            cin >> c;
            if(c == 'S') {
                  int x;
                  cin >> x;

                  int t = x;
                  if(b[x]) {
                        while(x > 1) {
                              int s = spf[x];
                              while(x % s == 0) x /= s;
                              auto it = pos[s].upper_bound(t);
                              auto it2 = pos[s].lower_bound(t);

                              if(it2 != pos[s].begin()) {
                                    it2--;

                                    int to = 1e9;
                                    if(it != pos[s].end()) to = *it;
                                    if(nxt[*it2] == t) {
                                          int z = *it2;
                                          nxt[*it2] = to;

                                          while(z > 1) {
                                                int s = spf[z];
                                                while(z % s == 0) z /= s;
                                                auto it = pos[s].upper_bound(t);
                                                if(it != pos[s].end()) nxt[*it2] = min(nxt[*it2], *it);
                                          }

                                          update(1, 1, n, *it2, nxt[*it2]);
                                    }
                              }

                              pos[s].erase(t);
                        }

                        nxt[t] = 1e9; 
                        update(1, 1, n, t, nxt[t]);
                  } else {
                        while(x > 1) {
                              int s = spf[x];
                              while(x % s == 0) x /= s;
                              auto it = pos[s].upper_bound(t);
                              if(it != pos[s].end()) nxt[t] = min(nxt[t], *it);

                              if(it != pos[s].begin()) {
                                    it--;
                                    nxt[*it] = min(nxt[*it], t);
                                    update(1, 1, n, *it, nxt[*it]);
                              }
                              
                              pos[s].insert(t);
                        } 

                        update(1, 1, n, t, nxt[t]);
                  }

                  b[t] ^= 1;
            } else {
                  int l, r;
                  cin >> l >> r;

                  if(query(1, 1, n, l, r) <= r) {
                        cout << "DA\n";
                  } else {
                        cout << "NE\n";
                  }
            }
      }

}
# Verdict Execution time Memory Grader output
1 Correct 34 ms 70740 KB Output is correct
2 Correct 34 ms 70724 KB Output is correct
3 Correct 34 ms 70732 KB Output is correct
4 Correct 35 ms 70676 KB Output is correct
5 Correct 35 ms 70664 KB Output is correct
6 Correct 35 ms 70664 KB Output is correct
7 Correct 35 ms 70760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 229 ms 77468 KB Output is correct
2 Correct 383 ms 87268 KB Output is correct
3 Correct 499 ms 91100 KB Output is correct
4 Correct 42 ms 70988 KB Output is correct
5 Correct 73 ms 71996 KB Output is correct
6 Correct 117 ms 73140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 70740 KB Output is correct
2 Correct 34 ms 70724 KB Output is correct
3 Correct 34 ms 70732 KB Output is correct
4 Correct 35 ms 70676 KB Output is correct
5 Correct 35 ms 70664 KB Output is correct
6 Correct 35 ms 70664 KB Output is correct
7 Correct 35 ms 70760 KB Output is correct
8 Correct 229 ms 77468 KB Output is correct
9 Correct 383 ms 87268 KB Output is correct
10 Correct 499 ms 91100 KB Output is correct
11 Correct 42 ms 70988 KB Output is correct
12 Correct 73 ms 71996 KB Output is correct
13 Correct 117 ms 73140 KB Output is correct
14 Correct 175 ms 72392 KB Output is correct
15 Correct 312 ms 75572 KB Output is correct
16 Correct 593 ms 93872 KB Output is correct
17 Correct 503 ms 90372 KB Output is correct
18 Correct 535 ms 92108 KB Output is correct
19 Correct 531 ms 92136 KB Output is correct
20 Correct 121 ms 73148 KB Output is correct
21 Correct 126 ms 73240 KB Output is correct
22 Correct 127 ms 73292 KB Output is correct
23 Correct 142 ms 73292 KB Output is correct