You are given a simple task. Given a sequence A[i] with N numbers. You have to perform Q operations on the given sequence.
Here are the operations:
- A v l, add the value v to element with index l.(1<=V<=1000)
- R a l r, replace all the elements of sequence with index i(l<=i<= r) with a(1<=a<=10^6) .
- Q l r, print the number of elements with index i(l<=i<=r) and A[i] is a prime number
Note that no number in sequence ever will exceed 10^7.
Input
The first line is a signer integer T which is the number of test cases.
For each test case, The first line contains two numbers N and Q (1 <= N, Q <= 100000) - the number of elements in sequence and the number of queries.
The second line contains N numbers - the elements of the sequence.
In next Q lines, each line contains an operation to be performed on the sequence.
Output
For each test case and each query,print the answer in one line.
Sample Input
1 5 10 1 2 3 4 5 A 3 1 Q 1 3 R 5 2 4 A 1 1 Q 1 1 Q 1 2 Q 1 4 A 3 5 Q 5 5 Q 1 5
Sample Output
2 1 2 4 04
线段树的区间更新,,第一次写,大神勿喷
#include <iostream> #include <cstring> #include <stdio.h> using namespace std; const int N = 500010; int num[N], sum[N]; const int maxn=10000010; bool notprim[maxn]; void init() { memset(notprim,false ,sizeof(notprim)); notprim[0]=notprim[1]=true; for(int i=2; i<maxn; i++) { if(!notprim[i]) { if(i>maxn/i)continue; for(int j=i*i; j<maxn; j+=i) notprim[j]=true; } } } void ini() { memset(num,0,sizeof(num)); memset(sum,0,sizeof(sum)); } void pushDown(int i, int l, int r) { if(num[i] != 0) { int mid = l + r >> 1; num[i << 1] = num[i]; num[i << 1 | 1] = num[i]; if(notprim[num[i]]==false) { sum[i << 1] = (mid - l + 1); sum[i << 1 | 1] = (r - mid); } else sum[i<<1]=sum[i<<1|1]=0; num[i] = 0; } } void update(int i, int l, int r, int ql, int qr, int val) { if(l > qr || ql > r) return ; if(l >= ql && r <= qr) { num[i] = val; if(notprim[val]==false) sum[i] = (r - l + 1); else sum[i]=0; return ; } pushDown(i, l, r); int mid = l + r >> 1; update(i << 1, l, mid, ql, qr, val); update(i << 1 | 1, mid + 1, r, ql, qr, val); sum[i] = sum[i << 1] + sum[i << 1 | 1]; } void addup(int i, int l, int r, int ql, int qr, int val) { if(l > qr || ql > r) return ; if(l >= ql && r <= qr) { num[i]+= val; if(notprim[num[i]]==false) sum[i] = 1; else sum[i]=0; return ; } pushDown(i, l, r); int mid = l + r >> 1; addup(i << 1, l, mid, ql, qr, val); addup(i << 1 | 1, mid + 1, r, ql, qr, val); sum[i] = sum[i << 1] + sum[i << 1 | 1]; } int query(int i, int l, int r, int ql, int qr) { if(l > qr || ql > r) return 0; if(l >= ql && r <= qr) return sum[i]; pushDown(i, l, r); int mid = l + r >> 1; return query(i << 1, l, mid, ql, qr) + query(i << 1 | 1, mid + 1, r, ql, qr); } int main() { int t; scanf("%d",&t); init(); while(t--) { ini(); int n,m; scanf("%d%d",&n,&m); int x; for(int i=0;i<n;i++) {scanf("%d",&x); addup(1,1,n,i+1,i+1,x); } char s[5]; int x1,x2,x3; for(int i=0;i<m;i++) { //cin>>s; scanf("%s",&s); if(s[0]=='A') { scanf("%d%d",&x1,&x2); addup(1,1,n,x2,x2,x1); } else if(s[0]=='Q') { scanf("%d%d",&x1,&x2); // cout<<query(1,1,n,x1,x2)<<endl; printf("%d\n",query(1,1,n,x1,x2)); } else if(s[0]=='R') { scanf("%d%d%d",&x1,&x2,&x3); update(1,1,n,x2,x3,x1); } } } return 0; }