codeforces 633G Yash And Trees

http://www.elijahqi.win/archives/3135
题目描述

Yash loves playing with trees and gets especially excited when they have something to do with prime numbers. On his 20th birthday he was granted with a rooted tree of

n
n nodes to answer queries on. Hearing of prime numbers on trees, Yash gets too intoxicated with excitement and asks you to help out and answer queries on trees for him. Tree is rooted at node

1
1 . Each node

i
i has some value

a_{i}
ai​ associated with it. Also, integer

m
m is given.

There are queries of two types:

for given node
v

v and integer value

x

x , increase all

a_{i}

ai​ in the subtree of node

v

v by value

x

x

for given node
v

v , find the number of prime numbers

p

p less than

m

m , for which there exists a node

u

u in the subtree of

v

v and a non-negative integer value

k

k , such that

a_{u}=p+m·k

au​=p+m⋅k .

输入输出格式

输入格式:
The first of the input contains two integers

n
n and

m
m (

1<=n<=100000,1<=m<=1000
1<=n<=100000,1<=m<=1000 ) — the number of nodes in the tree and value

m
m from the problem statement, respectively.

The second line consists of

n
n integers

a_{i}
ai​ (

0<=a_{i}<=10^{9}
0<=ai​<=109 ) — initial values of the nodes.

Then follow

n-1
n−1 lines that describe the tree. Each of them contains two integers

u_{i}
ui​ and

v_{i}
vi​ (

1<=u_{i},v_{i}<=n
1<=ui​,vi​<=n ) — indices of nodes connected by the

i
i -th edge.

Next line contains a single integer

q
q (

1<=q<=100000
1<=q<=100000 ) — the number of queries to proceed.

Each of the last

q
q lines is either 1 v x or 2 v (

1<=v<=n,0<=x<=10^{9}
1<=v<=n,0<=x<=109 ), giving the query of the first or the second type, respectively. It’s guaranteed that there will be at least one query of the second type.

输出格式:
For each of the queries of the second type print the number of suitable prime numbers.

输入输出样例

输入样例#1: 复制

8 20
3 7 9 8 4 11 7 3
1 2
1 3
3 4
4 5
4 6
4 7
5 8
4
2 1
1 1 1
2 5
2 4
输出样例#1: 复制

3
1
1
输入样例#2: 复制

5 10
8 7 5 1 0
1 2
2 3
1 5
2 4
3
1 1 0
1 1 2
2 2
输出样例#2: 复制

2
题意:每次给一个子树加一个值 然后每次询问一个子树内比m小的质数的不相同的个数

考虑首先做出dfs序 映射到线段树上 子树操作变成区间操作 每个节点维护一个1000的bitset表示这个节点的1000个数都有谁出现了 那么每次修改的时候其实相当于是将整个bitset做了一个循环操作 那么显然通过左移右移即可完成操作 然后每次查询的时候全局维护一个bitset然后记录答案即可 注意判断质数需要小于m

#include<bitset>
#include<cstdio>
#include<cctype>
#define lc (x<<1)
#define rc (x<<1|1)
#include<algorithm>
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
    return x*f;
}
const int N=1e5+10;
struct node{
    bitset<1000> bt;int tag;
}tree[N<<2];
bitset<1000>ans;bool not_prime[N];
int a[N],in[N],out[N],prime[200],tot,n,m,Q,h[N],dfn[N],num;
inline void update(int x){tree[x].bt=tree[lc].bt|tree[rc].bt;}
struct node1{
    int y,next;
}data[N<<1];
inline void doc(int x,int v){
    tree[x].tag+=v;tree[x].tag%=m;tree[x].bt=(tree[x].bt<<v)|(tree[x].bt>>m-v);
}
inline void pushdown(int x){
    if (!tree[x].tag) return;static int tag;
    tag=tree[x].tag;tree[x].tag=0;
    doc(lc,tag);doc(rc,tag);
}
inline void build(int x,int l,int r){
    if (l==r){tree[x].bt[a[dfn[l]]]=1;return;}int mid=l+r>>1;
    build(lc,l,mid);build(rc,mid+1,r);update(x);
}
inline void modify(int x,int l,int r,int l1,int r1,int v){
    if (l1<=l&&r1>=r){doc(x,v);return;}
    int mid=l+r>>1;pushdown(x);
    if (l1<=mid) modify(lc,l,mid,l1,r1,v);
    if (r1>mid) modify(rc,mid+1,r,l1,r1,v);update(x); 
}
inline void query(int x,int l,int r,int l1,int r1){
    if (l1<=l&&r1>=r){ans|=tree[x].bt;return;}int mid=l+r>>1;pushdown(x);
    if (l1<=mid) query(lc,l,mid,l1,r1);if (r1>mid) query(rc,mid+1,r,l1,r1);
}
inline void dfs(int x,int fa){
    in[x]=++num;dfn[num]=x;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (y==fa) continue;dfs(y,x);
    }out[x]=num;
}
int main(){
    freopen("cf633g.in","r",stdin);
    n=read();m=read();
    for (int i=2;i<=1e3;++i){
        if (!not_prime[i]) prime[++tot]=i;
        for (int j=1;prime[j]*i<=1e3;++j){
            not_prime[prime[j]*i]=1;
            if (i%prime[j]==0) break;
        }
    }
    for (int i=1;i<=n;++i) a[i]=read()%m;
    for (int i=1;i<n;++i){
        int x=read(),y=read();
        data[++num].y=y;data[num].next=h[x];h[x]=num;
        data[++num].y=x;data[num].next=h[y];h[y]=num;
    }num=0;dfs(1,1);build(1,1,n);Q=read();
    while(Q--){
        static int op,x,v;op=read();
        if (op==1){
            x=read();v=read()%m;modify(1,1,n,in[x],out[x],v);continue;
        }x=read();ans&=0;
        query(1,1,n,in[x],out[x]);int ans1=0;
        for (int i=1;i<=168&&prime[i]<m;++i) 
        if (ans[prime[i]]&1) ++ans1;
        printf("%d\n",ans1);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值