跳到正文

题解

题解:CF1732E Location

Swirling约 14 分钟
原文

这是 2026年3月9日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年3月9日
更新
2026年3月9日
篇幅
约 14 分钟
题目
CF1732E

版本

  1. v2当前
  2. v1

这是我第三次写这篇题解,前两次没保存炸掉了。

题目大意:两个序列 a,ba, b,你需要支持区间覆盖 aa,区间查询 min⁡lcm(ai,bi)gcd⁡(ai,bi)\min \frac{\text{lcm}(a_i, b_i)}{\gcd(a_i, b_i)}。

这个题 ODT 应该不是很好做,一个连续段每个位置的答案都不一样不好处理。当然我也不确定。

鄙人不会除了分块的其他东西,所以我们直接上序列分块。

查询的时候散块直接暴力查,整块预处理,记 gig_i 为第 ii 块的答案。修改的时候散块仍然暴力,不过整块不是很好做。考虑记区间覆盖标记 tit_i。然后你发现似乎还是不是很好做,tt 与 gg 的处理没法快速解决,咋办。

不咋办,直接暴力。记 md,im_{d, i} 为第 ii 块中所有 dd 的倍数 bjb_j 的 bjd\frac{b_j}{d} 的最小值。整块修改的时候我们可以枚举 xx 的所有因数,更新 gi=min⁡p∣xmd,i×xpg_i = \min _ {p | x}m_{d, i} \times \frac{x}{p}。

时间复杂度:O(nnlog⁡n+qnτ(V))\mathcal{O}(n\sqrt{n}\log n + q\sqrt{n}\tau(V)),其中 τ(V)\tau(V) 为 VV 的因数个数。

理论上 τ(V)∼n1/3\tau(V) \sim n ^ {1 / 3},实测 max⁡τ(x)\max \tau (x) 不超过 100100,无伤大雅。

code

实现上,可以搞一个 O(1)\mathcal{O}(1) 的 gcd⁡\gcd。具体的,上值域分块。

5×1045 \times 10^4 以内的任何整数,都可以被分解为最多 33 个因数的乘积,且这 33 个因数中,至多只有一个大于 320320。

存下来,小因数则拿一个 GB,BG_{B, B} 的数组预处理。具体见代码。

C++
#include <bits/stdc++.h>
#define fastio
#define int long long
#define bw(x) (1 << (x))
#define eb emplace_back
#define pii pair<int, int>
#define inf (0x3f3f3f3f3f3f3f3f)
#define F(x, v) for (auto x : (v))
#define ALL(x) (x).begin(), (x).end()
#define L(i, a, b) for (register int i = (a), i##END = (b); i <= i##END; ++i)
#define R(i, a, b) for (register int i = (a), i##END = (b); i >= i##END; --i)
#define FRE(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
#define debug(a) cerr << "\033[32m[DEBUG] " << #a << " = " << (a) << " at line " << __LINE__ << "\033[0m\n"
using namespace std; bool bgmem;
#ifdef fastio
struct IO {
	#define ion bw(20)
	char i[ion], o[ion], *icl = i, *icr = i, *oc = o;
	char gc() { return (icl == icr && (icr = (icl = i) + fread(i, 1, ion, stdin), icl == icr)) ? EOF : *icl++; }
	void pc(char c) { if (oc - o == ion) fwrite(o, 1, ion, stdout), oc = o; *oc++ = c; }
	void rd(auto &x) { char c; int f = 1; x = 0; while (c = gc(), c < '0' || c > '9') if (c == '-') f = -1; while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); x *= f; } 
	void pr(auto x) { int a[64], p = 0; if (x < 0) x = -x, pc('-'); do a[p++] = x % 10; while (x /= 10); while (p--) pc(a[p] + '0'); } 
	IO& operator>>(char &c) { return c = gc(), *this; }
	IO& operator<<(char c) { return pc(c), *this; }
	IO& operator<<(const char *c) { while (*c) pc(*c++); return *this; }
	IO& operator>>(auto &x) { return rd(x), *this; }
	IO& operator<<(auto x) { return pr(x), *this; }
} io;
#define cin io
#define cout io
#endif
template < const int P > class modint { public:
	int x;
	modint(int _ = 0) { x = (_ % P + P) % P; }
	explicit operator int() { return x; }
	friend modint operator+(modint x, modint y) { return x.x + y.x < P ? x.x + y.x : x.x + y.x - P; }
	friend modint operator-(modint x, modint y) { return x.x - y.x < 0 ? x.x - y.x + P : x.x - y.x; }
	friend modint operator*(modint x, modint y) { return modint((__int128)(x.x * y.x)); }
	modint& operator+=(modint x) { return (*this) = (*this) + x; }
	modint& operator-=(modint x) { return (*this) = (*this) - x; }
	modint& operator*=(modint x) { return (*this) = (*this) * x; }
};
inline int cmax(int& x, int c) { return x = max(x, c); }
inline int cmin(int& x, int c) { return x = min(x, c); }
int tes = 1, cas;
namespace _ {
    const int N = 5e4 + 5, B = 300, MB = N / B + 5, SQ = 320, inf_32 = 1e9;
    int n, q, a[N], b[N], g[MB], bl[N], l[MB], r[MB], tg[MB], M[N][MB], fac[N][3], _g[SQ][SQ], pri[N], C;
    vector< int > divs[N]; bool vis[N];
    int gcd_(int a, int b) {
        int ans = 1;
        for (int i = 0; i < 3; ++i) {
            int f = fac[a][i];
            int _ = f >= SQ ? (b % f ? 1 : f) : _g[f][b % f];
            b /= _, ans *= _;
        }
        return ans;
    }
    void bld() {
        L(i, 1, n) bl[i] = (i - 1) / B + 1;
        L(i, 1, bl[n]) {
            l[i] = (i - 1) * B + 1;
            r[i] = min(n, i * B);
        }
        L(d, 1, N - 5) L(i, 1, bl[n]) M[d][i] = inf_32;
        L(i, 1, bl[n]) L(j, l[i], r[i]) F(d, divs[b[j]]) cmin(M[d][i], b[j] / d);
    } void reb(int id) {
        g[id] = inf;
        L(i, l[id], r[id]) {
            int d = gcd_(a[i], b[i]);
            cmin(g[id], a[i] / d * b[i] / d);
        }
    } void down(int id) {
        if (!tg[id]) return;
        L(i, l[id], r[id]) a[i] = tg[id];
        tg[id] = 0;
    } void assign(int ql, int qr, int x) {
        int lb = bl[ql], rb = bl[qr];
        if (lb == rb) {
            down(lb);
            L(i, ql, qr) a[i] = x;
            reb(lb);
            return;
        }
        down(lb);
        L(i, ql, r[lb]) a[i] = x;
        reb(lb);
        auto& dx = divs[x];
        L(i, lb + 1, rb - 1) {
            tg[i] = x;
            g[i] = inf;
        } F(d, dx) {  // 暴力
            int x_d = x / d;
            L(i, lb + 1, rb - 1) if (M[d][i] != inf_32) 
                cmin(g[i], x_d * M[d][i]);
        }
        down(rb);
        L(i, l[rb], qr) a[i] = x;
        reb(rb);
    } int qry(int ql, int qr) {
        int lb = bl[ql], rb = bl[qr], ans = inf;
        if (lb == rb) {
            down(lb);
            L(i, ql, qr) {
                int d = gcd_(a[i], b[i]);
                cmin(ans, a[i] / d * b[i] / d);
            }
            return ans;
        }
        down(lb);
        L(i, ql, r[lb]) {
            int d = gcd_(a[i], b[i]);
            cmin(ans, a[i] / d * b[i] / d);
        }
        L(i, lb + 1, rb - 1) cmin(ans, g[i]);
        down(rb);
        L(i, l[rb], qr) {
            int d = gcd_(a[i], b[i]);
            cmin(ans, a[i] / d * b[i] / d);
        }
        return ans;
    }
    void init() { 
        L(i, 1, N - 5) for (int j = i; j <= N - 5; j += i) divs[j].eb(i);
        vis[0] = vis[1] = true;
        fac[1][0] = fac[1][1] = fac[1][2] = 1;
        L(i, 2, N - 5) {  // 质数筛
            if (!vis[i]) {
                fac[i][0] = fac[i][1] = 1;
                fac[i][2] = i;
                pri[++C] = i;
            }
            L(x, 1, C) {
                int j = pri[x];
                int mul = i * j;
                if (mul > N - 5) break;
                vis[mul] = true;
                fac[mul][0] = fac[i][0] * j, fac[mul][1] = fac[i][1], fac[mul][2] = fac[i][2];
                sort(fac[mul], fac[mul] + 3);
                if (i % j == 0) break;
            }
        }
        L(i, 0, SQ - 1) _g[0][i] = _g[i][0] = i;
        L(i, 1, SQ - 1) L(j, 1, i) _g[i][j] = _g[j][i] = _g[j][i % j];
    }
    void clear() { L(i, 1, MB - 1) tg[i] = 0; }
    void solve() {
        cin >> n >> q;
        L(i, 1, n) cin >> a[i];
        L(i, 1, n) cin >> b[i];
        bld();
        L(i, 1, bl[n]) reb(i);
        L(_, 1, q) {
            int op, lq, rq, x;
            cin >> op;
            if (op == 1) cin >> lq >> rq >> x, assign(lq, rq, x);
            if (op == 2) cin >> lq >> rq, cout << qry(lq, rq) << "\n";
        }
    }
} bool edmem; signed main() {
    // FRE("", "");
#ifndef fastio
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
#endif
	// cin >> tes;
    _::init(); while (++cas <= tes) { _::clear(), _::solve(); }
#ifdef fastio
	fwrite(io.o, 1, io.oc - io.o, stdout);
#endif
    cerr << "time  : " << (double)clock() / CLOCKS_PER_SEC * 1000 << "ms\n";
    cerr << "memory: " << fabs(&edmem - &bgmem) / 1024 / 1024 << "mb\n";
    return 0;
}
// I will never love you again. =(