PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000014. Bài toán phần dư (Remainder Problem)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bài toán phần dư (Remainder Problem)

Remainder Problem

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Consider an array aa containing exactly 500 000500\,000 elements, indexed from 11 to 500 000500\,000. Initially, all ai=0a_i=0. Process qq operations in order:

  • 1 x y: Add integer yy (possibly negative or zero) to axa_x.
  • 2 x y: Calculate the sum of all aia_i such that 1≤i≤500 0001\le i\le500\,000 and i mod x=yi\bmod x=y.

In type-2 queries, xx is the modulus and yy the remainder, with 0≤y<x0\le y<x. If y=0y=0, only positive multiples of xx are included; index 00 is not part of the array. Each query observes all preceding updates.

Input

The first line contains qq. Each of the next qq lines is t x y, where t∈{1,2}t\in\{1,2\}. For t=1t=1, xx is an index and yy an increment. For t=2t=2, xx is a modulus and yy a remainder.

Output

For every type-2 query, print the sum of array values at indices belonging to the specified residue class. Negative answers are allowed.

Subtasks

  • Subtask 1 (10 points): 1≤q≤801\le q\le 80; mảng có 500 000500\,000 phần tử. Với loại 1: 1≤x≤500 0001\le x\le500\,000, −1000≤y≤1000-1000\le y\le1000. Với loại 2: 1≤x≤500 0001\le x\le500\,000, 0≤y<x0\le y<x. Bảo đảm có ít nhất một thao tác loại 2.
  • Subtask 2 (20 points): 1≤q≤50001\le q\le 5000; mảng có 500 000500\,000 phần tử. Với loại 1: 1≤x≤500 0001\le x\le500\,000, −1000≤y≤1000-1000\le y\le1000. Với loại 2: 1≤x≤500 0001\le x\le500\,000, 0≤y<x0\le y<x. Bảo đảm có ít nhất một thao tác loại 2.
  • Subtask 3 (30 points): 1≤q≤1000001\le q\le 100000; mảng có 500 000500\,000 phần tử. Với loại 1: 1≤x≤500 0001\le x\le500\,000, −1000≤y≤1000-1000\le y\le1000. Với loại 2: 1≤x≤500 0001\le x\le500\,000, 0≤y<x0\le y<x. Bảo đảm có ít nhất một thao tác loại 2.
  • Subtask 4 (40 points): 1≤q≤5000001\le q\le 500000; mảng có 500 000500\,000 phần tử. Với loại 1: 1≤x≤500 0001\le x\le500\,000, −1000≤y≤1000-1000\le y\le1000. Với loại 2: 1≤x≤500 0001\le x\le500\,000, 0≤y<x0\le y<x. Bảo đảm có ít nhất một thao tác loại 2.

Examples

Input

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

Output

4
4
0

Explanation

After 1 3 4, only a3=4a_3=4. The indices congruent to 00 modulo 33 are 3,6,9,…3,6,9,\ldots and sum to 44. The indices congruent to 33 modulo 44 are 3,7,11,…3,7,11,\ldots and also sum to 44. After adding −4-4 at index 44, the sum over all positive indices (remainder 00 modulo 11) is 4−4=04-4=0.