跳到正文

题解

P9989题解

Athanasy约 14 分钟
原文
文章信息与版本

文章信息

发布
2023年12月24日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 14 分钟
题目
P9989

版本

  1. v1当前

题目链接: P9989

在佬 Little09 的提示下,才学习到维护“区间LCM”的绝妙想法。

感谢 Little09、cpchenpi、aisib 佬们的指点帮助。


考虑对于一个数 xx 而言。容易知道一次 x=gcd⁡(x,y)x=\gcd(x,y) 运算将会有以下两个结果:

1、如果有 y mod x==0 y\bmod x ==0,那么很显然的是 xx 并不会发生任何改变

2、如果第一点不成立,那么 xx 至少变为 x/2 x / 2。那么很显然的一点是,这个单调递减的过程最终会使得 xx 变为 11。那么很容易知道最多做 O(log⁡Valuemax)O(\log{Value_{max}})。

考虑到暴力一点的过程:假如如果每个点每次取“GCD 操作”会变小而不是不变。那么最终对于所有点的操作总次数为 n×log⁡Valuemaxn \times \log{Value_{max}}。很显然的是我们现在的问题是如何快速跳过上述第一点提到的“并没有啥用的操作”。如果我们能保证每次操作只会执行第二点,而第一点则是快速跳过,那么很显然,这个问题就能很好地解决了。


考虑一个很妙的想法------维护“区间 LCM”,对于一堆数而言,一个显而易见的结论是

x mod lcm⁡==0,其中lcm⁡为这个区间的最小公倍数这个其实上述所提到不会发生变化的情况1,此时跳过 \begin{aligned} x \bmod \operatorname{lcm} ==0 \text{,其中} \operatorname{lcm} \text{为这个区间的最小公倍数}\\ \text{这个其实上述所提到不会发生变化的情况1,此时跳过} \end{aligned}

这个结论十分美妙。所以只需要考虑一类数据结构需要具备两个功能:

1、维护区间的 lcm与sumlcm \text{与} sum。

2、需要支持区间修改。

很显而易见的会想到的常见数据结构就是“线段树”了。(emmmm,当然你这个数据范围想分块卡卡常试试也不是不可以的。

那么这个线段树该如何写这个区间修改,结合上述所提的很自然就能想到修改的思路了:

如果 x mod 这个区间的lcm⁡==0x \bmod \texttt{这个区间的} \operatorname{lcm} ==0,那么很显然这个时候就是该停止递归了。否则我们类似“李超线段树”维护线段一样,继续向下递归,一直到单点位置,再暴力修改。因为可能大区间下的小区间里的每个数才是我们需要修改的对象。


最后的细节问题。这个范围单纯维护“区间 LCM”肯定会溢出的。我们来考虑维护它的用处,显然是是判断它是否是 xx 的因子。如果超过了 INFINF,那么很显然绝对不是了。所以设个 INFINF 上界取最小就行。


参考代码:

C++
#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast,unroll-loops")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if(typeid(x)==typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

constexpr int N = 2e5 + 10;
constexpr ll MOD = 1ll << 32;
constexpr i128 INF = 1e18 + 10;

struct Node
{
    ll lcm;
    ll sum;
} node[N << 2];

auto GCD(const ll x, const ll y) -> ll
{
    if (!y)return x;
    return GCD(y, x % y);
}

auto LCM(const ll x, const ll y) -> ll
{
    return min(static_cast<i128>(x) / GCD(x, y) * y, INF);
}

inline void push_up(const int curr)
{
    node[curr].sum = (node[ls(curr)].sum + node[rs(curr)].sum) % MOD;
    node[curr].lcm = LCM(node[ls(curr)].lcm, node[rs(curr)].lcm), INF;
}

inline void update(const int curr, const int l, const int r, const int s, const int e, const ll x)
{
    if (l <= s and e <= r)
    {
        if (x % node[curr].lcm == 0)return;
        if (s == e)
        {
            node[curr].lcm = GCD(node[curr].lcm, x);
            node[curr].sum = node[curr].lcm;
            return;
        }
    }
    const int mid = (s + e) >> 1;
    if (l <= mid)update(ls(curr), l, r, s, mid, x);
    if (r > mid)update(rs(curr), l, r, mid + 1, e, x);
    push_up(curr);
}

inline ll query(const int curr, const int l, const int r, const int s, const int e)
{
    if (l <= s and e <= r)return node[curr].sum;
    ll ans = 0;
    const int mid = (s + e) >> 1;
    if (l <= mid)ans = (ans + query(ls(curr), l, r, s, mid)) % MOD;
    if (r > mid)ans = (ans + query(rs(curr), l, r, mid + 1, e)) % MOD;
    return ans;
}

inline void build(const int curr, const int l, const int r)
{
    if (l == r)
    {
        cin >> node[curr].sum;
        node[curr].lcm = node[curr].sum;
        return;
    }
    const int mid = (l + r) >> 1;
    build(ls(curr), l, mid);
    build(rs(curr), mid + 1, r);
    push_up(curr);
}

int n, q;

inline void solve()
{
    cin >> n >> q;
    build(1, 1, n);
    while (q--)
    {
        int op;
        cin >> op;
        if (op == 1)
        {
            ll l, r, x;
            cin >> l >> r >> x;
            update(1, l, r, 1, n, x);
        }
        else
        {
            ll l, r;
            cin >> l >> r;
            cout << query(1, l, r, 1, n) << endl;
        }
    }
}

signed int main()
{
    Spider
    //------------------------------------------------------
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
}
原文