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
题意:三种操作,将下标为l的加v,把l到r的换成a和查询l到r闭区间内素数的个数
解题思路:线段树
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
bool vis[10000009];
int n, q, x, y, z, t;
int sum[100009 << 2], lazy[100009 << 2], a[100009];
char ch[5];
void init()
{
memset(vis, true, sizeof vis);
vis[0] = vis[1] = false;
for (int i = 2; i < 10000009; i++)
{
if (!vis[i]) continue;
for (int j = i * 2; j < 10000009; j += i) vis[j] = false;
}
}
void build(int k, int l, int r)
{
lazy[k] = -1;
if (l == r) { scanf("%d", &a[l]); lazy[k] = a[l], sum[k] = vis[a[l]]; return; }
int mid = (l + r) >> 1;
build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
sum[k] = sum[k << 1] + sum[k << 1 | 1];
}
void Push(int k, int l, int r)
{
lazy[k << 1] = lazy[k << 1 | 1] = lazy[k];
int mid = (l + r) >> 1;
sum[k << 1] = (mid - l + 1)*vis[lazy[k]], sum[k << 1 | 1] = (r - mid)*vis[lazy[k]];
lazy[k] = -1;
}
void update(int k, int l, int r, int ll, int rr, int val)
{
if (l >= ll&&r <= rr) { lazy[k] = val, sum[k] = (r - l + 1)*vis[val]; return; }
int mid = (l + r) >> 1;
if (lazy[k] != -1) Push(k, l, r);
if (ll <= mid) update(k << 1, l, mid, ll, rr, val);
if (rr > mid) update(k << 1 | 1, mid + 1, r, ll, rr, val);
sum[k] = sum[k << 1] + sum[k << 1 | 1];
}
void update(int k, int l, int r, int p, int val)
{
if (l == r) { lazy[k] += val; sum[k] = vis[lazy[k]]; return; }
int mid = (l + r) >> 1;
if (lazy[k] != -1) Push(k, l, r);
if (p <= mid) update(k << 1, l, mid, p, val);
else update(k << 1 | 1, mid + 1, r, p, val);
sum[k] = sum[k << 1] + sum[k << 1 | 1];
}
int query(int k, int l, int r, int ll, int rr)
{
if (l >= ll&&r <= rr) return sum[k];
int mid = (l + r) >> 1, ans = 0;
if (lazy[k] != -1) Push(k, l, r);
if (ll <= mid) ans += query(k << 1, l, mid, ll, rr);
if (rr > mid) ans += query(k << 1 | 1, mid + 1, r, ll, rr);
return ans;
}
int main()
{
init();
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &q);
build(1, 1, n);
while (q--)
{
scanf("%s%d%d", ch, &x, &y);
if (ch[0] == 'R')
{
scanf("%d", &z);
update(1, 1, n, y, z, x);
}
else if (ch[0] == 'A') update(1, 1, n, y, x);
else printf("%d\n", query(1, 1, n, x, y));
}
}
return 0;
}