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

21240

4

很久没写线段树了,结果写了一发一直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
原创粉丝点击