1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include <map> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define lson l, mid, ls #define rson mid, r, rs #define ls ((rt << 1) + 1) #define rs ((rt << 1) + 2) using namespace std; const int MAXN = 100010;
struct Node{ int val, rep, sum; };
int data[MAXN]; Node tre[MAXN << 2];
int pcnt; int prime[1000100]; bool isPrime[10000100];
void getPrime(){ pcnt = 0; fill(isPrime, isPrime + 10000100, true); isPrime[0] = isPrime[1] = false; for(int i = 2; i < 10000100; i++){ if(isPrime[i]) prime[pcnt++] = i; for(int j = 0; j < pcnt && i * prime[j] < 10000100; j++){ isPrime[i * prime[j]] = false; if(i % prime[j] == 0) break; } } }
void pushUp(int rt){ tre[rt].sum = tre[ls].sum + tre[rs].sum; }
void pushDown(int l, int r, int rt){ if(tre[rt].rep){ tre[ls].rep = tre[rs].rep = tre[rt].rep; int mid = (l + r) >> 1; if(isPrime[tre[rt].rep]){ tre[ls].sum = mid - l; tre[rs].sum = r - mid; }else tre[ls].sum = tre[rs].sum = 0; tre[rt].rep = 0; } }
void buildTree(int l, int r, int rt){ tre[rt].rep = 0; if(r - l == 1){ tre[rt].val = data[l]; if(isPrime[data[l]]) tre[rt].sum = 1; else tre[rt].sum = 0; return; }
int mid = (l + r) >> 1; buildTree(lson); buildTree(rson);
pushUp(rt); }
void update(int L, int R, int l, int r, int rt, int val){ if(L == l && R == r){ tre[rt].rep = val; if(isPrime[val]) tre[rt].sum = r - l; else tre[rt].sum = 0; return; }
pushDown(l, r, rt);
int mid = (l + r) >> 1; if(R <= mid) update(L, R, lson, val); else if(L >= mid) update(L, R, rson, val); else{ update(L, mid, lson, val); update(mid, R, rson, val); }
pushUp(rt); }
void update1(int L, int R, int l, int r, int rt, int val){ if(L == l && R == r){ if(tre[rt].rep) tre[rt].val = tre[rt].rep; tre[rt].rep = 0; tre[rt].val += val; data[l] = tre[rt].val; if(isPrime[tre[rt].val]) tre[rt].sum = 1; else tre[rt].sum = 0; return; }
pushDown(l, r, rt);
int mid = (l + r) >> 1; if(R <= mid) update1(L, R, lson, val); else if(L >= mid) update1(L, R, rson, val); else{ update1(L, mid, lson, val); update1(mid, R, rson, val); }
pushUp(rt); }
int query(int L, int R, int l, int r, int rt){ if(L == l && R == r) return tre[rt].sum;
pushDown(l, r, rt);
int mid = (l + r) >> 1; if(R <= mid) return query(L, R, lson); else if(L >= mid) return query(L, R, rson); else return query(L, mid, lson) + query(mid, R, rson); }
int main(){ int t; scanf("%d", &t); getPrime(); while(t--){ int n, q; scanf("%d%d", &n, &q); for(int i = 0; i < n; i++) scanf("%d", &data[i]); buildTree(0, n, 0); while(q--){ char op; scanf(" %c", &op); if(op == 'A'){ int v, p; scanf("%d%d", &v, &p); update1(p - 1, p, 0, n, 0, v); }else if(op == 'Q'){ int l, r; scanf("%d%d", &l, &r); printf("%d\n", query(l - 1, r, 0, n, 0)); }else { int v, l, r; scanf("%d%d%d", &v, &l, &r); update(l - 1, r, 0, n, 0, v); } } } return 0; }
|