#include<bits/stdc++.h>
using namespace std;
const bool multiTest = false;
#define task "C"
#define fi first
#define se second
#define MASK(i) (1LL << (i))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define BIT(mask, i) ((mask) >> (i) & 1)
template<typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) a = b; else return 0; return 1;
}
template<typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) a = b; else return 0; return 1;
}
const int MAX = 1000100;
const int oo = 1e9 + 7;
int n, q;
bool flag[MAX];
int prime[MAX];
set<int> mSet[MAX];
struct segmentTree {
vector<int> st;
int n;
segmentTree(int _n = 0) {
n = _n;
st.assign(4 * n + 5, oo);
}
void update(int pos, int value) {
int id = 1, l = 1, r = n;
while (l != r) {
int mid = l + r >> 1;
if (pos <= mid) id <<= 1, r = mid;
else id = id << 1 | 1, l = mid + 1;
}
st[id] = value;
while (id > 1) {
id >>= 1;
st[id] = min(st[id << 1], st[id << 1 | 1]);
}
}
void updateMin(int pos, int value) {
int id = 1, l = 1, r = n;
while (l != r) {
int mid = l + r >> 1;
if (pos <= mid) id <<= 1, r = mid;
else id = id << 1 | 1, l = mid + 1;
}
minimize(st[id], value);
while (id > 1) {
id >>= 1;
st[id] = min(st[id << 1], st[id << 1 | 1]);
}
}
int getMin(int id, int l, int r, int u, int v) {
if (l > v || r < u) return oo;
if (l >= u && r <= v) return st[id]; int mid = l + r >> 1;
return min(getMin(id << 1, l, mid, u, v), getMin(id << 1 | 1, mid + 1, r, u, v));
}
int getMin(int l, int r) {
return getMin(1, 1, n, l, r);
}
};
void process(void) {
cin >> n >> q;
for (int i = 2; i <= 1000000; ++i) if (prime[i] == 0) {
for (int j = i; j <= 1000000; j += i) prime[j] = i;
}
segmentTree mTree(n);
while (q--) {
char type; cin >> type;
if (type == 'S') {
int x; cin >> x;
if (!flag[x]) {
int minRg = oo, curValue = x;
while (curValue > 1) {
int p = prime[curValue];
curValue /= p;
if (mSet[p].size()) {
auto it = mSet[p].lower_bound(x);
if (it != mSet[p].end()) {
minRg = min(minRg, *it);
}
if (it != mSet[p].begin()) {
--it;
mTree.updateMin(*it, x);
}
}
}
if (minRg != oo) {
mTree.update(x, minRg);
}
curValue = x;
while (curValue > 1) {
int p = prime[curValue];
mSet[p].insert(x);
curValue /= p;
}
} else {
set<int> candidates;
int curValue = x;
while (curValue > 1) {
int p = prime[curValue];
curValue /= p;
if (mSet[p].size()) {
auto it = mSet[p].lower_bound(x);
if (it != mSet[p].begin()) {
--it;
candidates.insert(*it);
}
}
}
mTree.update(x, oo);
curValue = x;
while (curValue > 1) {
int p = prime[curValue];
curValue /= p;
auto it = mSet[p].lower_bound(x);
if (it != mSet[p].end()) mSet[p].erase(x);
}
vector<int> values(candidates.size(), oo);
int cur = 0;
for (int x: candidates) {
mTree.update(x, oo);
curValue = x;
while (curValue > 1) {
int p = prime[curValue];
curValue /= p;
auto it = mSet[p].upper_bound(x);
if (it != mSet[p].end()) {
minimize(values[cur], *it);
}
}
cur++;
}
cur = 0;
for (int x: candidates) {
mTree.update(x, values[cur]);
}
}
flag[x] = !flag[x];
} else {
int lf, rg; cin >> lf >> rg;
int pos = mTree.getMin(lf, rg);
cout << (lf <= pos && pos <= rg ? "DA\n" : "NE\n");
}
}
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int nTest = 1; if (multiTest) cin >> nTest;
while (nTest--) {
process();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:174:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
174 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:175:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
175 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |