codeforces 1207-F Remainder Problem(ブロック)

1130 ワード

タイトル:http://codeforces.com/problemset/problem/1207/F
題意:全0数列、2つの操作を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;
            }
        }
    }
}