luogu#P3396. 哈希冲突

哈希冲突

Background

As is well known, modular hashing produces collisions. For example, if the modulus p=7p=7, then 44 and 1111 collide.

Problem Description

B is very interested in hash collisions. He will give a sequence of positive integers value\text{value}.

Naturally, B will store these data into hash buckets. valuek\text{value}_k will be stored in the bucket (kmodp)(k \bmod p). This creates many collisions.

B will give many pairs pp and xx, asking for the sum of numbers in bucket xx under modulus pp.

In addition, B may change valuek\text{value}_k at any time. Each change takes effect immediately.

It is guaranteed that 1p<n1 \leq p < n.

Input Format

The first line contains two positive integers nn, mm, where nn is the length of the sequence, and mm is the number of operations by B.

The second line contains nn positive integers, representing the initial sequence.

Then mm lines follow. Each line starts with a character cmd\text{cmd}, followed by two integers xx, yy.

  • If cmd=A\text{cmd}=\text{A}, query the sum of numbers in bucket yy under modulus xx.
  • If cmd=C\text{cmd}=\text{C}, change valuex\text{value}_x to yy.

Output Format

For each query, output one positive integer as the answer.

10 5
1 2 3 4 5 6 7 8 9 10
A 2 1
C 1 20
A 3 1
C 5 1
A 5 0
25
41
11

Hint

Sample Explanation

A 2 1 has the answer 1+3+5+7+9=25.

A 3 1 has the answer 20+4+7+10=41.

A 5 0 has the answer 1+10=11.

Constraints

For 10%10\% of the testdata, n1000n \leq 1000, m1000m \leq 1000.

For 60%60\% of the testdata, n100000n \leq 100000, m100000m \leq 100000.

For 100%100\% of the testdata, n150000n \leq 150000, m150000m \leq 150000.

All testdata are valid, and 1valuei10001 \leq \mathrm{value}_i \leq 1000.

Translated by ChatGPT 5