[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, ...
随机推荐
- beautifulsoup之CSS选择器
BeautifulSoup支持大部分的CSS选择器,其语法为:向tag或soup对象的.select()方法中传入字符串参数,选择的结果以列表形式返回. tag.select("string ...
- 3. 跟踪标记 (Trace Flag) 1204, 1222 抓取死锁信息
跟踪标记:1204/1222 功能及用途: 捕获SQL Server死锁信息,并自动存放到错误日志(ERRORLOG)中. 举例: USE tempdb GO CREATE TABLE t1(id i ...
- 微信自定义菜单的emoji图标
微信公众号自定义菜单添加emoji表情图标 第一步:打开微信公众平台接口调试工具,点击前往接口调试工具: 第二步:把这段代码 {"button":[{"sub_butt ...
- Java基础 之软引用、弱引用、虚引用 ·[转载]
Java基础 之软引用.弱引用.虚引用 ·[转载] 2011-11-24 14:43:41 Java基础 之软引用.弱引用.虚引用 浏览(509)|评论(1) 交流分类:Java|笔记分类: Ja ...
- python3 装饰器全解
本章结构: 1.理解装饰器的前提准备 2.装饰器:无参/带参的被装饰函数,无参/带参的装饰函数 3.装饰器的缺点 4.python3的内置装饰器 5.本文参考 理解装饰器的前提:1.所有东西都是对象( ...
- BZOJ 1877 晨跑 拆点费用流
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1877 题目大意: Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧 ...
- MySQL管理.md
用户管理 创建 举例 mysql> create user test@localhost identified by 'password'; Query OK, 0 rows affected ...
- impala jdbc驱动执行impala sql的一个坑(不支持多行sql)
架构使用spark streaming 消费kafka的数据,并通过impala来插入到kudu中,但是通过对比发现落地到kudu表中的数据比kafka消息数要少,通过后台日志发现,偶发性的出现jav ...
- k8s1.8 ingress 配置
kubectl create secret tls ingress-secret-fengjian --key /data/sslkey/cinyi.key --cert /data/sslkey/c ...
- 第二部分 OpenStack安装与配置
第二部分 OpenStack安装与配置 一.引言 本章内容讲解如何在3台物理机上搭建最小化云平台,这3台机器分为称为Server1.Server2和Client1,之后的各章也是如此.Server ...