P3396 根号分治

题意

传送门 P3396 哈希冲突

题解

p ≤ N p\leq \sqrt N pN ,每次朴素查询复杂度过高, O ( N N ) O(N\sqrt N) O(NN ) 预处理, O ( N ) O(\sqrt N) O(N ) 修改, O ( 1 ) O(1) O(1) 查询;若 p > N p>\sqrt N p>N ,朴素查询 O ( N ) O(\sqrt N) O(N )。总时间复杂度 O ( N N ) O(N\sqrt N) O(NN )

#include <bits/stdc++.h>
using namespace std;
const int maxn = 150005, maxqt = 400;
int N, M, qt, A[maxn], rec[maxqt][maxqt];

int main()
{
    
    
    scanf("%d%d", &N, &M);
    for (int i = 1; i <= N; ++i)
        scanf("%d", A + i);
    qt = sqrt(N);
    for (int i = 1; i <= qt; ++i)
        for (int j = 1; j <= N; ++j)
            rec[i][j % i] += A[j];
    for (int i = 1, x, y; i <= M; ++i)
    {
    
    
        char op;
        scanf(" %c%d%d", &op, &x, &y);
        if (op == 'A')
        {
    
    
            int res = 0;
            if (x <= qt)
                res = rec[x][y];
            else
                for (int j = y; j <= N; j += x)
                    res += A[j];
            printf("%d\n", res);
        }
        else
        {
    
    
            for (int j = 1; j <= qt; ++j)
                rec[j][x % j] += y - A[x];
            A[x] = y;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/neweryyy/article/details/114856650