[codeforces1207F]Remainder Problem

time limit per test : 4 seconds
memory limit per test : 512 megabytes

分数:2100

You are given an array a a consisting of 500000 integers (numbered from 1 to 500000). Initially all elements of a a are zero.

You have to process two types of queries to this array:

1   x   y i n c r e a s e   a x   b y   y 1 \ x \ y — increase \ a_x \ by \ y ;
2   x   y c o m p u t e i R ( x , y ) a i 2 \ x \ y — compute ∑i∈_{R(x,y)}a_i , where R ( x , y ) R(x,y) is the set of all integers from 1 to 500000 which have remainder y y modulo x x . Can you process all the queries?

Input

The first line contains one integer q ( 1 q 500000 ) q(1≤q≤500000) — the number of queries.

Then q q lines follow, each describing a query. The i i -th line contains three integers t i , x i t_i, x_i and y i ( 1 t i 2 ) y_i (1≤t_i≤2) . If t i = 1 t_i=1 , then it is a query of the first type, 1 x i 500000 1≤x_i≤500000 , and 1000 y i 1000 −1000≤y_i≤1000 . If t i = 2 t_i=2 , then it it a query of the second type, 1 x i 500000 1≤x_i≤500000 , and 0 y i < x i 0≤y_i<x_i .

It is guaranteed that there will be at least one query of type 2 2 .

Output

For each query of type 2 2 print one integer — the answer to it.
Example
Input

5
1 3 4
2 3 0
2 4 3
1 4 -4
2 1 0

Output

4
4
0

题意:
刚开始有一个长度为500000的全0序列,有两种操作,第一种是将 a x a_x 变为 a x + y a_x+y ,第二种是询问所有 a i a_i 的和, i   m o d   x = y i \ mod \ x = y

题解:
首先第一种操作,单点修改,这个可以直接修改,然后对于操作2种所有 x < = 500000 x<=\sqrt{500000} 的情况,用 500000 \sqrt{500000} 的时间来更新一下答案。
然后第二种操作,查询,如果 x < = 500000 x<=\sqrt{500000} ,那么可以直接查询之前预处理过的结果。
如果 x > 500000 x>\sqrt{500000} ,那么则可以直接枚举所有的 i i (数量不会超过 500000 \sqrt{500000}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll sum[804][804];
ll a[500004];
int q;
int main(){
    memset(sum,0,sizeof(sum));
    memset(a,0,sizeof(a));
    scanf("%d",&q);
    while(q--){
        int t,x,y;scanf("%d%d%d",&t,&x,&y);
        if(t==1){
            a[x]+=y;
            for(int i=1;i<=800;i++)sum[i][x%i]+=y;
        }
        if(t==2){
            if(x<=800){
                printf("%lld\n",sum[x][y]);
            }
            else{
                ll ans=0;
                for(int i=y;i<=500000;i+=x)ans+=a[i];
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}
发布了302 篇原创文章 · 获赞 19 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/dxyinme/article/details/100179918