Codeforces633G - Yash And Trees
Description
给出一个\(n(n\leq10^5)\)个点的带点权树,以\(1\)为根;以及正整数\(m(m\leq10^3)\)。进行\(q(q\leq10^5)\)次操作:
- 给\(v\)的子树中的所有点的点权加\(x\)。
- 询问有多少个不同的质数\(p\),在\(v\)的子树中存在一个点的点权\(\bmod m=p\)。
Solution
线段树+bitset
。
做出这棵树的DFS序,那么原操作就相当于序列的区间加和区间查询,用线段树实现。线段树上每个节点维护一个bitset
\(info\),其中\(info[x]=1\)表示在这个区间内存在点权\(\bmod m=x\)的点。那么合并就只要按位或。
考虑如何给区间加\(x\):\(info[(i+x)\bmod m]=info[i]\),那么对于\(i<m-x\)只要左移\(x\)位,对于\(i>=m-x\)相当于移动到\(i+x-m\)位,也就是右移\(m-x\)位。info=(info<<x)|(info>>m-x)
,前面溢出的由于用不到所以可以不管(不过我因为这个WA了QAQ)。
询问时,将\(O(logn)\)个节点的\(info\)或起来,然后检查有多少个小于\(m\)的质数出现了。
时间复杂度\(O(qlogn)\)。
Code
//Yash And Trees
#include <bitset>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef bitset<1010> bits;
inline char gc()
{
static char now[1<<16],*s,*t;
if(s==t) {t=(s=now)+fread(now,1,1<<16,stdin); if(s==t) return EOF;}
return *s++;
}
inline int read()
{
int x=0; char ch=gc();
while(ch<'0'||'9'<ch) ch=gc();
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
return x;
}
int pr[200]={0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997};
const int N=1e5+10;
int n,m,a[N];
vector<int> son[N];
void edAdd(int u,int v) {son[u].push_back(v),son[v].push_back(u);}
int dfCnt,dfn[N],fr[N],to[N];
void dfs(int u,int fa)
{
dfn[++dfCnt]=u; fr[u]=dfCnt;
for(int i=0;i<son[u].size();i++)
{
int v=son[u][i];
if(v!=fa) dfs(v,u);
}
to[u]=dfCnt;
}
int sgCnt,rt,ch[N<<1][2]; bits info[N<<1]; int tag[N<<1];
void update(int p) {info[p]=info[ch[p][0]]|info[ch[p][1]];}
void change(int p,int x) {info[p]=(info[p]<<x)|(info[p]>>m-x); tag[p]=(tag[p]+x)%m;}
void pushdw(int p) {if(tag[p]) change(ch[p][0],tag[p]),change(ch[p][1],tag[p]),tag[p]=0;}
void bldTr(int &p,int L0,int R0)
{
if(!p) p=++sgCnt;
if(L0==R0) {info[p][a[dfn[L0]]]=1; return;}
int mid=L0+R0>>1;
bldTr(ch[p][0],L0,mid);
bldTr(ch[p][1],mid+1,R0);
update(p);
}
int optL,optR;
void ins(int p,int L0,int R0,int x)
{
if(optL<=L0&&R0<=optR) {change(p,x); return;}
pushdw(p);
int mid=L0+R0>>1;
if(optL<=mid) ins(ch[p][0],L0,mid,x);
if(mid<optR) ins(ch[p][1],mid+1,R0,x);
update(p);
}
bits resQ;
void query(int p,int L0,int R0)
{
if(optL<=L0&&R0<=optR) {resQ|=info[p]; return;}
pushdw(p);
int mid=L0+R0>>1;
if(optL<=mid) query(ch[p][0],L0,mid);
if(mid<optR) query(ch[p][1],mid+1,R0);
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++) a[i]=read()%m;
for(int i=1;i<=n-1;i++) edAdd(read(),read());
dfs(1,0); bldTr(rt,1,n);
int Q=read();
while(Q--)
{
int opt=read(),v=read(); optL=fr[v],optR=to[v];
if(opt==1) ins(rt,1,n,read()%m);
else
{
resQ=0,query(rt,1,n); int ans=0;
for(int i=1;i<=168&&pr[i]<m;i++) if(resQ[pr[i]]) ans++; //要同时判断pr[i]<m!
printf("%d\n",ans);
}
}
return 0;
}
P.S.
因为懒得写线性筛所以打表了\(1000\)以内的质数,但是由于没有处理溢出也没有判断质数是否小于\(m\)所以WA掉了...感谢elijahqi大佬
Codeforces633G - Yash And Trees的更多相关文章
- 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 ...
- Manthan, Codefest 16 G. Yash And Trees dfs序+线段树+bitset
G. Yash And Trees 题目连接: http://www.codeforces.com/contest/633/problem/G Description Yash loves playi ...
- CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset
题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...
- G. Yash And Trees 线段树 + dfs序 + bitset
这个是要用bitset 一个大整数的二进制 学习推荐博客 这个题目大意就是:给你n,m 还有一个序列,还有一个一棵树,有一种操作一种询问 操作是给你一个节点 把这个节点及其子节点都加上x 询问是 给你 ...
- Codeforces633G(SummerTrainingDay06-I dfs序+线段树+bitset)
G. Yash And Trees time limit per test:4 seconds memory limit per test:512 megabytes input:standard i ...
- Manthan, Codefest 16
暴力 A - Ebony and Ivory import java.util.*; import java.io.*; public class Main { public static void ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- [C#] C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
随机推荐
- Oracle汇总
1.数据库事务并发会产生那些问题?有哪些隔离级别,分别能够避免什么错误,而无法避免什么错误? a.事务并发会导致三种问题:脏读.不可重复读.幻象读 脏读:读取了未提交的数据 不可重复读:前后读取同一行 ...
- 查看Windows激活信息
使用 Windows + R组合快捷键打开运行命令框 1.运行: slmgr.vbs -dlv 可以查询到Win10的激活信息,包括:激活ID.安装ID.激活截止日期等信息. 2.运行: slmgr. ...
- FZU 1977 Pandora adventure (插头DP,常规)
题意:有一个n*m矩阵,其中有些格子必走,有些格子不可走,其他格子是可走也可不走,问有多少条哈密顿回路? 思路: 本来是一道很简单的题,代码写多了连白痴bug都查不出了,竟然用i>=ex& ...
- 解决Errno::ENOENT: No Such File or Directory - Jekyll ~ Octopress and El Capitan
参考http://schalkneethling.github.io/blog/2015/10/16/errno-enoent-no-such-file-or-directory-jekyll-oct ...
- make与makefile的几个例子和(自己写一下,汗!忘记了!)总结
共用的几个源代码文件: main.c 2.c 3.c 代码依次为: #include<stdlib.h> #include "a.h" extern void func ...
- VC-基础:VC中得到当前系统的时间和日期
得到当前时间的方法一般都是得到从1900年0点0分到现在的秒数,然后转为年月日时分秒的形式得到当前的时间(时分秒).主要方法如下: 1)使用CRT函数 C++代码 ]; time_t nowtim ...
- CAS (Compare and Swap)
synchronized是悲观锁 注意:实现了CAS的有原子类(AtomicInteger,AtomicLong,等等原子类) CAS 是乐观锁,一种高效实现线程安全性的方法 1.支持原子更新操作,适 ...
- shell脚本,按行读取文件的几种方法。
第一种方法用while实现按读取文件.[root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 e ...
- Philipp Wagner
本文大部分来自OpenCV官网上的Face Reconition with OpenCV这节内容(http://docs.opencv.org/modules/contrib/doc/facerec/ ...
- 新建Maven工程,pom.xml报错web.xml is missing and <failOnMissingWebXml> is set to true
错误原因: 项目中没有web.xml 解决办法: 在项目中添加web.xml 在pom.xml中添加下面的插件 <build> <plugins> <plugin> ...