codeforces 1207-F Remainder Problem(分块)

题目:http://codeforces.com/problemset/problem/1207/F

题意:给一个全0数列,两种操作,1:a[x] += y ; 2:求所有下标满足 i % x = y 的a[i] 的和。

思路:按sqrt(n)=710分块,保存x<=710 的值,询问时若x<=710,直接输出;否则暴力查找即可,时间复杂度可以保证在O(n \sqrt{n} )

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 5e5+5;

LL n, a[maxn], b[715][715], op, x, y;
int main()
{
    cin >> n;
    while(n--){
        cin >> op >> x >> y;
        if(op == 1){
            a[x] += y;
            for(int i=1; i<=710; i++) b[i][x%i] += y;
        } else {
            if(x <= 710) cout << b[x][y] << endl;
            else {
                LL ans = 0;
                for(int i=y; i<=5e5; i+=x) ans += a[i];
                cout << ans << endl;
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值