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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

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

2019-11-09 20:13:51
字體:
供稿:網(wǎng)友

題目鏈接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個(gè)結(jié)點(diǎn),再給出一個(gè)數(shù)m(1?≤?m?≤?1000)

每個(gè)點(diǎn)有一個(gè)點(diǎn)權(quán)

有兩個(gè)操作

1 x v 使得x子樹里面的所有點(diǎn)的權(quán)值加v

2 x 查詢x的子樹里面所有點(diǎn)的權(quán)值中有多少個(gè)滿足p+k*m,其中p是小于m的素?cái)?shù),求出p的個(gè)數(shù)。

題解:

對于樹上對一個(gè)結(jié)點(diǎn)子樹的操作,可以采用dfs序。然后用線段樹存儲(chǔ),對于線段樹的每個(gè)節(jié)點(diǎn),維護(hù)區(qū)間出現(xiàn)過哪些數(shù)。可以用bitset實(shí)現(xiàn)。

更新操作,就直接讓這個(gè)bitset循環(huán)移動(dòng)就好了,注意循環(huán)移動(dòng)可以拆成兩個(gè)步驟,一個(gè)向右邊移動(dòng)(x%m),一個(gè)向左邊移動(dòng)(m-x),然后兩個(gè)并起來就好了

查詢操作,就最后得到那個(gè)區(qū)間的bitset和m以內(nèi)的素?cái)?shù)表&一下就好了。

#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++)       //預(yù)處理m以內(nèi)的素?cái)?shù),儲(chǔ)存在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;}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 台中县| 灵台县| 辛集市| 合阳县| 霞浦县| 诏安县| 松江区| 长顺县| 龙南县| 平潭县| 江安县| 广安市| 黄山市| 东乌珠穆沁旗| 综艺| 武威市| 宜宾县| 德钦县| 灵武市| 玉林市| 南通市| 隆子县| 桓台县| 柳林县| 监利县| 长阳| 庐江县| 常山县| 叙永县| 敖汉旗| 屏边| 陇南市| 田阳县| 石城县| 高清| 肇庆市| 北宁市| 蛟河市| 竹溪县| 博湖县| 仁布县|