zoj 3911 Prime Query (ZOJ Monthly, October 2015 - I)
来源:互联网 发布:sqlserver日常维护 编辑:程序博客网 时间:2024/06/11 07:40
题目链接~~~
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
15 101 2 3 4 5A 3 1 Q 1 3R 5 2 4A 1 1Q 1 1Q 1 2Q 1 4A 3 5Q 5 5Q 1 5
Sample Output
212404
很久没写线段树了,结果写了一发一直wa原因是pushdown()的root没赋0 附代码
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;const int N = 10000010;bool isprime[N+5];void init(){ isprime[0] = isprime[1] = 1; for(long long i=2; i<N; i++) if(!isprime[i]) for(long long j=i*i; j<N; j+=i) isprime[j]=1; for(int i = 0 ;i<N;i++) isprime[i] = !isprime[i];// printf("%d\n",isprime[1]);}const int maxn = 100000+10;int sum[maxn<<2];int lazy[maxn<<2];int ad_[maxn<<2];int num[maxn<<2];int x[maxn];#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1void pushup(int rt) { sum[rt] = sum[rt<<1]+sum[rt<<1|1];}void build(int l,int r,int rt) { if(l==r) { num[rt] = x[l]; if(isprime[x[l]]) sum[rt] = 1; else sum[rt] = 0; return ; } int mid = (l+r)/2; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); pushup(rt);}void pushdown(int rt,int m) { if(num[rt]) { if(isprime[num[rt]]) { sum[rt<<1] = (m-m/2); sum[rt<<1|1] = m/2; } else { sum[rt<<1] = sum[rt<<1|1] = 0; } num[rt<<1] = num[rt<<1|1] = num[rt]; num[rt] = 0; }}void update(int l,int r,int rt,int L,int R,int val) { if(l>=L&&r<=R) { if(isprime[val]) sum[rt] = r+1-l; else sum[rt] = 0; num[rt] = val; return ; } int mid = (l+r)/2; pushdown(rt,r+1-l); if(L<=mid) { update(lson,L,R,val); } if(R>mid) { update(rson,L,R,val); } pushup(rt);}void add(int l,int r,int rt,int xx,int val) { if(l==r) { num[rt] += val; if(isprime[num[rt]]) { sum[rt] = 1; } else sum[rt] = 0; return ; } pushdown(rt,r+1-l); int mid = (l+r)/2; if(mid<xx) add(rson,xx,val); else add(lson,xx,val); pushup(rt);}int query(int l,int r,int rt,int L,int R) { if (L <= l && r <= R) { return sum[rt]; } pushdown(rt , r - l + 1); int mid = (l + r) >> 1; int ret = 0; if (L <= mid) ret += query(lson,L , R ); if (mid < R) ret += query(rson,L , R ); return ret;}void print(int l,int r,int rt) { if(l==r) { printf("%d %d\n",l,num[rt]); return ; } int mid = (l+r)/2; print(l,mid,rt<<1); print(mid+1,r,rt<<1|1);}int main(){// freopen("in.txt","r",stdin); memset(isprime,false,sizeof(isprime)); init(); int T; scanf("%d",&T); while(T--) { int n,m; scanf("%d%d",&n,&m); for(int i = 1;i<=n;i++) { scanf("%d",&x[i]); } memset(sum,0,sizeof(sum)); memset(num,0,sizeof(num)); build(1,n,1); while(m--) { char ss[5];int x,y,z; scanf("%s",ss); if(ss[0]== 'A') { scanf("%d%d",&z,&x); add(1,n,1,x,z); } else if(ss[0]=='Q') { scanf("%d %d",&x,&y); printf("%d\n",query(1,n,1,x,y)); } else { scanf("%d%d %d",&z,&x,&y); update(1,n,1,x,y,z); }// print(1,n,1); } }return 0;}
0 0
- zoj 3911 Prime Query (ZOJ Monthly, October 2015 - I)
- Prime Query(ZOJ Monthly, October 2015 - I)
- ZOJ 4536 ZOJ Monthly, October 2011 I
- 143 - ZOJ Monthly, October 2015
- ZOJ 补题 3903 && 3905 && 3911 (ZOJ Monthly, October 2015)
- 143 - ZOJ Monthly, October 2015 - A(zoj3903)
- 143 - ZOJ Monthly, October 2015 - Solution[part]
- ZOJ 3911 Prime Query
- ZOJ 3911Prime Query
- ZOJ 3911 Prime Query
- Prime Query【ZOJ--3911】
- ZOJ Monthly, October 2010 ABEFI
- ZOJ 4528 ZOJ Monthly, October 2011 A
- ZOJ 4535 ZOJ Monthly, October 2011 H
- ZOJ Monthly, October 2011 (DP+数学专场!!!)
- ZOJ 3913 Bob wants to pour water —— ZOJ Monthly, October 2015
- ZOJ 3911 Prime Query(线段树区间操作)
- Prime Query (ZOJ 3911 线段树)
- android 设置手机状态栏的背景颜色
- 常用MySQL操作
- forward 和redirect的区别
- Cursor工具类,用于支持根据列名取得对应的值
- Android Binder机制(超级详尽)
- zoj 3911 Prime Query (ZOJ Monthly, October 2015 - I)
- Java 的序列化 (Serialization) 教程
- 得到当前日期是星期几
- ASP.NET 5简介
- ser2net安装使用说明
- Mongod 芒果数据库初学.
- Ldom can't boot from vdisk ( Can't open disk label package )
- int 类型的自增长ID生成类,支持多线程。
- MySQL- SHOW TABLE STATUS命令