3091: 城市旅行

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1454  Solved: 483
[Submit][Status][Discuss]

Description

Input

Output

Sample Input

4 5
1 3 2 5
1 2
1 3
2 4
4 2 4
1 2 4
2 3 4
3 1 4 1
4 1 4

Sample Output

16/3
6/1

HINT

对于所有数据满足 1<=N<=50,000 1<=M<=50,000 1<=Ai<=10^6 1<=D<=100 1<=U,V<=N

Source

wyx528命题


谜一般的翻转标记

大爷题解传送门:http://blog.csdn.net/popoqqq/article/details/40823659

与上一题不一样的是,每个点在链上的位置不一定,所以没法维护vi*i之类的东西,只能像那样记录lsum和rsum然后update的时候处理

本题的翻转标记必须立即生效,因为反转标记还会影响lsum和rsum

除了一开始就Link外,还可以保存图然后dfs只设置fa关系不用Link,然而并没有带来常数提升反而更慢了...

BZOJ 100题达成

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define pa t[x].fa
#define lc t[x].ch[0]
#define rc t[x].ch[1]
const int N=5e4+;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} struct node{
int ch[],fa,rev;
ll add,lsum,rsum,sum,exp,w,size;
}t[N];
inline int wh(int x){return t[pa].ch[]==x;}
inline int isRoot(int x){return t[pa].ch[]!=x&&t[pa].ch[]!=x;}
inline void update(int x){
t[x].size=t[lc].size+t[rc].size+;
t[x].sum=t[lc].sum+t[rc].sum+t[x].w;
t[x].lsum=t[lc].lsum+t[x].w*(t[lc].size+)+t[rc].lsum+t[rc].sum*(t[lc].size+);
t[x].rsum=t[rc].rsum+t[x].w*(t[rc].size+)+t[lc].rsum+t[lc].sum*(t[rc].size+);
t[x].exp=t[lc].exp+t[rc].exp
+t[lc].lsum*(t[rc].size+)+t[rc].rsum*(t[lc].size+)
+t[x].w*(t[lc].size+)*(t[rc].size+);
}
inline ll cal1(ll x){return x*(x+)/;}
inline ll cal2(ll x){return x*(x+)*(x+)/;}
inline void paint(int x,ll d){
t[x].w+=d;
t[x].add+=d;
t[x].sum+=d*t[x].size;
t[x].lsum+=d*cal1(t[x].size);
t[x].rsum+=d*cal1(t[x].size);
t[x].exp+=d*cal2(t[x].size);
}
inline void rever(int x){
swap(lc,rc);
swap(t[x].lsum,t[x].rsum);
t[x].rev^=;
}
inline void pushDown(int x){
if(t[x].rev){
rever(lc);
rever(rc);//!!!!!
t[x].rev=;
}
if(t[x].add){
paint(lc,t[x].add);
paint(rc,t[x].add);
t[x].add=;
}
} inline void rotate(int x){
int f=t[x].fa,g=t[f].fa,c=wh(x);
if(!isRoot(f)) t[g].ch[wh(f)]=x;t[x].fa=g;
t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
t[x].ch[c^]=f;t[f].fa=x;
update(f);update(x);
}
int st[N],top;
inline void splay(int x){
top=;st[++top]=x;
for(int i=x;!isRoot(i);i=t[i].fa) st[++top]=t[i].fa;
for(int i=top;i>=;i--) pushDown(st[i]); for(;!isRoot(x);rotate(x))
if(!isRoot(pa)) rotate(wh(x)==wh(pa)?pa:x);
} inline void Access(int x){
for(int y=;x;y=x,x=pa){
splay(x);
rc=y;
update(x);
}
}
inline void MakeR(int x){
Access(x);splay(x);
rever(x);
}
inline int FindR(int x){
Access(x);splay(x);
while(lc) x=lc;
return x;
}
inline void Link(int x,int y){
MakeR(x);
t[x].fa=y;
}
inline void Cut(int x,int y){
MakeR(x);Access(y);splay(y);
t[y].ch[]=t[x].fa=;
update(y);//!!!
}
inline void Add(int x,int y,int d){
if(FindR(x)!=FindR(y)) return;
MakeR(x);Access(y);splay(y);
paint(y,d);
}
inline ll gcd(ll a,ll b){return b==?a:gcd(b,a%b);}
inline void Que(int x,int y){
if(FindR(x)!=FindR(y)){puts("-1");return;}
MakeR(x);Access(y);splay(y);
ll a=t[y].exp,b=t[y].size*(t[y].size+)/;
ll g=gcd(a,b);//printf("Que %d %d %d\n",a,b,g);
printf("%lld/%lld\n",a/g,b/g);
}
int n,Q,a,op,x,y,d;
int main(){
n=read();Q=read();
for(int i=;i<=n;i++){
a=read();
t[i].size=;
t[i].w=t[i].lsum=t[i].rsum=t[i].sum=t[i].exp=a;
}
for(int i=;i<=n-;i++) x=read(),y=read(),Link(x,y);
while(Q--){
op=read();x=read();y=read();
if(op==) if(FindR(x)==FindR(y)) Cut(x,y);
if(op==) if(FindR(x)!=FindR(y)) Link(x,y);
if(op==) d=read(),Add(x,y,d);
if(op==) Que(x,y);
}
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define pa t[x].fa
#define lc t[x].ch[0]
#define rc t[x].ch[1]
const int N=5e4+;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} struct node{
int ch[],fa,rev;
ll add,lsum,rsum,sum,exp,w,size;
}t[N];
inline int wh(int x){return t[pa].ch[]==x;}
inline int isRoot(int x){return t[pa].ch[]!=x&&t[pa].ch[]!=x;}
inline void update(int x){
t[x].size=t[lc].size+t[rc].size+;
t[x].sum=t[lc].sum+t[rc].sum+t[x].w;
t[x].lsum=t[lc].lsum+t[x].w*(t[lc].size+)+t[rc].lsum+t[rc].sum*(t[lc].size+);
t[x].rsum=t[rc].rsum+t[x].w*(t[rc].size+)+t[lc].rsum+t[lc].sum*(t[rc].size+);
t[x].exp=t[lc].exp+t[rc].exp
+t[lc].lsum*(t[rc].size+)+t[rc].rsum*(t[lc].size+)
+t[x].w*(t[lc].size+)*(t[rc].size+);
}
inline ll cal1(ll x){return x*(x+)/;}
inline ll cal2(ll x){return x*(x+)*(x+)/;}
inline void paint(int x,ll d){
t[x].w+=d;
t[x].add+=d;
t[x].sum+=d*t[x].size;
t[x].lsum+=d*cal1(t[x].size);
t[x].rsum+=d*cal1(t[x].size);
t[x].exp+=d*cal2(t[x].size);
}
inline void rever(int x){
swap(lc,rc);
swap(t[x].lsum,t[x].rsum);
t[x].rev^=;
}
inline void pushDown(int x){
if(t[x].rev){
rever(lc);
rever(rc);//!!!!!
t[x].rev=;
}
if(t[x].add){
paint(lc,t[x].add);
paint(rc,t[x].add);
t[x].add=;
}
} inline void rotate(int x){
int f=t[x].fa,g=t[f].fa,c=wh(x);
if(!isRoot(f)) t[g].ch[wh(f)]=x;t[x].fa=g;
t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
t[x].ch[c^]=f;t[f].fa=x;
update(f);update(x);
}
int st[N],top;
inline void splay(int x){
top=;st[++top]=x;
for(int i=x;!isRoot(i);i=t[i].fa) st[++top]=t[i].fa;
for(int i=top;i>=;i--) pushDown(st[i]); for(;!isRoot(x);rotate(x))
if(!isRoot(pa)) rotate(wh(x)==wh(pa)?pa:x);
} inline void Access(int x){
for(int y=;x;y=x,x=pa){
splay(x);
rc=y;
update(x);
}
}
inline void MakeR(int x){
Access(x);splay(x);
rever(x);
}
inline int FindR(int x){
Access(x);splay(x);
while(lc) x=lc;
return x;
}
inline void Link(int x,int y){
MakeR(x);
t[x].fa=y;
}
inline void Cut(int x,int y){
MakeR(x);Access(y);splay(y);
t[y].ch[]=t[x].fa=;
update(y);//!!!
}
inline void Add(int x,int y,int d){
if(FindR(x)!=FindR(y)) return;
MakeR(x);Access(y);splay(y);
paint(y,d);
}
inline ll gcd(ll a,ll b){return b==?a:gcd(b,a%b);}
inline void Que(int x,int y){
if(FindR(x)!=FindR(y)){puts("-1");return;}
MakeR(x);Access(y);splay(y);
ll a=t[y].exp,b=t[y].size*(t[y].size+)/;
ll g=gcd(a,b);//printf("Que %d %d %d\n",a,b,g);
printf("%lld/%lld\n",a/g,b/g);
}
int n,Q,a,op,x,y,d; struct edge{
int v,ne;
}e[N<<];
int cnt,h[N];
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
inline void dfs(int u,int fa){
for(int i=h[u];i;i=e[i].ne)
if(e[i].v!=fa){
t[e[i].v].fa=u;
dfs(e[i].v,u);
}
}
int main(){
n=read();Q=read();
for(int i=;i<=n;i++){
a=read();
t[i].size=;
t[i].w=t[i].lsum=t[i].rsum=t[i].sum=t[i].exp=a;
}
for(int i=;i<=n-;i++) x=read(),y=read(),ins(x,y);
dfs(,);
while(Q--){
op=read();x=read();y=read();
if(op==) if(FindR(x)==FindR(y)) Cut(x,y);
if(op==) if(FindR(x)!=FindR(y)) Link(x,y);
if(op==) d=read(),Add(x,y,d);
if(op==) Que(x,y);
}
}

BZOJ 3091: 城市旅行 [LCT splay 期望]的更多相关文章

  1. BZOJ 3091: 城市旅行 lct 期望 splay

    https://www.lydsy.com/JudgeOnline/problem.php?id=3091 https://blog.csdn.net/popoqqq/article/details/ ...

  2. bzoj 3091: 城市旅行 LCT

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=3091 题解: 首先前三个操作就是裸的LCT模板 只考虑第四个操作. 要求我们计算期望,所以我 ...

  3. bzoj 3091 城市旅行(LCT+数学分析)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3091 [思路] 膜Popoqqq大爷的题解 click here [代码]是坑... ...

  4. BZOJ 3091 城市旅行

    Description Input Output Sample Input 4 5 1 3 2 5 1 2 1 3 2 4 4 2 4 1 2 4 2 3 4 3 1 4 1 4 1 4 Sample ...

  5. 【BZOJ3091】城市旅行 LCT

    [BZOJ3091]城市旅行 Description Input Output Sample Input 4 5 1 3 2 5 1 2 1 3 2 4 4 2 4 1 2 4 2 3 4 3 1 4 ...

  6. BZOJ3091: 城市旅行(LCT,数学期望)

    Description Input Output Sample Input 4 5 1 3 2 5 1 2 1 3 2 4 4 2 4 1 2 4 2 3 4 3 1 4 1 4 1 4 Sample ...

  7. 【BZOJ】3091: 城市旅行 Link-Cut Tree

    [题意]参考PoPoQQQ. 给定一棵树,每个点有一个点权,提供四种操作: 1.删除两点之间的连边 不存在边则无视 2.在两点之前连接一条边 两点已经联通则无视 3.在两点之间的路径上所有点的点权加上 ...

  8. bzoj3091 城市旅行 LCT + 区间合并

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3091 题解 调了整个晚自习才调出来的问题. 乍一看是个 LCT 板子题. 再看一眼还是个 LC ...

  9. BZOJ3091城市旅行——LCT区间信息合并

    题目描述 输入 输出 样例输入 4 5 1 3 2 5 1 2 1 3 2 4 4 2 4 1 2 4 2 3 4 3 1 4 1 4 1 4 样例输出 16/3 6/1 提示 对于所有数据满足 1& ...

随机推荐

  1. Zigbee Class 直播公告2016-10-10

    周一我们将开始本期课程的首次直播, 详情如下: 场次 第一场 阶段 入门 开始时间 2016-10-10  19:00 结束时间 不定 斗鱼地址 douyu.com/zigbeeclass 内容 本期 ...

  2. HDU 1233 还是畅通工程(模板——克鲁斯卡尔算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1233 题意描述: 输入n个城镇以及n*(n-1)/2条道路信息 计算并输出将所有城镇连通或者间接连通 ...

  3. 语义化版本控制规范(SemVer)

    摘自: http://semver.org/lang/zh-CN/ 简介 在软件管理的领域里存在着被称作"依赖地狱"的死亡之谷,系统规模越大,加入的套件越多,你就越有可能在未来的某 ...

  4. 免费空间上的mysql数据库怎么连接?

    我申请了一个php的免费空间,空间有带mysql数据库,可是我不知道怎么连接. 平时在本地做php时我都是怎么连接的 可是现在到空间上了我就不知道怎么连接了.空间有提供phpmyadmin 会的教一下 ...

  5. 各大型邮箱smtp服务器及端口收集

    >新浪邮箱smtp服务器 外发服务器:smtp.vip.sina.com 收件服务器:pop3.vip.sina.com 新浪免费邮件 外发服务器:smtp.sina.com.cn 收件服务器: ...

  6. VIM命令模式与输入模式切换

     vi编辑器 vi是UNIX和类UNIX环境下的可用于创建文件的屏幕编辑器.vi有两种工作模式:命令模式和文本输入模式.启动vi需要输入vi,按[Spacebar]键并输入文件名后回车. 切换模式键 ...

  7. Oracle临时表空间组

    Oracle 10g之前,同一用户的多个会话只可以使用同一个临时表空间,因为在给定的时间只有一个临时表空间默认给用户,为了解决这个潜在的瓶颈,Oracle支持临时表空间组即包含多个临时表空间的集合.临 ...

  8. JavaScript对象的valueOf()方法

    js对象中的valueOf()方法和toString()方法非常类似,但是,当需要返回对象的原始值而非字符串的时候才调用它,尤其是转换为数字的时候.如果在需要使用原始值的上下文中使用了对象,JavaS ...

  9. 要学的东西太多了,还想学习opencv

    资料先放这里,以后好好学 http://m.blog.csdn.net/column/details?alias=opencv-tutorial eclipse加载opencv库成功! B站视频教程资 ...

  10. Django_xamin注册model错误

    可能出现的错误: 1. xadmin.sites.AlreadyRegistered: The model UserProfile is already registered 2. error:Fie ...