国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Manthan, Codefest 16 G. Yash And Trees(dfs序,bitset,線段樹,好題)

2019-11-09 20:36:27
字體:
來源:轉載
供稿:網友

題目鏈接G. Yash And Treestime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard 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:

for given node v and integer value x, increase all ai in the subtree of node v by value xfor 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.

Examplesinput
8 203 7 9 8 4 11 7 31 21 33 44 54 64 75 842 11 1 12 52 4output
311input
5 108 7 5 1 01 22 31 52 431 1 01 1 22 2output
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 secondtypedef 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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 股票| 紫云| 文安县| 普宁市| 义马市| 三明市| 儋州市| 房山区| 湖北省| 乌鲁木齐县| 万源市| 开原市| 青川县| 永胜县| 甘谷县| 天峻县| 石嘴山市| 竹北市| 永仁县| 益阳市| 南康市| 仁化县| 德清县| 昌宁县| 澳门| 峡江县| 林口县| 铜川市| 汝阳县| 特克斯县| 安陆市| 冕宁县| 务川| 九龙坡区| 新源县| 襄垣县| 资中县| 贵定县| 米泉市| 堆龙德庆县| 永城市|