LuoguP3396 哈希冲突

题目链接

对数据进行操作,数据结构是没跑了
但是之前接触的数据结构基本是对连续的几个数或者特定的一个区间进行操作
这个就很迷

考虑从暴力先开始入手

  for(int i=y;i<=N;i+=x) ans += a[i];

这样单词修改代价是O(N)
考虑优化暴力,也只有分块了
也就是我们要让这个代价变成根号
发现就是x大于根号N的时候

如果x小于根号N,就可以直接预处理出来

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define MAXN 150005

int a[MAXN],ans[400][400];
int N,M;

int main() {

    cin >> N >> M; int block = sqrt(N);
    for(int i=1;i<=N;++i) {
        cin >> a[i];
        for(int j=1;j<=block;++j) ans[j][i%j] += a[i];
    }

    char opt; int x,y;
    for(int i=1;i<=M;++i) {
        cin >> opt >> x >> y;
        if(opt=='A') {
            if(x<=block) cout << ans[x][y] << '\n';
            else {
                int an = 0;
                for(int j=y;j<=N;j+=x) an += a[j];
                cout << an << '\n';
            }
        }
        else {
            for(int j=1;j<=block;++j) ans[j][x%j] -= a[x];
            a[x] = y;
            for(int j=1;j<=block;++j) ans[j][x%j] += a[x];
        }
    }

    return 0;
}
posted @ 2021-11-04 10:26  Neworld1111  阅读(26)  评论(0编辑  收藏  举报