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

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

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

2019-11-09 20:25:58
字體:
供稿:網(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個結(jié)點,再給出一個數(shù)m(1?≤?m?≤?1000)

每個點有一個點權(quán)

有兩個操作

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

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

題解:

對于樹上對一個結(jié)點子樹的操作,可以采用dfs序。然后用線段樹存儲,對于線段樹的每個節(jié)點,維護區(qū)間出現(xiàn)過哪些數(shù)??梢杂胋itset實現(xiàn)。

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

查詢操作,就最后得到那個區(qū)間的bitset和m以內(nè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)的素數(shù),儲存在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ā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 西华县| 锡林郭勒盟| 宝丰县| 平阳县| 克山县| 江阴市| 江达县| 思茅市| 阿瓦提县| 临夏市| 延边| 邢台市| 舞钢市| 赣州市| 辽阳县| 饶平县| 五台县| 罗源县| 天等县| 新沂市| 闵行区| 德化县| 新野县| 陇南市| 郸城县| 邹城市| 洪湖市| 鄂托克前旗| 昭觉县| 井研县| 郓城县| 鄂伦春自治旗| 东乡县| 定西市| 逊克县| 淮滨县| 文登市| 临城县| 古浪县| 赫章县| 喀喇沁旗|