P3396 哈希冲突 根号分治模板题

Posted kaka0010

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3396 哈希冲突 根号分治模板题相关的知识,希望对你有一定的参考价值。

原题链接:https://www.luogu.com.cn/problem/P3396

不过多介绍了,基础的分块技巧

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ul;
typedef pair<int, int> PII;
const ll inf = 2e18;
const int N = 2e5 + 10;
const int M = 1e6 + 10;
const ll mod = 1e9 + 7;
const double eps = 1e-8;

#define lowbit(i) (i & -i)
#define Debug(x) cout << (x) << endl
#define fi first
#define se second
#define mem memset
#define endl '\\n'

int a[N], n, m;
int sum[405][405];
int ask(int x, int y) //y池,模x
    int ans = 0;
    for (int i = y; i <= n; i += x) ans += a[i];
    return ans;

void add(int x, int val) //x位置,加val
    for (int i = 1; i <= 400; i++) 
        sum[x%i][i] += val;
    

inline void solve() 
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int p = 1; p <= 400; p++) 
        for (int i = 0; i < p; i++) 
            for (int j = i; j <= n; j += p) 
                sum[i][p] += a[j];
            
        
    
    while (m--) 
        char op; cin >> op;
        int x, y; cin >> x >> y;
        if (op == 'A') 
            if (x <= 400) 
                printf("%d\\n", sum[y][x]);
             else 
                printf("%d\\n", ask(x, y));
            
         else 
            add(x, -a[x]);
            add(x, y);
            a[x] = y;
        
    

signed main() 
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do 
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
     while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;


以上是关于P3396 哈希冲突 根号分治模板题的主要内容,如果未能解决你的问题,请参考以下文章

P3396 哈希冲突 (根号算法)

P3396 哈希冲突

P3396 哈希冲突

P3396 哈希冲突

P3396 哈希冲突

p3396 哈希冲突(暴力)