E. Remainder Problem 分块

 Two operations

  One pair of position x a [x] + y

  2 = y (mod x) find a [i] for all i and

  We certainly can not n ^ 2 run, stable overtime, but we can block consideration.

  N ^ 2 Why not? Because x relatively small, the number of summing operations too much. But when x is relatively large, and this has no effect on the time

  All we consider the block.

  And with a dp [i] [j] represents (1-5e5 length into the length of block x, the block and offset j) of all positions.

  Then operation 1, the blocks of a [pos] + = x, for all require long blocks 1 to sqrt (len) of the offset position pos which is maintained, in order to ensure a long interrogation block 1-sqrt (len) when we can o (1) answer

  If the block length is greater than sqrt (len), we have opened no less than the array, and maintenance time will exceed O (sqrt (5e5)), we consider direct violence

  Because at this time the block is longer than sqrt (len), and we add the maximum number of violence will sqrt (5e5), and with the greater number of blocks long, the fewer the number of

  Through this both cases, we put down to the time complexity o (sqrt (5e5)) = 700 * 5e5 queries, four seconds is also acceptable. (In fact, I feel like I can not accept, who told cf run faster). . .

  

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 5e5+6;
LL dp[760][760];
LL a[maxn];
int main(){
  int t,op;
  LL x,y;
  scanf("%d",&t);
  while(t--){
    scanf("%d",&op);
    if(op==1){
        scanf("%lld%lld",&x,&y);
        a[x]+=y;
        for(int i=1;i<750;i++){
            dp[i][x%i]+=y;
        }
    }else {
        scanf("%lld%lld",&x,&y);
        if(x<750){
          printf("%lld\n",dp[x][y]);
        }else {
         LL ans=0;
          for(int i=y;i<=5e5;i+=x){
             ans+=a[i];
          }
          printf("%lld\n",ans);
        }
    }
  }
  return 0;
}

 

Guess you like

Origin www.cnblogs.com/bluefly-hrbust/p/11416781.html