Prime Query


Time Limit: 1 Second      Memory Limit: 196608 KB

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

题意:维护一个长度为n的序列,有三种操作

A v u 给第u个点增加v的权值

R a l r 把第l到r的元素的权值全部改成a

Q l r 询问第l到r的元素中一共有多少素数

分析:显然的线段树裸题

先线性筛素数,然后维护一下就行

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <deque>
#include <queue>
using namespace std;
typedef long long LL;
typedef double DB;
#define Rep(i, n) for(int i = (0); i < (n); i++)
#define Repn(i, n) for(int i = (n)-1; i >= 0; i--)
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, t, s) for(int i = (t); i >= (s); i--)
#define rep(i, s, t) for(int i = (s); i < (t); i++)
#define repn(i, s, t) for(int i = (s)-1; i >= (t); i--)
#define MIT (2147483647)
#define MLL (1000000000000000000LL)
#define INF (1000000001)
#define mk make_pair
#define ft first
#define sd second
#define clr(x, y) (memset(x, y, sizeof(x)))
#define sqr(x) ((x)*(x))
#define sz(x) ((int) (x).size())
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
inline void SetIO(string Name) {
string Input = Name+".in", Output = Name+".out";
freopen(Input.c_str(), "r", stdin);
freopen(Output.c_str(), "w", stdout);
} const int N = , M = , Max = ;
struct SegTree {
int Tot, Tag, Child[];
#define Tot(x) (Tr[x].Tot)
#define Tag(x) (Tr[x].Tag)
#define Lc(x) (Tr[x].Child[0])
#define Rc(x) (Tr[x].Child[1])
#define Child(x, y) (Tr[x].Child[y])
} Tr[N*M];
int CTr;
int Prime[Max], CPrime;
bool NotPrime[Max];
int n, m, Arr[N]; inline void GetPrime() {
CPrime = ;
For(i, , Max-) {
if(!NotPrime[i]) Prime[++CPrime] = i;
For(j, , CPrime) {
if(1LL*i*Prime[j] >= Max) break;
NotPrime[i*Prime[j]] = ;
if(!(i%Prime[j])) break;
}
}
} inline int Getint() {
int Ret = ;
char Ch = ' ';
while(!(Ch >= '' && Ch <= '')) Ch = getchar();
while(Ch >= '' && Ch <= '') {
Ret = Ret*+Ch-'';
Ch = getchar();
}
return Ret;
} inline void Solve(); inline void Input() {
GetPrime();
int TestNumber;
//scanf("%d", &TestNumber);
TestNumber = Getint();
while(TestNumber--) {
//scanf("%d%d", &n, &m);
n = Getint();
m = Getint();
For(i, , n) scanf("%d", Arr+i);
Solve();
}
} inline void Init() {
CTr = ;
} inline void Updata(int x) {
Tot(x) = ;
Rep(i, )
Tot(x) += Tot(Child(x, i));
} inline void Draw(int x, int Left, int Right, int a) {
if(Left == Right) {
Arr[Left] = a;
Tot(x) = !NotPrime[a];
} else {
Tag(x) = a;
if(NotPrime[a]) Tot(x) = ;
else Tot(x) = Right-Left+;
}
} inline void PushDown(int x, int L, int R) {
if(!Tag(x)) return;
int Mid = (L+R)>>;
Draw(Lc(x), L, Mid, Tag(x));
Draw(Rc(x), Mid+, R, Tag(x));
Tag(x) = ;
} inline void Build(int Left, int Right) {
int Mid = (Left+Right)>>;
int x = ++CTr;
clr(Tr[x].Child, ), Tot(x) = Tag(x) = ;
if(Left == Right) Tot(x) = !NotPrime[Arr[Left]];
else {
Lc(x) = CTr+;
Build(Left, Mid);
Rc(x) = CTr+;
Build(Mid+, Right);
Updata(x);
}
} inline void Add(int x, int Left, int Right, int v, int a) {
int Mid = (Left+Right)>>;
if(Left == Right) {
Arr[v] += a;
Tot(x) = !NotPrime[Arr[v]];
} else {
if(Tag(x)) PushDown(x, Left, Right); if(v <= Mid) Add(Lc(x), Left, Mid, v, a);
else Add(Rc(x), Mid+, Right, v, a);
Updata(x);
}
} inline int Query(int x, int Left, int Right, int L, int R) {
if(Left >= L && Right <= R) return Tot(x);
else {
int Mid = (Left+Right)>>, Ret = ; if(Tag(x)) PushDown(x, Left, Right); if(R <= Mid) Ret = Query(Lc(x), Left, Mid, L, R);
else if(L > Mid) Ret = Query(Rc(x), Mid+, Right, L, R);
else {
Ret = Query(Lc(x), Left, Mid, L, Mid);
Ret += Query(Rc(x), Mid+, Right, Mid+, R);
}
return Ret;
}
} inline void Change(int x, int Left, int Right, int L, int R, int a) {
if(Left >= L && Right <= R) Draw(x, Left, Right, a);
else {
int Mid = (Left+Right)>>; if(Tag(x)) PushDown(x, Left, Right); if(R <= Mid) Change(Lc(x), Left, Mid, L, R, a);
else if(L > Mid) Change(Rc(x), Mid+, Right, L, R, a);
else {
Change(Lc(x), Left, Mid, L, Mid, a);
Change(Rc(x), Mid+, Right, Mid+, R, a);
}
Updata(x);
}
} inline void Solve() {
Init();
Build(, n); char Opt;
int L, R, v, a, Ans;
while(m--) {
for(Opt = ' '; Opt != 'A' && Opt != 'Q' && Opt != 'R'; Opt = getchar()); if(Opt == 'A') {
a = Getint();
v = Getint();
Add(, , n, v, a);
} else if(Opt == 'Q') {
L = Getint();
R = Getint();
Ans = Query(, , n, L, R);
printf("%d\n", Ans);
} else {
a = Getint();
L = Getint();
R = Getint();
Change(, , n, L, R, a);
}
}
} int main() {
Input();
//Solve();
return ;
}

ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I的更多相关文章

  1. ZOJ 3911 Prime Query(线段树)

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  2. 143 - ZOJ Monthly, October 2015 I Prime Query 线段树

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  3. Prime Query (ZOJ 3911 线段树)

    Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a sequen ...

  4. ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  5. ZOJ 2015 10月份 月赛 3911 Prime Query

    这道题我改啊,改啊,交啊,就对了. #include <stdio.h> #include <stdlib.h> #include <math.h> #includ ...

  6. ZOJ 3913 Bob wants to pour water ZOJ Monthly, October 2015 - H

    Bob wants to pour water Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge There i ...

  7. ZOJ 3910 Market ZOJ Monthly, October 2015 - H

    Market Time Limit: 2 Seconds      Memory Limit: 65536 KB There's a fruit market in Byteland. The sal ...

  8. ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F

    Number Game Time Limit: 2 Seconds      Memory Limit: 65536 KB The bored Bob is playing a number game ...

  9. ZOJ 3905 Cake ZOJ Monthly, October 2015 - C

    Cake Time Limit: 4 Seconds      Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...

随机推荐

  1. Delphi开发Windows服务程序

    开发步骤: 1.New->Other->Service Application 2.现在一个服务程序的框架已经搭起来了 打开Service1窗口,有几个属性说明一下: AllowPause ...

  2. 45. Singleton类的C++/C#实现[Singleton]

    [题目] 设计一个类,我们只能生成该类的一个实例. [分析] 单例模式的意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点.让类自身负责保存它的唯一实例.这个类可以保证没有其他实例可.以被创建 ...

  3. hdu5832 A water problem

    A water problem Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  4. MST:Roadblocks(POJ 3255)

       路上的石头 题目大意:某个街区有R条路,N个路口,道路双向,问你从开始(1)到N路口的次短路经长度,同一条边可以经过多次. 这一题相当有意思,现在不是要你找最短路径,而是要你找次短路经,而且次短 ...

  5. ORACLE查询某一字段重复的数据

    第一种方法: select a.*  from ASSET_MAINTAIN a inner join ASSET_MAINTAIN b on a.asset_id=b.asset_id and a. ...

  6. 【linux】awk的使用

    教程来自:http://www.runoob.com/linux/linux-comm-awk.html 教程中的例子很好,可以有助于快速上手awk.但是里面的细节介绍的并不清楚. 问题1:什么时候写 ...

  7. XML Parser Error on line 1: 前言中不允许有内容, Mybatis 生成代码

    使用用notepad++打开xml文件,然后在菜单“格式”中选择“以UTF-8无BOM格式编码”,保存.

  8. C++拷贝构造函数(深拷贝,浅拷贝)

    http://www.cnblogs.com/BlueTzar/articles/1223313.html 对于普通类型的对象来说,它们之间的复制是很简单的,例如:int a=88;int b=a;  ...

  9. linux rdsktop 运程管理 windows

    [root@ok ISO]# yum list |grep rdesktop rdesktop.x86_64 1.7.1-1.el6 base [root@ok ISO]# yum install r ...

  10. [转]ASP.NET Web.Config 读写辅助类

    using System; using System.Configuration; using System.Web; using System.Web.Configuration; namespac ...