[BZOJ3772]精神污染 主席树上树+欧拉序
3772: 精神污染
Time Limit: 10 Sec Memory Limit: 64 MB
Description
Input
Output
Sample Input
1 2
2 3
3 4
2 5
3 5
2 5
1 4
Sample Output
样例解释
可以选择的路径对有(1,2),(1,3),(2,3),只有路径1完全覆盖路径2。
HINT
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int N=;
vector<int>vec[N];
int n,m,num,deep[N],adj[N],e,l[N],r[N];
struct edge{int zhong,next;}s[N<<];
inline void add(int qi,int zhong){s[++e].zhong=zhong;s[e].next=adj[qi];adj[qi]=e;}
struct data{int qi,zhong;}ro[N];
inline bool mt(const data &a,const data &b){return (a.qi==b.qi)?a.zhong<b.zhong:a.qi<b.qi;}
struct node
{
node *ch[];int cnt;
void* operator new (size_t,node *a,node *b,int c)
{
static node mempool[N*],*C=mempool;
C->ch[]=a,C->ch[]=b,C->cnt=c;
return C++;
}
node* insert(int l,int r,int pos,int val)
{
if(l==r)return new (NULL,NULL,this->cnt+val)node;
int mi=(l+r)>>;
if(pos<=mi) return new (ch[]->insert(l,mi,pos,val),ch[],this->cnt+val)node;
else return new (ch[],ch[]->insert(mi+,r,pos,val),this->cnt+val)node;
}
}*root[N];
int query(node *a,node *b,node *c,node *d,int le,int ri,int L,int R)
{
if(L<=le&&ri<=R)return a->cnt+b->cnt-c->cnt-d->cnt;
int ret=,mi=(le+ri)>>;
if(L<=mi)ret+=query(a->ch[],b->ch[],c->ch[],d->ch[],le,mi,L,R);
if(mi<R)ret+=query(a->ch[],b->ch[],c->ch[],d->ch[],mi+,ri,L,R);
return ret;
} int f[N][],bin[];
inline int LCA(int a,int b)
{
if(deep[a]<deep[b])swap(a,b);
int cha=deep[a]-deep[b];
for(int j=;~j;j--)
if(cha&bin[j])a=f[a][j];
if(a==b)return a;
for(int j=;~j;j--)
if(f[a][j]!=f[b][j])a=f[a][j],b=f[b][j];
return f[a][];
} void dfs1(int rt,int fa)
{
l[rt]=++num;deep[rt]=deep[fa]+;
for(int i=adj[rt];i;i=s[i].next)
if(s[i].zhong!=fa)f[s[i].zhong][]=rt,dfs1(s[i].zhong,rt);
r[rt]=++num;
}
void dfs2(int rt,int fa)
{
root[rt]=root[fa];
for(int i=,len=vec[rt].size();i<len;i++)
{
root[rt]=root[rt]->insert(,n<<,l[vec[rt][i]],);
root[rt]=root[rt]->insert(,n<<,r[vec[rt][i]],-);
}
for(int i=adj[rt];i;i=s[i].next)
if(s[i].zhong!=fa)dfs2(s[i].zhong,rt);
}
LL Z,M;
LL gcd(LL a,LL b){return (b==)?a:gcd(b,a%b);} int main()
{
scanf("%d%d",&n,&m);int a,b;
for(int i=;i<n;i++)scanf("%d%d",&a,&b),add(a,b),add(b,a);
for(int i=;i<=m;i++)
scanf("%d%d",&ro[i].qi,&ro[i].zhong),vec[ro[i].qi].push_back(ro[i].zhong);
root[]=new (NULL,NULL,) node;
root[]->ch[]=root[]->ch[]=root[];
dfs1(,);dfs2(,);
bin[]=;for(int i=;i<=;i++)bin[i]=bin[i-]<<;
for(int j=;j<=;j++)
for(int i=;i<=n;i++)
f[i][j]=f[f[i][j-]][j-];
for(int i=;i<=m;i++)
{
int lca=LCA(ro[i].qi,ro[i].zhong),t=f[lca][];
a=ro[i].qi,b=ro[i].zhong;
Z+=query(root[a],root[b],root[lca],root[t],,n<<,l[lca],l[a]);
Z+=query(root[a],root[b],root[lca],root[t],,n<<,l[lca],l[b]);
Z-=query(root[a],root[b],root[lca],root[t],,n<<,l[lca],l[lca]);
Z--;
}
M=(LL)m*(m-)>>;LL tmp=gcd(Z,M);
printf("%lld/%lld",Z/tmp,M/tmp);
}
[BZOJ3772]精神污染 主席树上树+欧拉序的更多相关文章
- BZOJ3772 精神污染 主席树 dfs序
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3772 题意概括 给出一个树,共n个节点. 有m条互不相同的树上路径. 现在让你随机选择2条路径,问 ...
- 【BZOJ 3772】精神污染 主席树+欧拉序
这道题的内存…………………真·精神污染……….. 这道题的思路很明了,我们就是要找每一个路径包含了多少其他路径那么就是找,有多少路径的左右端点都在这条路径上,对于每一条路径,我们随便选定一个端点作为第 ...
- bzoj 3772 精神污染 主席树+dfs序
精神污染 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 637 Solved: 177[Submit][Status][Discuss] Descri ...
- BZOJ 4026 dC Loves Number Theory (主席树+数论+欧拉函数)
题目大意:给你一个序列,求出指定区间的(l<=i<=r) mod 1000777 的值 还复习了欧拉函数以及线性筛逆元 考虑欧拉函数的的性质,(l<=i<=r),等价于 (p[ ...
- CF383C Propagating tree (线段树,欧拉序)
\(tag\)没开够\(WA\)了一发... 求出\(dfs\)序,然后按深度分类更新与查询. #include <iostream> #include <cstdio> #i ...
- BZOJ 4034 [HAOI2015]树上操作(欧拉序+线段树)
题意: 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都增 ...
- Underground Lab CodeForces - 782E (欧拉序)
大意:$n$结点,$m$条边无向图, 有$k$个人, 每个人最多走$\left\lceil\frac {2n}{k}\right\rceil$步, 求一种方案使得$k$个人走遍所有的点 $n$结点树的 ...
- hdu 2586 欧拉序+rmq 求lca
题意:求树上任意两点的距离 先说下欧拉序 对这颗树来说 欧拉序为 ABDBEGBACFHFCA 那欧拉序有啥用 这里先说第一个作用 求lca 对于一个欧拉序列,我们要求的两个点在欧拉序中的第一个位置之 ...
- lca 欧拉序+rmq(st) 欧拉序+rmq(线段树) 离线dfs 倍增
https://www.luogu.org/problemnew/show/P3379 1.欧拉序+rmq(st) /* 在这里,对于一个数,选择最左边的 选择任意一个都可以,[left_index, ...
随机推荐
- [TSQL|SQLSERVER|MSSQL数据库] 将数据库文件与日志附加到数据库引擎,以及转移数据库文件位置
附加: USE [master] GO CREATE DATABASE [database_name] ON ( FILENAME = N'C:\Data\<database name>. ...
- 原生java调用webservice的方法,不用生成客户端代码
原生java调用webservice的方法,不用生成客户端代码 2015年10月29日 16:46:59 阅读数:1455 <span style="font-family: Aria ...
- 如何判断一个整数是否是2的N次幂
static bool CheckPowerOfTwo(ulong num) { && (num & (num - )) == ; }
- 转:线程Thread (1)
引言 1.理解多线程 2. 线程异步与线程同步 3.创建多线程应用程序 3.1通过System.Threading命名空间的类构建 3.1.1异步调用线程 3.1.2并发问题 3.1.3线程同步 3. ...
- django新建项目,连接mysql数据库
安装django,进入Django目录,运行 python setup.py install 在workplace目录下新建一个名为site01的项目: cd workplacedjango-admi ...
- Python学习---IO的异步[asyncio模块(no-http)]
Asyncio进行异步IO请求操作: 1. @asyncio.coroutine 装饰任务函数 2. 函数内配合yield from 和装饰器@asyncio.coroutine 配合使用[固定格式 ...
- PHP CLI模式下echo换行
近日在执行导库程序的时候,需要在CLI模式下运行程序进行调试,如下图,这是什么鬼?不是我想要的结果 后经过查资料发现代码中执行的输出为 //错误方法实例 echo '其他-683\n'; //正确打开 ...
- Uva1395 POJ3522 Slim Span (最小生成树)
Description Given an undirected weighted graph G, you should find one of spanning trees specified as ...
- [AHOI2009]最小割
题目 最小割的可行边和必须边 可行边\((u,v)\)需要满足以下两个条件 满流 残量网络中不存在\(u\)到\(v\)的路径 这个挺好理解的呀,如果存在还存在路径的话那么这条边就不会是瓶颈了 必须边 ...
- js Json与对象、数组转化
对象.数组转json json = JSON.stringify(obj) json 转对象.数组 arr = JSON.parse(json)