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

#SP0000241. Số nguyên tố gần nhất nhỏ hơn (Closest Prime)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Số nguyên tố gần nhất nhỏ hơn (Closest Prime)

Closest Prime

Source: CodeChef

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array of nn non-negative integers a1,a2,…,ana_1,a_2,\ldots,a_n, indexed from 11 to nn. Process qq operations in their given order.

  • 1 l r: for each position ii with l≤i≤rl\le i\le r, if ai>2a_i>2, find the greatest prime pp satisfying p<aip<a_i, and replace aia_i by ai−pa_i-p. Values in {0,1,2}\{0,1,2\} remain unchanged.
  • 2 l r: compute and print the current sum al+al+1+⋯+ara_l+a_{l+1}+\cdots+a_r.

The prime must be strictly smaller than the current value: for example, 55 becomes 5−3=25-3=2, not 00. Operations take effect in the order in which they appear.

Input

  • The first line contains two integers n,qn,q, the number of array elements and operations.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in index order.
  • Each of the following qq lines contains three integers t,l,rt,l,r. If t=1t=1, update the interval [l,r][l,r]; if t=2t=2, query its sum.

Output

For each operation of type 2, print one integer: the sum of the specified interval at that time, on its own line, in query order.

Subtasks

  • Subtask 1 (20 points): 1≤n≤1001\le n\le 100; 1≤q≤3001\le q\le 300; 0≤ai≤1060\le a_i\le 10^6 for all 1≤i≤n1\le i\le n; max⁡iai−min⁡iai<100200\max_i a_i-\min_i a_i<100200; every operation has t∈{1,2}t\in\{1,2\} and 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le 5000; 1≤q≤100001\le q\le 10000; 0≤ai≤1060\le a_i\le 10^6 for all 1≤i≤n1\le i\le n; max⁡iai−min⁡iai<100200\max_i a_i-\min_i a_i<100200; every operation has t∈{1,2}t\in\{1,2\} and 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000; 0≤ai≤109+1060\le a_i\le 10^9+10^6 for all 1≤i≤n1\le i\le n; max⁡iai−min⁡iai<100200\max_i a_i-\min_i a_i<100200; every operation has t∈{1,2}t\in\{1,2\} and 1≤l≤r≤n1\le l\le r\le n.

Examples

Input

5 7
2 3 4 5 11
2 1 5
1 2 5
2 1 5
1 1 5
2 1 5
1 1 5
2 3 5

Output

25
10
7
4

Explanation

Initially the array is [2,3,4,5,11][2,3,4,5,11]. The first query sums all entries: 2+3+4+5+11=252+3+4+5+11=25.

For update 1 2 5, positions 22 through 55 change as follows: 3→13\to1 by subtracting 22, 4→14\to1 by subtracting 33, 5→25\to2 by subtracting 33, and 11→411\to4 by subtracting 77. The resulting array [2,1,1,2,4][2,1,1,2,4] has sum 1010.

The next update 1 1 5 changes only 4→14\to1, so the array becomes [2,1,1,2,1][2,1,1,2,1] and its sum is 77. The subsequent update does not change any value because every entry is at most 22. The last query sums positions 33 through 55: 1+2+1=41+2+1=4.