P5398 [Ynoi2018] GOSICK 题解

题目链接: GOSICK

前置知识点:莫队二次离线、根号分治、调和级数

我的评价是逆天题,不愧为 \(Ynoi\) 第十四分块,拥有这些前置知识点可以让你比较快的思考出本题正解,然而本题独特的空间限制,逼迫你写出更多卡常细节代码。

好多人搞了两个数组单独处理特判,其实莫得必要的。先说一点常识性的东西:

枚举 \(1 \sim n\) 所有数的倍数,且不超过 \(n\)

对于 \(1\) 来说有 \(\dfrac{n}{1}\) 个倍数,对于 \(2\) 来说有 \(\dfrac{n}{2}\) 个倍数,以此类推,总枚举次数为:\(n \times (1+\dfrac{1}{2}+\dfrac{1}{3}+\dfrac{1}{4}+.....\dfrac{1}{n}) \sim n \times (\ln{n}+C)\),这玩意就是调和级数。虽然本题没啥大用,但要知道。

枚举 \(1 \sim n\) 所有数的因数,且不超过 \(n\)

容易知道,只需要每个数枚举 \(1 \sim \sqrt{i}\),因为因数是对称的,所以每个 \(\le \sqrt{i}\) 的因数,都有一个对应 \(\ge \sqrt{i}\) 的因数,稍微特判下平方数,我们就能做到 \(n\sqrt{n}\) 级别枚举完。

PS: 忘了最经典的 \(n\ln{n}\) 做法了,如果可以预处理的情况下,上述的做法只适合求单个的,我们可以枚举因数 \(i\),然后每次 \(+i\) 寻找它的倍数的情况下,同时也找到了因数。

莫二离线的扫描线条数规模

莫二离线的扫描线取决于每次 \(l\)\(r\) 移动,每次至多各种往一个方向移动,如果 \(0\) 处也计贡献的情况下,那么就是满的 \(O(2q)\),这个 \(2\) 可别忽略,本题这个 \(2 \times 5e5=1e6\) 可是相当巨大的。随机数据下扫描线条数规模约为 \(O(2q)\),特意构造下,全是 \([1,i]\) 这种查询,且 \(0\) 处不计入贡献,当然是 \(0\) 了,计入贡献的情况下是满的。

叙述本题

本题其实很好思考的一点就是,由于任意的 \(i\)\(j\) 都能互相配对,那么对于一个数的加入或者删除,涉及到的只有两大类贡献:

  1. 包括等于它自身,已经存在的数是它的倍数的数量。

  2. 包括等于它自身,已经存在的数是它的因数的数量。

很显然,如果在已经存在的数当中,有的数等于它,那么我们不妨这样设,\(a_j\)\(a_i\) 的倍数。那么很显然当 \(a_i=a_j\) 时,\((i,j)\)\((j,i)\) 都要计为一种贡献。换句话来说,除开它自身,其他的包括和它相等的数可以作为它的倍数的数量 \(+\) 可以作为它的因数的数量之和即为它的贡献。

老规矩,分析两块复杂度。对于一个数的加入来说,我们枚举它的因子,可以轻松地为它的因子的 倍数贡献+1 。这个倍数贡献就解决了。对于因数贡献部分,这玩意我们发现,虽然说枚举 \(1 \sim n\) 的所有数这玩意是调和级数,可以与预处理出来,但架不住原数组当中有一大堆重复的 小数,它们枚举倍数复杂度会退化到 \(O(n)\)。这玩意其实很明显,根号分治,大数暴力,小数单独处理。一般涉及到这个 \(i \times x\) 问题,就往根号分治方面想想有没有问题,比如经典的 \(start+k \times step\) 根号分治问题。

大的枚举就行了,这个我们可以用一个分割点 \(split\) 表示,大于等于它的就是大数,否则为小数。大数暴力,小数就得仔细思考了,也是本题难点。首先预处理小数很简单,直接枚举小数计入贡献就行了。关键是这玩意至多是 \(\sqrt{n}\),我们在处理普通莫队时没办法让修改这么大,那很显然,这玩意仔细观察下,是一个可差性问题,就是可以莫二传统讨论的前缀和差分。(其实大部分这种莫队计数类问题都可以莫二优化,可以看看我之前发的一些题解),为啥可以?大概讲讲:

莫队计数类问题常常以这种方式或者可以转化为这种方式询问你:某个区间上满足某个要求的对有多少,可能 \(i<j\) 也可能没限制。那么我们对于某个点 \(i\) 的贡献都可以换句话讲:\([0,r]\) 上有多少个与 \(i\) 满足的对数去掉 \([0,l-1]\) 上有多少个与 \(i\) 满足的对数,这类问题我们称之为 可差性问题

莫二处理,我们倍数及其大数的因数贡献还是按上述说的,传统暴力插入桶枚举单点解决。主要是小数的因数贡献,首先我们可以枚举小数这个复杂度为 \(O(split)\),很容易想到,内层复杂度不能超过 \(O(Cn)\)\(C\) 为一个常数,保证 \(C \times split \times n\) 尽量为:\(O(n\sqrt{n})\) 级别。咋办,我们其实发现,对于一个小数 \(val\),来说,我们虽然不能它的枚举倍数,但别忘了根号分治常见的预处理,换位思考,但我们可以在原数组中找到哪些数是它的倍数。这玩意就是一个暴力的 \(n\times spilt\),我们可以预处理出这种数量前缀和,这样可以拿到一个小数能对够作为某个区间 \([l,r]\) 上多少个数的因数贡献,再乘上此时此刻桶里的小数个数。就这样做完了。

然后做完拍了下,\(5e5\) 没法跑,因为扫描线接近 \(1e6\) 条,你的莫二关于小数部分的复杂度为 \(split \times 1e6\),虽然显然 \(split\) 你可不能直接取 \(\sqrt{n}\),我们平衡下复杂度,让 \(split\) 小一点,这样枚举倍数的复杂度大一些,但扫描线处理小数的复杂度就小了很多。这里我们最好别用常见的扫描线写法,那样会小数每次遍历 \(O(n)\) 再附带扫描线,这样一来并不是严格 \(O(split \times n)\) 常数实测挺大的,我们可以离线下来全放一个数组里保证是严格复杂度。然后说下这个贡献问题,其实只需要注意到一点,就是一开始我们预处理前缀贡献时,这个数在原数组当中的因素和倍数都可以和它组成贡献答案,这个数加入以后,自身会和自身形成一个 \(1\) 的贡献,所以我们先查答案然后 \(+1\),再插入这个数,我看有些人反而先加再查维护两个数组,一个表示相等,一个表示不等的倍数和因数就过于麻烦了,一个数组就够了。然后再拿个桶维护数的情况,我们枚举小数,桶表示当前前缀小数的个数,如果可以作为当前数因子那就加上它。这样前缀贡献就做完了。

至于二莫部分那就更简单了,因为本题并没有 \(i \neq j\) 的限制,所以也不用特判有重叠部分去贡献问题。直接插入这个数的因子,作为倍数贡献,还有这个数如果为大数,就加入它的倍数作为因子贡献。然后单独处理小数的倍数的的因子贡献就行了。并不需要特判什么相等,本题的询问是很宽松的,只有预处理部分注意一些细节。

给出一份我一开始 \(MLE\) 的代码:

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#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;
}

struct Hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ x >> 30) * 0xbf58476d1ce4e5b9;
        x = (x ^ x >> 27) * 0x94d049bb133111eb;
        return x ^ x >> 31;
    }

    static size_t get(const uint64_t x)
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }

    template <typename T>
    size_t operator()(T x) const
    {
        return get(std::hash<T>()(x));
    }

    template <typename F, typename S>
    size_t operator()(pair<F, S> p) const
    {
        return get(std::hash<F>()(p.first)) ^ std::hash<S>(p.second);
    }
};

constexpr int N = 5e5 + 10;
constexpr int SIZE = sqrt(N) + 1;
unordered_set<int, Hash> vis, minFac;
int up;
vector<int> fac[N], mul[N];
int preMul[SIZE][N];
int pre[N], pos[N], contribute[N], cnt[N];
ll ans[N];
vector<tii> seg[N];

struct Mo
{
    int l, r, id;

    bool operator<(const Mo& other) const
    {
        return pos[l] ^ pos[other.l] ? l < other.l : pos[l] & 1 ? r < other.r : r > other.r;
    }
} node[N];

int n, q;
int a[N];
constexpr int split = 100;

inline void add(const int val)
{
    cnt[val]++;
    for (const auto v : fac[val])contribute[v]++;
    for (const auto v : mul[val])contribute[v]++;
}

inline void solve()
{
    cin >> n >> q;
    int siz = sqrt(n);
    forn(i, 1, n)
    {
        pos[i] = (i - 1) / siz + 1;
        cin >> a[i];
        uMax(up, a[i]);
    }
    forn(i, 1, n)
    {
        vis.insert(a[i]);
        if (a[i] < split)minFac.insert(a[i]);
    }
    for (const auto v : vis)
    {
        const int T = sqrt(v);
        forn(i, 1, T)
        {
            if (v % i == 0)
            {
                if (vis.contains(i))fac[v].push_back(i);
                if (v / i != i and vis.contains(v / i))fac[v].push_back(v / i);
            }
        }
        if (v >= split)for (int tmp = v; tmp <= up; tmp += v)if (vis.contains(tmp))mul[v].push_back(tmp);
    }
    for (const auto v : minFac)
    {
        forn(i, 1, n)preMul[v][i] = preMul[v][i - 1] + (a[i] % v == 0);
    }
    forn(i, 1, n)
    {
        pre[i] = contribute[a[i]] + 1;
        for (const auto v : minFac)pre[i] += cnt[v] * (a[i] % v == 0);
        add(a[i]);
    }
    forn(i, 1, q)
    {
        auto& [l,r,id] = node[i];
        cin >> l >> r, id = i;
    }
    sortArr(node, q);
    int l = 1, r = 0;
    forn(i, 1, q)
    {
        auto [L,R,id] = node[i];
        if (l < L)seg[r].emplace_back(l, L - 1, -id);
        while (l < L)ans[id] += pre[l++];
        if (l > L)seg[r].emplace_back(L, l - 1, id);
        while (l > L)ans[id] -= pre[--l];
        if (r < R)seg[l - 1].emplace_back(r + 1, R, -id);
        while (r < R)ans[id] += pre[++r];
        if (r > R)seg[l - 1].emplace_back(R + 1, r, id);
        while (r > R)ans[id] -= pre[r--];
    }
    memset(contribute, 0, sizeof contribute);
    memset(cnt, 0, sizeof cnt);
    ll sum = 0;
    forn(i, 1, n)
    {
        add(a[i]);
        if (!seg[i].empty())
        {
            for (const auto [l,r,id] : seg[i])
            {
                ll v = id / abs(id);
                forn(j, l, r)ans[abs(id)] += v * contribute[a[j]];
            }
            for (const auto val : minFac)
            {
                if (cnt[val] == 0)continue;
                for (const auto [l,r,id] : seg[i])
                {
                    ll v = id / abs(id);
                    ans[abs(id)] += v * (preMul[val][r] - preMul[val][l - 1]) * cnt[val];
                }
            }
        }
    }
    forn(i, 1, q)ans[node[i].id] += ans[node[i - 1].id];
    forn(i, 1, q)cout << ans[i] << endl;
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

本题对空间限制太大了,我们需要优化。

首先那个二维预处理刚刚说的每个小数的前缀和该丢了,改为一维的,每次枚举小数时再预处理前缀和。然后嘛,可以不用哈希表判断是否存在某个数,我们改为布尔数组就行了。常数还小一些,对于每个数的因子桶,我们布尔数组判断是否存在,存在才插入这个数。然后继续观察常数大的地方,我个人习惯莫队预处理单点的 \([1,i]\) 贡献,我们其实可以再多处理一遍前缀和,这样莫队指针移动我们可以直接用前缀和计算了。\(split\)\(100\) 还是太大了,取 \(50\),让小数的复杂度下降到 \(5e7\)。考虑下还有一个大常数的地方,就是由于莫二还要再进行一遍大数倍数和每个因数的插入,这个复杂度较高,我们可以不先预处理,先跑一遍莫二拿到扫描线,然后优先跑大数倍数和因数贡献的扫描线,边跑边算预处理数组,跑完以后 再跑一遍莫队把贡献加进去,这样就只进行了一次的插入元素。最后上点快读,其实也不需要快读也能过。去掉一些无用的扫描线,即 \(0\) 处扫描线。

最终参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#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 = 5e5 + 10;
int siz;
int up;
vector<int> fac[N];
int pre[N], contribute[N], cnt[N];
ll ans[N];
#define pos(t) ((t-1)/siz+1)


struct Mo
{
    int l, r, id;

    bool operator<(const Mo& other) const
    {
        return pos(l) ^ pos(other.l) ? l < other.l : pos(l) & 1 ? r < other.r : r > other.r;
    }
} node[N];

int n, q;
int a[N];
constexpr int split = 50;

inline void add(const int val)
{
    for (const auto v : fac[val])contribute[v]++;
}

int idx;
typedef tuple<int, int, int, int> T4;
T4 seg[N << 1];
bool vis[N];

inline void solve()
{
    read(n, q);
    siz = sqrt(n);
    forn(i, 1, n)
    {
        read(a[i]);
        vis[a[i]] = true;
        uMax(up, a[i]);
    }
    forn(i, 1, n)
    {
        const int v = a[i];
        const int T = sqrt(v);
        if (fac[v].empty())
        {
            forn(j, 1, T)
            {
                if (v % j == 0)
                {
                    if (vis[j])fac[v].push_back(j);
                    if (v / j != j and vis[v / j])fac[v].push_back(v / j);
                }
            }
        }
    }
    forn(i, 1, q)
    {
        auto& [l,r,id] = node[i];
        read(l, r), id = i;
    }
    sortArr(node, q);
    int l = 1, r = 0;
    forn(i, 1, q)
    {
        auto [L,R,id] = node[i];
        if (l < L)seg[++idx] = T4(r, l, L - 1, -id), l = L;
        if (l > L)seg[++idx] = T4(r, L, l - 1, id), l = L;
        if (r < R)
        {
            if (l > 1)seg[++idx] = T4(l - 1, r + 1, R, -id);
            r = R;
        }
        if (r > R)
        {
            if (l > 1)seg[++idx] = T4(l - 1, R + 1, r, id);
            r = R;
        }
    }
    sortArr(seg, idx);
    int curr = 0;
    forn(t, 1, idx)
    {
        const auto [i,l,r,id] = seg[t];
        while (curr < i)
        {
            const int val = a[++curr];
            pre[curr] = contribute[a[curr]] + 1;
            forn(val, 1, split-1)pre[curr] += cnt[val] * (a[curr] % val == 0);
            cnt[val]++;
            add(val);
            if (val >= split)for (int tmp = val; tmp <= up; tmp += val)contribute[tmp]++;
        }
        ll v = id / abs(id);
        forn(j, l, r)ans[abs(id)] += v * contribute[a[j]];
    }
    while (curr < n)
    {
        const int val = a[++curr];
        pre[curr] = contribute[a[curr]] + 1;
        forn(val, 1, split-1)pre[curr] += cnt[val] * (a[curr] % val == 0);
        cnt[val]++;
        add(val);
        if (val >= split)for (int tmp = val; tmp <= up; tmp += val)contribute[tmp]++;
    }
    forn(i, 1, n)pre[i] += pre[i - 1];
    l = 1, r = 0;
    forn(i, 1, q)
    {
        auto [L,R,id] = node[i];
        if (l < L)ans[id] += pre[L - 1] - pre[l - 1], l = L;
        if (l > L)ans[id] -= pre[l - 1] - pre[L - 1], l = L;
        if (r < R)ans[id] += pre[R] - pre[r], r = R;
        if (r > R)ans[id] -= pre[r] - pre[R], r = R;
    }
    forn(val, 1, split-1)
    {
        forn(i, 1, n) pre[i] = pre[i - 1] + (a[i] % val == 0), cnt[i] = cnt[i - 1] + (a[i] == val);
        forn(t, 1, idx)
        {
            const auto [i,l,r,id] = seg[t];
            ll v = id / abs(id);
            ans[abs(id)] += v * (pre[r] - pre[l - 1]) * cnt[i];
        }
    }
    forn(i, 1, q)ans[node[i].id] += ans[node[i - 1].id];
    forn(i, 1, q)write(endl, ans[i]);
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    // read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[期望时间复杂度为:\ O(\dfrac{n^2}{split}+2q\times split+n(\sqrt{q}+\sqrt{V_{max}})) \]

PS:预处理因数的 \(O(n\ln{n})\) 做法

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#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 = 5e5 + 10;
int siz;
int up;
vector<int> fac[N];
int pre[N], contribute[N], cnt[N];
ll ans[N];
#define pos(t) ((t-1)/siz+1)


struct Mo
{
    int l, r, id;

    bool operator<(const Mo& other) const
    {
        return pos(l) ^ pos(other.l) ? l < other.l : pos(l) & 1 ? r < other.r : r > other.r;
    }
} node[N];

int n, q;
int a[N];
constexpr int split = 50;

inline void add(const int val)
{
    for (const auto v : fac[val])contribute[v]++;
}

int idx;
typedef tuple<int, int, int, int> T4;
T4 seg[N << 1];
bool vis[N];
constexpr int MX = 5e5;

inline void solve()
{
    read(n, q);
    siz = sqrt(n);
    forn(i, 1, n)
    {
        read(a[i]);
        vis[a[i]] = true;
        uMax(up, a[i]);
    }
    forn(i, 1, MX)for (int j = i; j <= MX; j += i)if (vis[j])fac[j].push_back(i);
    forn(i, 1, q)
    {
        auto& [l,r,id] = node[i];
        read(l, r), id = i;
    }
    sortArr(node, q);
    int l = 1, r = 0;
    forn(i, 1, q)
    {
        auto [L,R,id] = node[i];
        if (l < L)seg[++idx] = T4(r, l, L - 1, -id), l = L;
        if (l > L)seg[++idx] = T4(r, L, l - 1, id), l = L;
        if (r < R)
        {
            if (l > 1)seg[++idx] = T4(l - 1, r + 1, R, -id);
            r = R;
        }
        if (r > R)
        {
            if (l > 1)seg[++idx] = T4(l - 1, R + 1, r, id);
            r = R;
        }
    }
    sortArr(seg, idx);
    int curr = 0;
    forn(t, 1, idx)
    {
        const auto [i,l,r,id] = seg[t];
        while (curr < i)
        {
            const int val = a[++curr];
            pre[curr] = contribute[a[curr]] + 1;
            forn(val, 1, split-1)pre[curr] += cnt[val] * (a[curr] % val == 0);
            cnt[val]++;
            add(val);
            if (val >= split)for (int tmp = val; tmp <= up; tmp += val)contribute[tmp]++;
        }
        ll v = id / abs(id);
        forn(j, l, r)ans[abs(id)] += v * contribute[a[j]];
    }
    while (curr < n)
    {
        const int val = a[++curr];
        pre[curr] = contribute[a[curr]] + 1;
        forn(val, 1, split-1)pre[curr] += cnt[val] * (a[curr] % val == 0);
        cnt[val]++;
        add(val);
        if (val >= split)for (int tmp = val; tmp <= up; tmp += val)contribute[tmp]++;
    }
    forn(i, 1, n)pre[i] += pre[i - 1];
    l = 1, r = 0;
    forn(i, 1, q)
    {
        auto [L,R,id] = node[i];
        if (l < L)ans[id] += pre[L - 1] - pre[l - 1], l = L;
        if (l > L)ans[id] -= pre[l - 1] - pre[L - 1], l = L;
        if (r < R)ans[id] += pre[R] - pre[r], r = R;
        if (r > R)ans[id] -= pre[r] - pre[R], r = R;
    }
    forn(val, 1, split-1)
    {
        forn(i, 1, n) pre[i] = pre[i - 1] + (a[i] % val == 0), cnt[i] = cnt[i - 1] + (a[i] == val);
        forn(t, 1, idx)
        {
            const auto [i,l,r,id] = seg[t];
            ll v = id / abs(id);
            ans[abs(id)] += v * (pre[r] - pre[l - 1]) * cnt[i];
        }
    }
    forn(i, 1, q)ans[node[i].id] += ans[node[i - 1].id];
    forn(i, 1, q)write(endl, ans[i]);
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    // read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[期望时间复杂度为:\ O(\dfrac{n^2}{split}+2q\times split+n(\sqrt{q}+\ln{V_{max}})) \]

posted @ 2024-02-07 00:55  Athanasy  阅读(13)  评论(0编辑  收藏  举报