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 0 4
Author: HUA, Yiwei
Source: ZOJ Monthly, October 2015
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5638
题目大意:三个操作,将下标为l的加v,把l到r的换成a,然后查询l到r闭区间内素数的个数
题目分析:离线素数筛,然后区间更新,注意pushdown的时候要判断下就行了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
int const MAX = 1e5 + 5;
int const MAXM = 1e7 + 5;
int a[MAX << 2], num[MAX << 2], p[MAXM];
bool noprime[MAXM];
int n, q, pnum;
void get_prime()
{
noprime[0] = true;
noprime[1] = true;
pnum = 0;
for(int i = 2; i < MAXM; i++)
{
if(!noprime[i])
p[pnum ++] = i;
for(int j = 0; j < pnum && (long long)i * p[j] < MAXM; j++)
{
noprime[i * p[j]] = true;
if(i % p[j] == 0)
break;
}
}
}
void PushUp(int rt)
{
num[rt] = num[rt << 1] + num[rt << 1 | 1];
}
void PushDown(int ln, int rn, int rt)
{
if(a[rt])
{
if(!noprime[a[rt]])
{
num[rt << 1] = ln;
num[rt << 1 | 1] = rn;
}
else
num[rt << 1] = num[rt << 1 | 1] = 0;
a[rt << 1] = a[rt];
a[rt << 1 | 1] = a[rt];
a[rt] = 0;
}
return;
}
void Build(int l, int r, int rt)
{
a[rt] = 0;
if(l == r)
{
scanf("%d", &a[rt]);
if(!noprime[a[rt]])
num[rt] = 1;
return;
}
int mid = (l + r) >> 1;
Build(lson);
Build(rson);
PushUp(rt);
}
void Update1(int pos, int c, int l, int r, int rt)
{
if(l == r)
{
a[rt] += c;
num[rt] = (!noprime[a[rt]]) ? 1 : 0;
return;
}
int mid = (l + r) >> 1;
PushDown(mid - l + 1, r - mid, rt);
if(mid >= pos)
Update1(pos, c, lson);
else
Update1(pos, c, rson);
PushUp(rt);
}
void Update2(int L, int R, int c, int l, int r, int rt)
{
if(L <= l && r <= R)
{
a[rt] = c;
num[rt] = (!noprime[a[rt]]) ? (r - l + 1) : 0;
return;
}
int mid = (l + r) >> 1;
PushDown(mid - l + 1, r - mid, rt);
if(L <= mid)
Update2(L, R, c, lson);
if(mid < R)
Update2(L, R, c, rson);
PushUp(rt);
return;
}
int Query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R)
return num[rt];
int mid = (l + r) >> 1, ans = 0;
PushDown(mid - l + 1, r - mid, rt);
if(L <= mid)
ans += Query(L, R, lson);
if(mid < R)
ans += Query(L, R, rson);
return ans;
}
int main()
{
get_prime();
int T;
scanf("%d", &T);
while(T --)
{
memset(num, 0, sizeof(num));
scanf("%d %d", &n, &q);
Build(1, n, 1);
while(q --)
{
char s[2];
scanf("%s", s);
if(s[0] == 'A')
{
int v, l;
scanf("%d %d", &v, &l);
Update1(l, v, 1, n, 1);
}
else if(s[0] == 'R')
{
int v, l, r;
scanf("%d %d %d", &v, &l, &r);
Update2(l, r, v, 1, n, 1);
}
else
{
int l, r;
scanf("%d %d", &l, &r);
printf("%d\n", Query(l, r, 1, n, 1));
}
}
}
}