跳到正文

个人记录

P3396 哈希冲突 做题记录

sample_mean_VVV约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟

版本

  1. v1当前

这不是分块……

按理来说绿题我一般不写做题记录,但是这道题是根号分治这种我还没学的题,所以写一下。

我们发现,我们可以处理出模上某个数余某个数的值,belike:

C++
for (rnt i=1;i<=n;i++)
  for (rnt j=1;j<n;j++)
    f[j][i%j]+=a[i];

然后我们发现这是 n2n^2 的,在 150000 这么大的数据下包 T 的,于是我们引入根号分治这个知识点。 自我理解就是把情况拆成两种,以 n\sqrt{n} 为分界,像这道题,小于 n\sqrt{n} 时,我们预处理出答案:

C++
for (rnt i = 1; i <= n; i++)
  for (rnt j = 1; j <= sqrt(n); j++)
    f[j][i % j] += a[i];

这样是 nnn\sqrt{n} 的,不会超时(最大是 7e67e6 左右),然后大于的部分我们暴力查找:

C++
ll ans = 0;
for (rnt i = y; i <= n; i += x)
  ans += a[i];
write(ans), pr(10);

这样查找的量不大,只有不到 n\sqrt{n} 个,所以总时间复杂度就是 (m+n)n(m+n)\sqrt{n} 的。 代码:

C++
//Just Sayori
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <utility>
#define ll long long
#define rnt register int
#define gr getchar
#define pr putchar
#define N 500005
#define M 10007
using namespace std;

inline ll read()
{
    ll x = 0, f = 1;
    char ch = gr();
    while (ch < '0' || ch > '9')
        ch == '-' ? f = -1, ch = gr() : ch = gr();
    while (ch >= '0' && ch <= '9')
        x = (x << 3) + (x << 1) + (ch ^ 48), ch = gr();
    return x * f;
}

inline void write(ll x)
{
    static int sta[39], top = 0;
    if (x < 0)
        pr('-'), x = -x;
    do
        sta[++top] = x % 10, x /= 10;
    while (x);
    while (top)
        pr(sta[top--] ^ 48);
}
int n, m, x, y, len;
int a[N];
ll f[400][400];
int main()
{
    char ch;
    n = read(), m = read(), len = sqrt(n);
    for (rnt i = 1; i <= n; i++)
        a[i] = read();
    for (rnt i = 1; i <= n; i++)
        for (rnt j = 1; j <= len; j++)
            f[j][i % j] += a[i];
    for (rnt i = 1; i <= m; i++)
    {
        cin >> ch, x = read(), y = read();
        if (ch == 'A')
        {
            if (x <= len)
                write(f[x][y]), pr(10);
            else
            {
                ll ans = 0;
                for (rnt i = y; i <= n; i += x)
                    ans += a[i];
                write(ans), pr(10);
            }
        }
        else
        {
            for (rnt i = 1; i <= len; i++)
                f[i][x % i] += y - a[x];
            a[x] = y;
        }
    }
    return 0;
}
原文