P3396 Radix divide e conquista

Título

Colisão de hash do portal P3396

responda

Se p ≤ N p \ leq \ sqrt NpN , A complexidade de cada consulta simples é muito alta, O (NN) O (N \ sqrt N)O ( NN ) Pré-processamento,O (N) O (\ sqrt N)O(N ) Modificar,O (1) O (1)O ( 1 ) consulta; sep> N p> \ sqrt Np>N , Consulta ingênua O (N) O (\ sqrt N)O(N ) . Complexidade de tempo totalO (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;
}

Acho que você gosta

Origin blog.csdn.net/neweryyy/article/details/114856650
Recomendado
Clasificación