Manthan, Codefest 16 G. Yash And Trees dfs序+线段树+bitset
G. Yash And Trees
题目连接:
http://www.codeforces.com/contest/633/problem/G
Description
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 ai associated 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 x
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 m from 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.
Sample 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
Sample Output
3
1
1
Hint
题意
给你一棵树,然后给你n,m
每个点有一个点权
有两个操作
1 x v 使得x子树里面的所有点的权值加v
2 x 查询x的子树里面有多少个点的权值满足p+k*m,其中p是小于m的素数
题解:
就直接dfs序跑一发就好了
我们用bitset维护每一个点他下面出现过哪些数
更新操作,就直接让这个bitset循环移动就好了,注意循环移动可以拆成两个步骤,一个向右边移动(x%m),一个向左边移动(m-x),然后两个并起来就好了
查询操作,就最后得到那个区间的bitset和素数表&一下就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int MAXN = 1005;
typedef bitset<MAXN> SgTreeDataType;
vector<int> E[maxn];
bitset<MAXN>pri;
int st[maxn],ed[maxn],tot=0,n,m,v[1020],a[maxn],c[maxn];
struct treenode
{
int L , R ;
SgTreeDataType sum;
int lazy;
void update(int x)
{
lazy=(lazy+x)%m;
sum=(sum<<x)|(sum>>(m-x));
}
};
treenode tree[maxn*4];
inline void push_down(int o)
{
int lazyval = tree[o].lazy;
if(lazyval)
{
tree[2*o].update(lazyval) ; tree[2*o+1].update(lazyval);
tree[o].lazy = 0;
}
}
inline void push_up(int o)
{
tree[o].sum = tree[2*o].sum | tree[2*o+1].sum;
}
inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum.reset(), tree[o].lazy = 0;
if (L == R)
{
tree[o].sum[c[L]]=1;
return;
}
if (R > L)
{
int mid = (L+R) >> 1;
build_tree(L,mid,o*2);
build_tree(mid+1,R,o*2+1);
push_up(o);
}
}
inline void update(int QL,int QR,int v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].update(v);
else
{
push_down(o);
int mid = (L+R)>>1;
if (QL <= mid) update(QL,QR,v,o*2);
if (QR > mid) update(QL,QR,v,o*2+1);
push_up(o);
}
}
inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
push_down(o);
int mid = (L+R)>>1;
SgTreeDataType res;
res.reset();
if (QL <= mid) res |= query(QL,QR,2*o);
if (QR > mid) res |= query(QL,QR,2*o+1);
push_up(o);
return res;
}
}
void dfs(int x,int fa)
{
st[x]=++tot;
c[tot]=a[x];
for(int i=0;i<E[x].size();i++)
{
if(E[x][i]==fa)continue;
dfs(E[x][i],x);
}
ed[x]=tot;
}
void get_pri()
{
for(int i=2;i<m;i++)
{
if(v[i])continue;
pri[i]=1;
for(int j=i;j<m;j+=i)
v[j]=1;
}
}
int main()
{
scanf("%d%d",&n,&m);
get_pri();
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),a[i]%=m;
for(int i=1;i<n;i++)
{
int x,y;scanf("%d%d",&x,&y);
E[x].push_back(y);
E[y].push_back(x);
}
dfs(1,-1);
build_tree(1,n,1);
int q;scanf("%d",&q);
while(q--)
{
int op;scanf("%d",&op);
if(op==1)
{
int x,y;
scanf("%d%d",&x,&y);
update(st[x],ed[x],y%m,1);
}
else
{
int x;scanf("%d",&x);
bitset<MAXN> ans=query(st[x],ed[x],1);
printf("%d\n",(ans&pri).count());
}
}
return 0;
}
Manthan, Codefest 16 G. Yash And Trees dfs序+线段树+bitset的更多相关文章
- codeforces 633G. Yash And Trees dfs序+线段树+bitset
题目链接 G. Yash And Trees time limit per test 4 seconds memory limit per test 512 megabytes input stand ...
- CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset
题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...
- Codeforces633G(SummerTrainingDay06-I dfs序+线段树+bitset)
G. Yash And Trees time limit per test:4 seconds memory limit per test:512 megabytes input:standard i ...
- Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树
E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...
- DFS序+线段树+bitset CF 620E New Year Tree(圣诞树)
题目链接 题意: 一棵以1为根的树,树上每个节点有颜色标记(<=60),有两种操作: 1. 可以把某个节点的子树的节点(包括本身)都改成某种颜色 2. 查询某个节点的子树上(包括本身)有多少个不 ...
- codeforces 620E. New Year Tree dfs序+线段树+bitset
题目链接 给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60. 每个节点一个bitset维护就可以. #in ...
- POJ 3321 DFS序+线段树
单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4: 5: #include < ...
- 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树
题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...
- BZOJ1103 [POI2007]大都市meg dfs序 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1103 题意概括 一棵树上,一开始所有的边权值为1,我们要支持两种操作: 1. 修改某一条边的权值为 ...
随机推荐
- 自己动手实现arm函数栈帧回溯【转】
转自:http://blog.csdn.net/dragon101788/article/details/18668505 内核版本:2.6.14 glibc版本:2.3.6 CPU平台:arm gl ...
- Linux 入门记录:十八、Linux 系统启动流程 + 单用户修改 root 密码 + GRUB 加密
一.系统启动流程 一般来说,Linux 系统的启动流程是这样的: 1. 开机之后,位于计算机主板 ROM 芯片上的 BIOS 被最先读取,在进行硬件和内存的校验以及 CPU 的自检没有异常后, BIO ...
- macaca安装失败的解决办法!
https://github.com/macacajs/macaca-android https://www.jianshu.com/p/76a5be6c1036
- AMD嵌入式G系列SoC协助优化Gizmo 2开发板
http://www.gizmosphere.org/ AMD嵌入式G系列SoC协助优化Gizmo 2开发板 http://news.zol.com.cn/491/4910444.html
- c json实战引擎四 , 最后❤跳跃
引言 - 以前那些系列 长活短说, 写的最终 scjson 纯c跨平台引擎, 希望在合适场景中替代老的csjon引擎, 速度更快, 更轻巧. 下面也是算一个系列吧. 从cjson 中得到灵感, 外加 ...
- Socket与URL通信比较
转至链接:http://blog.csdn.net/qq_15848173/article/details/46328399 利用URL通信和Socket进行通信有很多相似之处.他们都是利用建立连接. ...
- android dialog 筛选列表
1.效果图 2. 布局文件 1)显示筛选的标题 <?xml version="1.0" encoding="utf-8"?> <LinearL ...
- 14:django 用户认证系统
django认证系统包含三个部分:用户.权限和分组 安装 django项目默认启用了认证系统,如果不是使用django-admin.py创建项目的可以通过在settings配置文件里面的INSTALL ...
- POJ 2492 A Bug's Life(带权并查集)
题目链接:http://poj.org/problem?id=2492 题目大意:有n只虫子,m对关系,m行每行有x y两个编号的虫子,告诉你每对x和y都为异性,先说的是对的,如果后面给出关系与前面的 ...
- request.getRemoteAddr() 获取的值为0:0:0:0:0:0:0:1的原因及解决办法
问题: 在近期开发的javaweb项目中,需要记录登录时的电脑ip地址和主机名,通过request.getRemoteAddr()和request.getRemoteHost()得到的值都是0:0:0 ...