|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) |
| 24 | + nextLine(); |
| 25 | + return st.nextToken(); |
| 26 | + } |
| 27 | + |
| 28 | + int nextInt() throws Exception { |
| 29 | + return Integer.parseInt(nextToken()); |
| 30 | + } |
| 31 | + |
| 32 | + long nextLong() throws Exception { |
| 33 | + return Long.parseLong(nextToken()); |
| 34 | + } |
| 35 | + |
| 36 | + double nextDouble() throws Exception { |
| 37 | + return Double.parseDouble(nextToken()); |
| 38 | + } |
| 39 | + |
| 40 | + void close() throws Exception { |
| 41 | + bw.flush(); |
| 42 | + bw.close(); |
| 43 | + } |
| 44 | + |
| 45 | + void write(String content) throws Exception { |
| 46 | + bw.write(content); |
| 47 | + } |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +public class Main { |
| 52 | + |
| 53 | + static IOController io; |
| 54 | + |
| 55 | + // |
| 56 | + |
| 57 | + static int N, Q; |
| 58 | + static int[] f, g, sieve, max; |
| 59 | + static long[] sum; |
| 60 | + |
| 61 | + public static void main(String[] args) throws Exception { |
| 62 | + |
| 63 | + io = new IOController(); |
| 64 | + |
| 65 | + preprocess(1_000_000); |
| 66 | + |
| 67 | + N = io.nextInt(); |
| 68 | + Q = io.nextInt(); |
| 69 | + max = new int[N*4]; |
| 70 | + sum = new long[N*4]; |
| 71 | + for(int i=1;i<=N;i++) { |
| 72 | + int a = io.nextInt(); |
| 73 | + pointUpdate1(1,N,i,a,1); |
| 74 | + pointUpdate2(1,N,i,g[a],1); |
| 75 | + } |
| 76 | + |
| 77 | + while(Q-->0) { |
| 78 | + int o = io.nextInt(); |
| 79 | + int s = io.nextInt(); |
| 80 | + int e = io.nextInt(); |
| 81 | + if(o == 1) rangeUpdate(1,N,s,e,1); |
| 82 | + else io.write(find(1,N,s,e,1) + "\n"); |
| 83 | + } |
| 84 | + |
| 85 | + io.close(); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + private static void preprocess(int limit) { |
| 90 | + sieve = new int[limit+1]; |
| 91 | + for(int i=2;i*i<=limit;i++) if(sieve[i] == 0) for(int j=i*i;j<=limit;j+=i) if(sieve[j] == 0) sieve[j] = i; |
| 92 | + |
| 93 | + f = new int[limit+1]; |
| 94 | + g = new int[limit+1]; |
| 95 | + f[1] = 1; |
| 96 | + f[2] = 2; |
| 97 | + for(int i=3;i<=limit;i++) { |
| 98 | + int t = i, prev = 0, cnt = 0; |
| 99 | + f[i] = 1; |
| 100 | + while(sieve[t] != 0) { |
| 101 | + if(prev != sieve[t]) { |
| 102 | + f[i] *= (cnt+1); |
| 103 | + cnt = 0; |
| 104 | + prev = sieve[t]; |
| 105 | + } |
| 106 | + cnt++; |
| 107 | + t /= sieve[t]; |
| 108 | + } |
| 109 | + if(t == 1) f[i] *= (cnt+1); |
| 110 | + else { |
| 111 | + if(prev == t) f[i] *= (cnt+2); |
| 112 | + else f[i] *= (cnt+1) * 2; |
| 113 | + } |
| 114 | + g[i] = g[f[i]] + 1; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static void pointUpdate1(int s, int e, int i, int v, int n) { |
| 119 | + if(s == e) { |
| 120 | + sum[n] = v; |
| 121 | + return; |
| 122 | + } |
| 123 | + int m = (s+e)>>1; |
| 124 | + if(i <= m) pointUpdate1(s,m,i,v,n*2); |
| 125 | + else pointUpdate1(m+1,e,i,v,n*2+1); |
| 126 | + sum[n] = sum[n*2] + sum[n*2+1]; |
| 127 | + } |
| 128 | + |
| 129 | + private static void pointUpdate2(int s, int e, int i, int v, int n) { |
| 130 | + if(s == e) { |
| 131 | + max[n] = v; |
| 132 | + return; |
| 133 | + } |
| 134 | + int m = (s+e)>>1; |
| 135 | + if(i <= m) pointUpdate2(s,m,i,v,n*2); |
| 136 | + else pointUpdate2(m+1,e,i,v,n*2+1); |
| 137 | + max[n] = Math.max(max[n*2], max[n*2+1]); |
| 138 | + } |
| 139 | + |
| 140 | + private static void rangeUpdate(int s, int e, int l, int r, int n) { |
| 141 | + if(l>r || l>e || r<s) return; |
| 142 | + if(max[n] == 0) return; |
| 143 | + if(s == e) { |
| 144 | + sum[n] = f[(int)sum[n]]; |
| 145 | + max[n] = g[(int)sum[n]]; |
| 146 | + return; |
| 147 | + } |
| 148 | + int m = (s+e)>>1; |
| 149 | + if(max[n*2] > 0) rangeUpdate(s,m,l,r,n*2); |
| 150 | + if(max[n*2+1] > 0) rangeUpdate(m+1,e,l,r,n*2+1); |
| 151 | + sum[n] = sum[n*2] + sum[n*2+1]; |
| 152 | + max[n] = Math.max(max[n*2], max[n*2+1]); |
| 153 | + } |
| 154 | + |
| 155 | + private static long find(int s, int e, int l, int r, int n) { |
| 156 | + if(l>r || l>e || r<s) return 0; |
| 157 | + if(l<=s && e<=r) return sum[n]; |
| 158 | + int m = (s+e)>>1; |
| 159 | + return find(s,m,l,r,n*2) + find(m+1,e,l,r,n*2+1); |
| 160 | + } |
| 161 | + |
| 162 | +} |
| 163 | +``` |
0 commit comments