标签:
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 using namespace std; 6 #define LL(x) (x<<1) 7 #define RR(x) (x<<1|1) 8 #define MID(a,b) (a+((b-a)>>1)) 9 const int N = 100010; 10 const int M = 10000010; 11 int p[N]; 12 int prime() 13 { 14 memset(p, true, sizeof(p)); 15 for(int i = 2; i <= sqrt(M +0.5); i++) 16 if(p[i]) 17 for(int j = i << 1 ; j <= M; j += i) 18 p[j] = false; 19 } 20 struct node 21 { 22 int left,right; 23 int sum,type; 24 int mid() 25 { 26 return MID(left,right); 27 } 28 void fun(int tmp)///其实并没有改变数组,只是在和上加了上去 29 { 30 type = tmp; 31 if(p[tmp]) 32 { 33 sum =(right-left + 1) * type; 34 } 35 } 36 }; 37 int x[N],y[N]; 38 struct Segtree 39 { 40 node tree[N*4]; 41 void build(int left,int right,int ind) 42 { 43 tree[ind].left = left; 44 tree[ind].right = right; 45 //tree[ind].sum = right-left+1; 46 tree[ind].sum = 0; 47 if(left==right) tree[ind].sum=y[left]; 48 else 49 { 50 int mid=tree[ind].mid(); 51 build(left,mid,LL(ind)); 52 build(mid+1,right,RR(ind)); 53 tree[ind].sum = tree[LL(ind)].sum+tree[RR(ind)].sum; 54 } 55 } 56 void relax(int ind) 57 { 58 if(tree[ind].type) 59 { 60 tree[LL(ind)].fun(tree[ind].type); 61 tree[RR(ind)].fun(tree[ind].type); 62 tree[ind].type = 0; 63 } 64 } 65 void updata(int st,int ed,int ind,int type)///区间更新 66 { 67 int left=tree[ind].left,right=tree[ind].right; 68 if(st <= left && right <= ed) 69 tree[ind].fun(type); 70 else 71 { 72 relax(ind); 73 int mid = tree[ind].mid(); 74 if(st<=mid) 75 updata(st,ed,LL(ind),type); 76 if(ed>mid) 77 updata(st,ed,RR(ind),type); 78 tree[ind].sum = tree[LL(ind)].sum + tree[RR(ind)].sum; 79 } 80 } 81 int query(int st,int ed,int ind)///区间求和 82 { 83 int left=tree[ind].left,right=tree[ind].right; 84 if(st<=left&&right<=ed) return tree[ind].sum; 85 else 86 { 87 relax(ind); 88 int mid=tree[ind].mid(); 89 int sum1=0,sum2=0; 90 if(st <= mid) sum1 = query(st,ed,LL(ind)); 91 if(ed > mid) sum2 = query(st,ed,RR(ind)); 92 return sum1+sum2; 93 } 94 } 95 } seg; 96 int main() 97 { 98 int x[N],y[N]; 99 int t,t_cnt=0; 100 prime(); 101 scanf("%d",&t); 102 while(t--) 103 { 104 int n,m,a,b,c; 105 scanf("%d%d",&n,&m); 106 for(int i = 1; i <= n; i++) 107 { 108 scanf("%d",&x[i]); 109 if(p[x[i]]) 110 y[i] = 1; 111 else 112 y[i] = 0; 113 } 114 seg.build(1,n,1); 115 char s[N]; 116 while(m--) 117 { 118 scanf("%s",s); 119 if(s[0] == ‘A‘)///单点增加 120 { 121 scanf("%d%d",&a,&b); 122 x[b] += a; 123 if(p[x[b]])///如果加上后是质数 124 seg.updata(b,b,1,1); 125 else 126 seg.updata(b,b,1,0); 127 } 128 else if(s[0] == ‘Q‘)///查询区间内质数个数 129 { 130 scanf("%d%d",&a,&b); 131 printf("%d\n",seg.query(a,b,1)); 132 } 133 else///区间替换 134 { 135 scanf("%d%d%d",&a,&b,&c); 136 seg.updata(a,b,1,c); 137 } 138 } 139 } 140 return 0; 141 }
标签:
原文地址:http://www.cnblogs.com/ACMERY/p/4869158.html