【bzoj3091】 城市旅行
http://www.lydsy.com/JudgeOnline/problem.php?id=3091 (题目链接)
题意
给出一棵无根树,维护四个操作。link,cut,路径加法,路径期望查询。
Solution
右转题解→_→:PoPoQQQ
对于无法直接维护的值,我们可以考虑做差,或者是用别的比较好维护的值来加减乘除得到。
细节
全程LL,先翻转再打标记。
代码
// bzoj3091
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
using namespace std; const int maxn=50010;
int n,m,fa[maxn];
struct node {
int son[2];
LL sum,tag,rev,ans,val,lsum,rsum,size;
int& operator [] (int x) {return son[x];}
}tr[maxn]; LL gcd(LL a,LL b) {return b==0 ? a : gcd(b,a%b);}
namespace LCT {
void reverse(int k) {
swap(tr[k][0],tr[k][1]);
swap(tr[k].lsum,tr[k].rsum);
tr[k].rev^=1;
}
void add(int k,LL val) {
tr[k].val+=val;
tr[k].sum+=tr[k].size*val;
tr[k].tag+=val;
tr[k].ans+=tr[k].size*(tr[k].size+1)*(tr[k].size+2)/6*val;
tr[k].lsum+=tr[k].size*(tr[k].size+1)/2*val;
tr[k].rsum+=tr[k].size*(tr[k].size+1)/2*val;
}
void pushdown(int k) {
if (tr[fa[k]][0]==k || tr[fa[k]][1]==k) pushdown(fa[k]);
int l=tr[k][0],r=tr[k][1];
if (tr[k].rev) {
if (l) reverse(l);
if (r) reverse(r);
tr[k].rev^=1;
}
if (tr[k].tag) {
if (l) add(l,tr[k].tag);
if (r) add(r,tr[k].tag);
tr[k].tag=0;
}
}
void pushup(int k) {
int l=tr[k][0],r=tr[k][1];
tr[k].size=tr[l].size+tr[r].size+1;
tr[k].lsum=tr[l].lsum+(tr[l].size+1)*tr[k].val+tr[r].lsum+tr[r].sum*(tr[l].size+1);
tr[k].rsum=tr[r].rsum+(tr[r].size+1)*tr[k].val+tr[l].rsum+tr[l].sum*(tr[r].size+1);
tr[k].sum=tr[l].sum+tr[r].sum+tr[k].val;
tr[k].ans=tr[l].ans+tr[r].ans+tr[l].lsum*(tr[r].size+1)+tr[r].rsum*(tr[l].size+1)+tr[k].val*(tr[l].size+1)*(tr[r].size+1);
}
void rotate(int x) {
int y=fa[x],z=fa[y],l,r;
l=tr[y][1]==x;r=l^1;
if (tr[z][0]==y || tr[z][1]==y) tr[z][tr[z][1]==y]=x;
fa[tr[x][r]]=y;fa[x]=z;fa[y]=x;
tr[y][l]=tr[x][r];tr[x][r]=y;
pushup(y);pushup(x);
}
void splay(int x) {
pushdown(x);
while (tr[fa[x]][0]==x || tr[fa[x]][1]==x) {
int y=fa[x],z=fa[y];
if (tr[z][0]==y || tr[z][1]==y) {
if (tr[z][0]==y ^ tr[y][0]==x) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x) {
for (int y=0;x;y=x,x=fa[x]) splay(x),tr[x][1]=y,pushup(x);
}
void makeroot(int x) {
access(x);splay(x);reverse(x);
}
void link(int x,int y) {
makeroot(x);fa[x]=y;
}
void cut(int x,int y) {
makeroot(x);access(y);splay(y);
if (tr[y][0]==x && tr[x][1]==0)
tr[y][0]=0,fa[x]=0,pushup(y);
}
void modify(int x,int y,LL val) {
makeroot(x);access(y);splay(y);add(y,val);
}
int find(int x) {
return fa[x] ? find(fa[x]) : x;
}
void query(int x,int y) {
makeroot(x);access(y);splay(y);
LL A=tr[y].ans,B=tr[y].size*(tr[y].size+1)/2;
LL D=gcd(A,B);
printf("%lld/%lld\n",A/D,B/D);
}
}
using namespace LCT; int main() {
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%lld",&tr[i].val);
for (int u,v,i=1;i<n;i++) {
scanf("%d%d",&u,&v);
link(u,v);
}
for (int op,x,y,d,i=1;i<=m;i++) {
scanf("%d%d%d",&op,&x,&y);
if (op==1) if (find(x)==find(y)) cut(x,y);
if (op==2) if (find(x)!=find(y)) link(x,y);
if (op==3) {scanf("%d",&d);if (find(x)==find(y)) modify(x,y,d);}
if (op==4) if (find(x)==find(y)) query(x,y);else puts("-1");
}
return 0;
}
【bzoj3091】 城市旅行的更多相关文章
- 【LCT】BZOJ3091 城市旅行
3091: 城市旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1927 Solved: 631[Submit][Status][Discuss ...
- BZOJ3091 城市旅行 LCT
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3091 题意概括 鉴于本人语文不好,此题的描述原题很清晰,废话不多,请看原题. 可怕,原题是图片,不 ...
- 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区间信息合并
题目描述 输入 输出 样例输入 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,数学期望)
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 + 区间合并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3091 题解 调了整个晚自习才调出来的问题. 乍一看是个 LCT 板子题. 再看一眼还是个 LC ...
- 【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-- 不过不得不说我这暴力写的挺好看的.删边模仿链表删边,加边的时 ...
随机推荐
- 洛谷 P3302 [SDOI2013]森林
->题目链接 题解: #include<queue> #include<cstdio> #include<cstring> #include<iostr ...
- 利用Python统计微信联系人男女比例以及简单的地区分布
寒暄的话不多说,直接进入主题. 运行效果图: [准备环境] Python版本:v3.5及其以上 开发工具:随意,此处使用Pycharm [依赖包] 1.itchat (CMD运行:pip instal ...
- P2371 [国家集训队]墨墨的等式
膜意义下最短路. 把最小的\(a\)抠出来,作为模数\(mod\),然后建点编号为\(0\)到\(mod-1\),对每个数\(a\)连边\((i,(a+i)\mod mod)\)点\(i\)的最短路就 ...
- 我的SQL SERVER数据库会装满吗?
概述 今天有个客户问我一个蛮有意思的问题.我使用的SQL SERVER 2008数据库,目前数据库130多G,其中某个表的记录条数就有3亿1千多万,占用了50多G.那SQL SERVER 数据库中的表 ...
- LeetCode Letter Combinations of a Phone Number (DFS)
题意 Given a digit string, return all possible letter combinations that the number could represent. A ...
- numpy 初识(一)
基本操作: 读取文件(与pandas读取csv相似): import numpy numpy.genfromtxt("word.txt", delimiter=',', dtype ...
- TimelineJS JSON 数据格式 - 译文 [原创]
TimelineJS 是用于绘制时间轴的 Javascript 开源脚本,目前是 TimelineJS3 版.参阅 https://github.com/NUKnightLab/TimelineJS3 ...
- xss基础
0x0 定义 总结: (1) 在页面显示 (2) 用户可控 满足以上两点就有可能存在xss 0x1反射型 0x2存储型 0x3 DOM型 与反射型相似 也是从get等参数传参 但 反射 ...
- 《Linux内核分析》第三周
[李行之原创作品 转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] <Linux内 ...
- Linux内核分析作业第二周
操作系统是如何工作的 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.函数调用堆栈 1.计算机工作三 ...