Manthan, Codefest 16 G. Yash And Trees(dfs序,bitset,线段树,好题)

13 篇文章 0 订阅
8 篇文章 0 订阅

G. Yash And Trees
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 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. Each node i has some value aiassociated with it. Also, integer m is given.

There are queries of two types:

  1. for given node v and integer value x, increase all ai in the subtree of node v by value x
  2. for given node v, find the number of prime numbers p less than m, for which there exists a node u in the subtree of v and a non-negative integer value k, such that au = p + m·k.
Input

The first of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ 1000) — the number of nodes in the tree and value mfrom the problem statement, respectively.

The second line consists of n integers ai (0 ≤ ai ≤ 109) — initial values of the nodes.

Then follow n - 1 lines that describe the tree. Each of them contains two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of nodes connected by the i-th edge.

Next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of queries to proceed.

Each of the last q lines is either 1 v x or 2 v (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.

Output

For each of the queries of the second type print the number of suitable prime numbers.

Examples
input
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
output
3
1
1
input
5 10
8 7 5 1 0
1 2
2 3
1 5
2 4
3
1 1 0
1 1 2
2 2
output
2


题意

给你一棵树,n个结点,再给出一个数m(1 ≤ m ≤ 1000)

每个点有一个点权

有两个操作

1 x v 使得x子树里面的所有点的权值加v

2 x 查询x的子树里面所有点的权值中有多少个满足p+k*m,其中p是小于m的素数,求出p的个数。


题解:

对于树上对一个结点子树的操作,可以采用dfs序。然后用线段树存储,对于线段树的每个节点,维护区间出现过哪些数。可以用bitset实现。

更新操作,就直接让这个bitset循环移动就好了,注意循环移动可以拆成两个步骤,一个向右边移动(x%m),一个向左边移动(m-x),然后两个并起来就好了

查询操作,就最后得到那个区间的bitset和m以内的素数表&一下就好了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=100000+100;
const int maxm=1000+5;
typedef bitset<maxm> S;
int head[maxn];
int n,m;
struct edge
{
    int from,to,next;
}e[maxn*2];   //
int tol=0;
void add(int u,int v)
{
    e[++tol].to=v,e[tol].next=head[u],head[u]=tol;
}

struct node
{
    int l,r;
    int lazy,tag;
    S sum;
}seg[maxn*4];
int c[maxn];
void build(int i,int l,int r)
{
    seg[i].l=l,seg[i].r=r,seg[i].sum.reset(),seg[i].lazy=seg[i].tag=0;
    if(l==r)
    {
        seg[i].sum[c[l]]=1;
        return;
    }
    int m=(l+r)/2;
    build(i*2,l,m),build(i*2+1,m+1,r);
    seg[i].sum=seg[i*2].sum|seg[i*2+1].sum;
}
void pushdown(int i)
{
    if(seg[i].l!=seg[i].r)
    {
        int v=seg[i].tag;
        S t1=seg[i*2].sum,t2=seg[i*2+1].sum;
        seg[i*2].sum=(t1<<v)|(t1>>(m-v)),seg[i*2+1].sum=(t2<<v)|(t2>>(m-v));
        seg[i*2].tag=(seg[i*2].tag+v)%m,seg[i*2+1].tag=(seg[i*2+1].tag+v)%m;
        seg[i*2].lazy=seg[i*2+1].lazy=1;
        seg[i].lazy=seg[i].tag=0;
    }
}
void update(int i,int l,int r,int v)
{
    if(seg[i].l==l&&seg[i].r==r)
    {
        if(seg[i].lazy)
            seg[i].tag+=v,seg[i].tag%=m;
        else
        {
            seg[i].tag=v;
            seg[i].lazy=1;
        }
        S t=seg[i].sum;
        seg[i].sum=(seg[i].sum<<v)|(seg[i].sum>>(m-v));
        return;
    }
    if(seg[i].lazy)
        pushdown(i);
    int m=(seg[i].l+seg[i].r)/2;
    if(r<=m) update(i*2,l,r,v);
    else if(l>m) update(i*2+1,l,r,v);
    else
    {
        update(i*2,l,m,v),update(i*2+1,m+1,r,v);
    }
    seg[i].sum=seg[i*2].sum|seg[i*2+1].sum;
}
S query(int i,int l,int r)
{
    if(seg[i].l==l&&seg[i].r==r)
    {
        return seg[i].sum;
    }
    if(seg[i].lazy)
        pushdown(i);
    int m=(seg[i].l+seg[i].r)/2;
    if(r<=m) return query(i*2,l,r);
    else if(l>m) return query(i*2+1,l,r);
    else return query(i*2,l,m)|query(i*2+1,m+1,r);
}
int st[maxn],en[maxn];
int a[maxn];
int cnt=0;
void dfs(int u,int f)       //dfs序
{
    st[u]=++cnt;
    c[cnt]=a[u];
    for(int i=head[u];i;i=e[i].next)
    {
        int v=e[i].to;
        if(v==f) continue;
        dfs(v,u);
    }
    en[u]=cnt;
}
int vis[maxn];
S pri;
void init()
{
    memset(vis,0,sizeof(vis));
    pri.reset();
    for(int i=2;i<m;i++)       //预处理m以内的素数,储存在pri中
    {
        if(vis[i]) continue;
        pri[i]=1;
        for(int j=i*i;j<m;j+=i)
            vis[j]=1;
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    init();
    rep(i,1,n+1) scanf("%d",&a[i]),a[i]%=m;
    rep(i,1,n)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v),add(v,u);
    }
    dfs(1,-1);
    build(1,1,n);
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int op,u,v;
        scanf("%d",&op);
        if(op==1)
        {
            scanf("%d%d",&u,&v);
            v%=m;
            update(1,st[u],en[u],v);
        }
        else
        {
            scanf("%d",&u);
            S res=query(1,st[u],en[u]);
            int ans=(res&pri).count();   //
            printf("%d\n",ans);
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值