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 
#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;
            }
        }
    }
}

 

你可能感兴趣的:(分块)