#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;
if (on[x]) {
for (int i: factors[x]) {
oc[i].erase(x);
}
} else {
for (int i: factors[x]) {
oc[i].insert(x);
}
}
for (int j = 1; j <= N; j++) {
update(j);
}
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];
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1594 ms |
12164 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Execution timed out |
1594 ms |
12164 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |