ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I

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的元素中一共有多少素数

分析:显然的线段树裸题

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

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <cmath>
  5 #include <ctime>
  6 #include <iostream>
  7 #include <algorithm>
  8 #include <map>
  9 #include <set>
 10 #include <vector>
 11 #include <deque>
 12 #include <queue>
 13 using namespace std;
 14 typedef long long LL;
 15 typedef double DB;
 16 #define Rep(i, n) for(int i = (0); i < (n); i++)
 17 #define Repn(i, n) for(int i = (n)-1; i >= 0; i--)
 18 #define For(i, s, t) for(int i = (s); i <= (t); i++)
 19 #define Ford(i, t, s) for(int i = (t); i >= (s); i--)
 20 #define rep(i, s, t) for(int i = (s); i < (t); i++)
 21 #define repn(i, s, t) for(int i = (s)-1; i >= (t); i--)
 22 #define MIT (2147483647)
 23 #define MLL (1000000000000000000LL)
 24 #define INF (1000000001)
 25 #define mk make_pair
 26 #define ft first
 27 #define sd second
 28 #define clr(x, y) (memset(x, y, sizeof(x)))
 29 #define sqr(x) ((x)*(x))
 30 #define sz(x) ((int) (x).size())
 31 #define puf push_front
 32 #define pub push_back
 33 #define pof pop_front
 34 #define pob pop_back
 35 inline void SetIO(string Name) {
 36     string Input = Name+".in", Output = Name+".out";
 37     freopen(Input.c_str(), "r", stdin);
 38     freopen(Output.c_str(), "w", stdout);
 39 }
 40 
 41 const int N = 100010, M = 18, Max = 10000010;
 42 struct SegTree {
 43     int Tot, Tag, Child[2];
 44     #define Tot(x) (Tr[x].Tot)
 45     #define Tag(x) (Tr[x].Tag)
 46     #define Lc(x) (Tr[x].Child[0])
 47     #define Rc(x) (Tr[x].Child[1])
 48     #define Child(x, y) (Tr[x].Child[y])
 49 } Tr[N*M];
 50 int CTr;
 51 int Prime[Max], CPrime;
 52 bool NotPrime[Max];
 53 int n, m, Arr[N];
 54 
 55 inline void GetPrime() {
 56     CPrime = 0;
 57     For(i, 2, Max-1) {
 58         if(!NotPrime[i]) Prime[++CPrime] = i;
 59         For(j, 1, CPrime) {
 60             if(1LL*i*Prime[j] >= Max) break;
 61             NotPrime[i*Prime[j]] = 1;
 62             if(!(i%Prime[j])) break;
 63         }
 64     }
 65 }
 66 
 67 inline int Getint() {
 68     int Ret = 0;
 69     char Ch = ' ';
 70     while(!(Ch >= '0' && Ch <= '9')) Ch = getchar();
 71     while(Ch >= '0' && Ch <= '9') {
 72         Ret = Ret*10+Ch-'0';
 73         Ch = getchar();
 74     }
 75     return Ret;
 76 }
 77 
 78 inline void Solve();
 79 
 80 inline void Input() {
 81     GetPrime();
 82     int TestNumber;
 83     //scanf("%d", &TestNumber);
 84     TestNumber = Getint();
 85     while(TestNumber--) {
 86         //scanf("%d%d", &n, &m);
 87         n = Getint();
 88         m = Getint();
 89         For(i, 1, n) scanf("%d", Arr+i);
 90         Solve();
 91     }
 92 }
 93 
 94 inline void Init() {
 95     CTr = 0;
 96 }
 97 
 98 inline void Updata(int x) {
 99     Tot(x) = 0;
100     Rep(i, 2)
101         Tot(x) += Tot(Child(x, i));
102 }
103 
104 inline void Draw(int x, int Left, int Right, int a) {
105     if(Left == Right) {
106         Arr[Left] = a;
107         Tot(x) = !NotPrime[a];
108     } else {
109         Tag(x) = a;
110         if(NotPrime[a]) Tot(x) = 0;
111         else Tot(x) = Right-Left+1;
112     }
113 }
114 
115 inline void PushDown(int x, int L, int R) {
116     if(!Tag(x)) return;
117     int Mid = (L+R)>>1;
118     Draw(Lc(x), L, Mid, Tag(x));
119     Draw(Rc(x), Mid+1, R, Tag(x));
120     Tag(x) = 0;
121 }
122 
123 inline void Build(int Left, int Right) {
124     int Mid = (Left+Right)>>1;
125     int x = ++CTr;
126     clr(Tr[x].Child, 0), Tot(x) = Tag(x) = 0;
127     if(Left == Right) Tot(x) = !NotPrime[Arr[Left]];
128     else {
129         Lc(x) = CTr+1;
130         Build(Left, Mid);
131         Rc(x) = CTr+1;
132         Build(Mid+1, Right);
133         Updata(x);
134     }
135 }
136 
137 inline void Add(int x, int Left, int Right, int v, int a) {
138     int Mid = (Left+Right)>>1;
139     if(Left == Right) {
140         Arr[v] += a;
141         Tot(x) = !NotPrime[Arr[v]];
142     } else {
143         if(Tag(x)) PushDown(x, Left, Right);
144         
145         if(v <= Mid) Add(Lc(x), Left, Mid, v, a);
146         else Add(Rc(x), Mid+1, Right, v, a);
147         Updata(x);
148     }
149 }
150 
151 inline int Query(int x, int Left, int Right, int L, int R) {
152     if(Left >= L && Right <= R) return Tot(x);
153     else {
154         int Mid = (Left+Right)>>1, Ret = 0;
155         
156         if(Tag(x)) PushDown(x, Left, Right);
157         
158         if(R <= Mid) Ret = Query(Lc(x), Left, Mid, L, R);
159         else if(L > Mid) Ret = Query(Rc(x), Mid+1, Right, L, R);
160         else {
161             Ret = Query(Lc(x), Left, Mid, L, Mid);
162             Ret += Query(Rc(x), Mid+1, Right, Mid+1, R);
163         }
164         return Ret;
165     }
166 }
167 
168 inline void Change(int x, int Left, int Right, int L, int R, int a) {
169     if(Left >= L && Right <= R) Draw(x, Left, Right, a);
170     else {
171         int Mid = (Left+Right)>>1;
172         
173         if(Tag(x)) PushDown(x, Left, Right);
174         
175         if(R <= Mid) Change(Lc(x), Left, Mid, L, R, a);
176         else if(L > Mid) Change(Rc(x), Mid+1, Right, L, R, a);
177         else {
178             Change(Lc(x), Left, Mid, L, Mid, a);
179             Change(Rc(x), Mid+1, Right, Mid+1, R, a);
180         }
181         Updata(x);
182     }
183 }
184 
185 inline void Solve() {
186     Init();
187     Build(1, n);
188     
189     char Opt;
190     int L, R, v, a, Ans;
191     while(m--) {
192         for(Opt = ' '; Opt != 'A' && Opt != 'Q' && Opt != 'R'; Opt = getchar());
193         
194         if(Opt == 'A') {
195             a = Getint();
196             v = Getint();
197             Add(1, 1, n, v, a);
198         } else if(Opt == 'Q') {
199             L = Getint();
200             R = Getint();
201             Ans = Query(1, 1, n, L, R);
202             printf("%d\n", Ans);
203         } else {
204             a = Getint();
205             L = Getint();
206             R = Getint();
207             Change(1, 1, n, L, R, a);
208         }
209     }
210 }
211 
212 int main() {
213      Input();
214      //Solve();
215     return 0;
216 }
View Code

 

转载于:https://www.cnblogs.com/StupidBoy/p/4886220.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值