P3396 Radix divide and conquer

Title

Portal P3396 hash collision

answer

If p ≤ N p\leq \sqrt NpN , The complexity of each simple query is too high, O (NN) O(N\sqrt N)O ( NN ) Preprocessing,O (N) O(\sqrt N)O (N ) Modify,O (1) O (1)O ( 1 ) query; ifp> N p>\sqrt Np>N , Naive query O (N) O(\sqrt N)O (N ) . Total time complexityO (NN) 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;
}

Guess you like

Origin blog.csdn.net/neweryyy/article/details/114856650