【LCT】BZOJ3091 城市旅行
3091: 城市旅行
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1927 Solved: 631
[Submit][Status][Discuss]
Description
.jpg)
Input
.jpg)
Output
.jpg)
Sample Input
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
6/1
HINT
对于所有数据满足 1<=N<=50,000 1<=M<=50,000 1<=Ai<=10^6 1<=D<=100 1<=U,V<=N
题解
这题和BZOJ2752类似,只不过那个是在序列上而这个在链上,所以没做过2752的话可以先看看这里
我们发现每次询问其实就是算每个点对答案的贡献/C(n+1,2),也就是/(n+1)*n/2
所以每次只需要单独算每个点的贡献即可
然后每个点对答案的贡献就是a[i]*i*(n-i+1)
所以我们维护一个sum,一个siz,一个L=a[i]*i,一个R=a[i]*(n-i+1)
转移挺好想的(逃
代码
//by 减维
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<bitset>
#include<set>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<ctime>
#include<algorithm>
#define ll long long
#define il inline
#define rg register
#define db double
#define mpr make_pair
#define maxn 70005
#define inf (1<<30)
#define eps 1e-8
#define pi 3.1415926535897932384626
using namespace std; int n,m,fa[maxn],son[maxn][],siz[maxn];
ll val[maxn],sum[maxn],rev[maxn],mar[maxn],L[maxn],R[maxn],ans[maxn]; ll gcd(ll x,ll y){return y==?x:gcd(y,x%y);}
il bool pdp(int x){return son[fa[x]][]==x;}
il bool isrt(int x){return son[fa[x]][]!=x&&son[fa[x]][]!=x;}
il void rever(int x){rev[x]^=;swap(son[x][],son[x][]);swap(L[x],R[x]);} il void upda(int x)
{
siz[x]=siz[son[x][]]+siz[son[x][]]+;
sum[x]=sum[son[x][]]+sum[son[x][]]+val[x];
ans[x]=ans[son[x][]]+ans[son[x][]]+val[x]*(siz[son[x][]]+)*(siz[son[x][]]+)+L[son[x][]]*(siz[son[x][]]+)+R[son[x][]]*(siz[son[x][]]+);
L[x]=L[son[x][]]+L[son[x][]]+val[x]*(siz[son[x][]]+)+sum[son[x][]]*(siz[son[x][]]+);
R[x]=R[son[x][]]+R[son[x][]]+val[x]*(siz[son[x][]]+)+sum[son[x][]]*(siz[son[x][]]+);
} il void add(int x,ll y)
{
mar[x]+=y;
val[x]+=y;
sum[x]+=y*siz[x];
ans[x]+=y*(siz[x]+)*(siz[x]+)*siz[x]/;
L[x]+=y*(siz[x]+)*siz[x]/;
R[x]+=y*(siz[x]+)*siz[x]/;
} il void pdn(int x)
{
if(rev[x])
{
if(son[x][]) rever(son[x][]);
if(son[x][]) rever(son[x][]);
rev[x]=;
}
if(son[x][]) add(son[x][],mar[x]);
if(son[x][]) add(son[x][],mar[x]);
mar[x]=;
} il void pd(int x){if(!isrt(x)) pd(fa[x]);pdn(x);} il void rot(int x)
{
int f=fa[x],g=fa[f],o=pdp(x);
if(!isrt(f)) son[g][pdp(f)]=x;fa[x]=g;
son[f][o]=son[x][!o];fa[son[f][o]]=f;
son[x][!o]=f;fa[f]=x;
upda(f),upda(x);
} il void splay(int x)
{
pd(x);
for(;!isrt(x);rot(x))
if(!isrt(fa[x])) rot(pdp(fa[x])==pdp(x)?fa[x]:x);
} il void acc(int x)
{
for(int y=;x;y=x,x=fa[x])
splay(x),son[x][]=y,upda(x);
} il int find(int x)
{
acc(x);splay(x);
while(son[x][]) pdn(x),x=son[x][];
return x;
} il void bert(int x){acc(x);splay(x);rever(x);}
il void spli(int x,int y){bert(x);acc(y);splay(y);}
il void cut(int x,int y){spli(x,y);fa[x]=son[y][]=;upda(y);}
il void link(int x,int y){bert(x);fa[x]=y;} void dfs(int x)
{
if(son[x][]) dfs(son[x][]);
printf("%d ",x);
if(son[x][]) dfs(son[x][]);
} void dfs2(int x)
{
if(son[x][]) dfs2(son[x][]);
printf("%d ",sum[x]);
if(son[x][]) dfs2(son[x][]);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i){
scanf("%lld",&val[i]);
siz[i]=;
sum[i]=val[i];
ans[i]=val[i];
L[i]=val[i];
R[i]=val[i];
}
for(int i=,x,y;i<n;++i) scanf("%d%d",&x,&y),link(x,y);
ll d;
for(int i=,op,x,y;i<=m;++i)
{
scanf("%d%d%d",&op,&x,&y);
if(op==){
if(find(x)==find(y)) cut(x,y);
}else if(op==){
if(find(x)!=find(y)) link(x,y);
}else if(op==){
scanf("%lld",&d);
if(find(x)==find(y))spli(x,y),add(y,d);
}else{
if(find(x)!=find(y)) puts("-1");
else{
spli(x,y);
ll ans1=ans[y];
ll ans2=siz[y]*(siz[y]+)/;
ll gc=gcd(ans1,ans2);
printf("%lld/%lld\n",ans1/gc,ans2/gc);
}
}
}
return ;
}
【LCT】BZOJ3091 城市旅行的更多相关文章
- 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& ...
- BZOJ3091 城市旅行 LCT
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3091 题意概括 鉴于本人语文不好,此题的描述原题很清晰,废话不多,请看原题. 可怕,原题是图片,不 ...
- bzoj3091 城市旅行 LCT + 区间合并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3091 题解 调了整个晚自习才调出来的问题. 乍一看是个 LCT 板子题. 再看一眼还是个 LC ...
- 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 ...
- 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 1 4 1 4 Sample ...
- 【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 ...
- BZOJ 3091: 城市旅行 [LCT splay 期望]
3091: 城市旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1454 Solved: 483[Submit][Status][Discuss ...
- luogu P4842 城市旅行
嘟嘟嘟 好题,好题 刚开始突发奇想写了一个\(O(n ^ 2)\)暴力,结果竟然过了?!后来才知道是上传题的人把单个数据点开成了10s-- 不过不得不说我这暴力写的挺好看的.删边模仿链表删边,加边的时 ...
- 【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 题解 LCT区间合并 前三个 ...
随机推荐
- FFmpeg源码简单分析:libswscale的sws_scale()
===================================================== FFmpeg的库函数源码分析文章列表: [架构图] FFmpeg源码结构图 - 解码 FFm ...
- C++技术问题总结-第8篇 STL内存池是怎么实现的
STL内存池机制,使用双层级配置器.第一级採用malloc.free,第二级视情况採用不同策略. 这样的机制从heap中要空间,能够解决内存碎片问题. 1.内存申请流程图 简要流程图例如以下. ...
- Android之不须要自己定义View(ViewfindView.java)最简单的二维码扫描
不废话,先爆照 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- 原生JS与jQuery操作DOM对比
一.创建元素节点 1.1 原生JS创建元素节点 document.createElement("p"); 1.2 jQuery创建元素节点 $('<p></p&g ...
- vertical-align 和 img属性 和 鼠标样式
一.vertical-align 一)定义:定义行内元素的基线相对于该所在基线的垂直对齐.(只针对行类块inline/inline-block/<img>,块级不适用!) 二)语法: 三 ...
- Spring Boot [使用 Druid 数据库连接池]
导读 最近一段时间比较忙,以至于很久没有更新Spring Boot系列文章,恰好最近用到Druid, 就将Spring Boot 使用 Druid作为数据源做一个简单的介绍. Druid介绍: Dru ...
- HTML页面中JavaScript能获取到的各种屏幕大小信息
在HTML页面中,通过JavaScript代码访问 window 对象,能够获取到很多表征屏幕大小的信息,下面列举并加以区分. window 对象中的屏幕信息 window.innerheight. ...
- gearman学习笔记1
1.简介 gearman是一个分布式开发框架,适合处理一些必须处理但是不影响主流程的操作,比如保存日志.发送邮件.缩略图片等.最早是基于perl语言的,2008年发布的时候改为C++语言开 ...
- 《深入浅出node.js(朴灵)》【PDF】下载
<深入浅出node.js(朴灵)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062563 内容简介 <深入浅出Node. ...
- 《深入浅出Netty》【PDF】下载
<深入浅出Netty>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062563 内容简介 本文档主要讲述的是深入浅出Netty: ...