YC356C [ 20241022 CQYC NOIP 模拟赛 T3 ] 互质询问(gcd)
题意
维护一个集合:
- 加入或删除一个元素 \(x\)。
- 询问在值域为 \([l, r]\) 中时候存在两个不互质的数。
\(q \le 10 ^ 6\),保证修改操作不超过 \(3 \times 10 ^ 5\) 次。
Sol
首先,\(\gcd\) 只用考虑质因子即可。
先对每次修改的数质因数分解,拆成前 \(200\) 个质数 + 一个大质数。
对于前者,直接开一个线段树,因为只有 \(200\) 位,所有可以直接用 bitset 暴力与一下,当然也可以压位高精。
对于后者,考虑开 \(10 ^ 6\) 个 \(set\) 暴力维护该质数的位置,注意到一个区间 \([l, r]\) 合法仅当 \([l, r]\) 里面所有数的前驱 \(pre_i < l\)。
于是再开一个线段树维护所有数的前驱,与 \(set\) 一起暴力修改即可。
做完了。
复杂度:\(O(n \log n \dfrac{\sqrt n}{w\ln n})\)。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <set>
#include <tuple>
#include <bitset>
#define ll unsigned long long
#define tupl tuple <ll, ll, ll>
using namespace std;
#ifdef ONLINE_JUDGE
/* #define getchar() (p1 == p2 && (p1 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++) */
/* char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf; */
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(int x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
bool _stmer;
const int N = 1e6 + 5, M = 1e5 + 5, K = 172;
array <int, N> p, idx;
bitset <N> vis;
int cnt;
void Euler(int n) {
for (int i = 2; i <= n; i++) {
if (!vis[i]) cnt++, p[cnt] = i, idx[i] = cnt;
for (int j = 1; i * p[j] <= n && j <= cnt; j++) {
vis[i * p[j]] = 1;
if (i % p[j] == 0) break;
}
}
return;
}
namespace Sgt1 {
ll lowbit(ll x) { return x & -x; }
array <tupl, N * 4> edge;
tupl update(tupl x, tupl y) {
tupl ans = make_tuple(get <0>(x) | get <0>(y),
get <1>(x) | get <1>(y),
get <2>(x) | get <2>(y));
if (!(get <0>(ans) & 1))
get <0>(ans) |= (!!(lowbit(get <0>(x) & get <0>(y)))
|| !!(lowbit(get <1>(x) & get <1>(y)))
|| !!(lowbit(get <2>(x) & get <2>(y))));
return ans;
}
void pushup(int x) { edge[x] = update(edge[x * 2], edge[x * 2 + 1]); }
void modify(int x, int l, int r, int k, tupl y) {
if (l == r) {
edge[x] = y;
return;
}
int mid = (l + r) >> 1;
if (k <= mid) modify(x * 2, l, mid, k, y);
else modify(x * 2 + 1, mid + 1, r, k, y);
pushup(x);
}
tupl query(int x, int l, int r, int L, int R) {
if (L > r || R < l) return make_tuple(0, 0, 0);
if (L <= l && R >= r) return edge[x];
int mid = (l + r) >> 1; tupl ans(0, 0, 0);
if (L <= mid) ans = update(ans, query(x * 2, l, mid, L, R));
if (R > mid) ans = update(ans, query(x * 2 + 1, mid + 1, r, L, R));
return ans;
}
} //namespace Sgt1
namespace Sgt2 {
array <int, N * 4> edge;
void modify(int x, int l, int r, int k, int y) {
if (l == r) {
edge[x] = y;
return;
}
int mid = (l + r) >> 1;
if (k <= mid) modify(x * 2, l, mid, k, y);
else modify(x * 2 + 1, mid + 1, r, k, y);
edge[x] = min(edge[x * 2], edge[x * 2 + 1]);
}
int query(int x, int l, int r, int L, int R) {
if (L > r || R < l) return 2e6;
if (L <= l && R >= r) return edge[x];
int mid = (l + r) >> 1, ans = 2e6;
if (L <= mid) ans = min(ans, query(x * 2, l, mid, L, R));
if (R > mid) ans = min(ans, query(x * 2 + 1, mid + 1, r, L, R));
return ans;
}
} //namespace Sgt2
array <set <int>, M> isl;
char strbuf[2];
bool _edmer;
int main() {
cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";
#ifndef cxqghzj
freopen("gcd.in", "r", stdin);
freopen("gcd.out", "w", stdout);
#endif
Euler(1e6), vis = 0, Sgt2::edge.fill(2e6);
int n = read(), q = read();
while (q--) {
scanf("%s", strbuf);
if (strbuf[0] == 'S') {
int x = read(), tp0 = x;
tupl tp(0, 0, 0);
for (int i = 1; i <= 170; i++) {
if (x % p[i] == 0) {
if (i <= 63) get <0>(tp) |= (1ull << i);
else if (i <= 126) get <1>(tp) |= (1ull << (i - 63));
else get <2>(tp) |= (1ull << (i - 126));
}
while (tp0 % p[i] == 0) tp0 /= p[i];
}
if (tp0 > 1) {
tp0 = idx[tp0];
if (!vis[x]) isl[tp0].insert(x);
auto it = isl[tp0].upper_bound(x);
int suf = (it == isl[tp0].end()) ? 2e6 : *it;
if (it == isl[tp0].end() || vis[x])
Sgt2::modify(1, 1, n, x, 2e6);
else Sgt2::modify(1, 1, n, x, suf);
if ((--it) != isl[tp0].begin())
it--, Sgt2::modify(1, 1, n, *it, vis[x] ? suf : x);
if (vis[x]) isl[tp0].erase(x);
}
if (vis[x]) tp = make_tuple(0, 0, 0);
Sgt1::modify(1, 1, n, x, tp);
vis[x] = vis[x] ^ 1;
}
else {
int l = read(), r = read();
if ((get <0>(Sgt1::query(1, 1, n, l, r)) & 1) || Sgt2::query(1, 1, n, l, r) <= r)
puts("yes");
else puts("no");
}
}
return 0;
}