bzoj3091 城市旅行 LCT + 区间合并
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=3091
题解
调了整个晚自习才调出来的问题。
乍一看是个 LCT 板子题。
再看一眼还是个 LCT 板子题,不过需要考虑两个区间的结果的合并。
首先考虑到期望可以转化为每一个区间的权值和。那么对于每一个区间,我们维护一个 \(s, ls, rs, sum\) 分别表示整个区间的和,所有前缀的和,所有后缀的和,整个区间的子区间权值和。
那么在区间合并的时候,\(rs\) 会被计算右区间大小次,\(ls\) 会被计算左区间大小次,直接合并就可以了。
合并出 \(ls, rs\) 的时候考虑每一个区间的值的变化即可。
还有一个东西是加标记 \(k\) 对一个区间的影响。设区间长度为 \(len\)。
这个标记对 \(ls, rs\) 的影响很显然,就是每一个前缀的长度和 \(\cdot k\),即 $\frac{len(len+1)k}2 $。
对于 \(sum\) 的影响是所有子区间长度之和。可以从每一个长度的区间数量来求,过程中式子比较繁琐,只给出答案:\(\frac{len(len+1)(len+2)k}6\)。
注意:
- \(ls, rs, sum\) 一类东西太多,不要搞混;
- 不连通时不要操作!!!不连通时不要操作!!!不连通时不要操作!!!
时间复杂度 \(O(m\log n)\)。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
const int N = 50000 + 7;
#define lc c[0]
#define rc c[1]
int n, m;
int a[N];
struct Node {
int c[2], fa, rev, sz, v, add;
ll s, sum, ls, rs;
} t[N];
int st[N];
inline bool isroot(int o) { return t[t[o].fa].lc != o && t[t[o].fa].rc != o; }
inline bool idtfy(int o) { return t[t[o].fa].rc == o; }
inline void connect(int fa, int o, int d) { t[fa].c[d] = o, t[o].fa = fa; }
inline Node add_tag(Node a, int k) {
a.v += k, a.add += k;
a.s += (ll)k * a.sz;
a.sum += (ll)a.sz * (a.sz + 1) * (a.sz + 2) / 6 * k;
a.ls += (ll)a.sz * (a.sz + 1) / 2 * k;
a.rs += (ll)a.sz * (a.sz + 1) / 2 * k;
return a;
}
inline void pushup(int o) {
assert(!t[o].rev && !t[o].add);
const Node &tl = t[t[o].lc], &tr = t[t[o].rc];
t[o].sz = tl.sz + tr.sz + 1;
t[o].s = tl.s + tr.s + t[o].v;
t[o].ls = tl.ls + (tl.s + t[o].v) * (tr.sz + 1) + tr.ls;
t[o].rs = tr.rs + (tr.s + t[o].v) * (tl.sz + 1) + tl.rs;
t[o].sum = tl.sum + tr.sum + (ll)t[o].v * (tl.sz + 1) * (tr.sz + 1) + tl.rs * (tr.sz + 1) + tr.ls * (tl.sz + 1);
}
inline void pushdown(int o) {
if (t[o].rev) {
if (t[o].lc) t[t[o].lc].rev ^= 1, std::swap(t[t[o].lc].lc, t[t[o].lc].rc), std::swap(t[t[o].lc].ls, t[t[o].lc].rs);
if (t[o].rc) t[t[o].rc].rev ^= 1, std::swap(t[t[o].rc].lc, t[t[o].rc].rc), std::swap(t[t[o].rc].ls, t[t[o].rc].rs);
t[o].rev = 0;
}
if (t[o].add) {
if (t[o].lc) t[t[o].lc] = add_tag(t[t[o].lc], t[o].add);
if (t[o].rc) t[t[o].rc] = add_tag(t[t[o].rc], t[o].add);
t[o].add = 0;
}
}
inline void rotate(int o) {
int fa = t[o].fa, pa = t[fa].fa, d1 = idtfy(o), d2 = idtfy(fa), b = t[o].c[d1 ^ 1];
if (!isroot(fa)) t[pa].c[d2] = o; t[o].fa = pa;
connect(o, fa, d1 ^ 1), connect(fa, b, d1);
pushup(fa), pushup(o);
}
inline void splay(int o) {
int x = o, tp = 0;
st[++tp] = x;
while (!isroot(x)) st[++tp] = x = t[x].fa;
while (tp) pushdown(st[tp--]);
while (!isroot(o)) {
int fa = t[o].fa;
if (isroot(fa)) rotate(o);
else if (idtfy(o) == idtfy(fa)) rotate(fa), rotate(o);
else rotate(o), rotate(o);
}
}
inline void access(int o) {
for (int x = 0; o; o = t[x = o].fa)
splay(o), t[o].rc = x, pushup(o);
}
inline void mkrt(int o) {
access(o), splay(o);
t[o].rev ^= 1, std::swap(t[o].lc, t[o].rc), std::swap(t[o].ls, t[o].rs);
}
inline int getrt(int o) {
access(o), splay(o);
while (pushdown(o), t[o].lc) o = t[o].lc;
return splay(o), o;
}
inline void link(int x, int y) {
mkrt(x);
if (getrt(y) != x) t[x].fa = y;
}
inline void cut(int x, int y) {
mkrt(x), access(y), splay(y);
if (t[y].lc == x && !t[x].rc) t[y].lc = t[x].fa = 0, pushup(y);
}
inline void work() {
while (m--) {
int opt, x, y, d;
read(opt), read(x), read(y);
if (opt == 1) cut(x, y);
else if (opt == 2) link(x, y);
else if (opt == 3) read(d), getrt(x) == getrt(y) && (mkrt(x), access(y), splay(y), t[y] = add_tag(t[y], d), 1);
else if (getrt(x) != getrt(y)) puts("-1");
else {
mkrt(x), access(y), splay(y);
ll sum = t[y].sum, cnt = (ll)t[y].sz * (t[y].sz + 1) / 2, p = std::__gcd(sum, cnt);
printf("%lld/%lld\n", sum / p, cnt / p);
}
}
}
inline void init() {
read(n), read(m);
for (int i = 1; i <= n; ++i) read(a[i]);
for (int i = 1; i <= n; ++i) t[i].v = a[i], pushup(i);
int x, y;
for (int i = 1; i < n; ++i) read(x), read(y), link(x, y);
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
bzoj3091 城市旅行 LCT + 区间合并的更多相关文章
- 【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区间合并 前三个 ...
- 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,数学期望)
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 ...
- 【LCT】BZOJ3091 城市旅行
3091: 城市旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1927 Solved: 631[Submit][Status][Discuss ...
- bzoj 3091: 城市旅行 LCT
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=3091 题解: 首先前三个操作就是裸的LCT模板 只考虑第四个操作. 要求我们计算期望,所以我 ...
- BZOJ 3091: 城市旅行 [LCT splay 期望]
3091: 城市旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1454 Solved: 483[Submit][Status][Discuss ...
- BZOJ 3091: 城市旅行 lct 期望 splay
https://www.lydsy.com/JudgeOnline/problem.php?id=3091 https://blog.csdn.net/popoqqq/article/details/ ...
随机推荐
- 三十八、python中反射介绍
一.反射:根据字符串的形式去对象(某个模块)中去操作成员通过字符串的形式,导入模块通过字符串的形式,去模块中寻找指定的函数,并执行 1.__import__:用于字符串的形似执行导入模块 inp=in ...
- 关于db4o的透明激活与激活声明
关于db4o的透明激活与激活声明 有关于透明激活,其介绍可以参看这里:http://www.cnblogs.com/redmoon/archive/2008/02/23/1078619.html 文中 ...
- redis管道pipeline
Jedis jedis = new Jedis("127.0.0.1",6379); Pipeline pipeline = jedis.pipelined(); for(int ...
- oracle-不完全数据库恢复-被动恢复-RMAN-06025/ORA-01190
不完全数据库恢复 到目前为止,前面讨论的都是完全恢复数据库,这是recover database\recover tablespace\recover datafile默认的行为特征. 所谓完全恢复指 ...
- Matlab——数值计算——单个代数方程 代数方程组
方程求解 求解单个代数方程 MATLAB具有求解符号表达式的工具,如果表达式不是一个方程式(不含等 号),则在求解之前函数solve将表达式置成等于0. >> syms a syms b ...
- Jmeter JDBC请求---把数据库结果参数化传递到其他请求
摘要: 最近一个场景进行压力测试:生成商品id进行上下架和购买,记录写脚本的一个过程 1.在商品上架前需要准备商品ID,商品ID生成需要从数据库读取商品类别,从而生成商品ID,下面是从数据库:读取商品 ...
- 链路聚合teaming(网卡绑定技术)2
一.sentos7网卡绑定技术之teaming 这里介绍两种最常见的双网卡绑定模式: (1) roundrobin - 轮询模式 所有链路处于负载均衡状态,这种模式的特点增加了带宽,同时支持容错能力. ...
- 【监控笔记】【3.1】DML(CDC)、DDL(DDL触发器)跟踪数据更改,数据库审计
关键词:数据库审计.DDL审计.DML审计 [监控笔记][3.1]DML(CDC).DDL(DDL触发器)跟踪数据更改 [1]DML(CDC) 2008及以上 https://www.cnblogs. ...
- 最大连续和 Easy
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. F ...
- Docker 镜像的常用操作
镜像作为 Docker 三大核心概念中,最重要的一个关键词,它有很多操作,是您想学习容器技术不得不掌握的.本文将带您一步一步,图文并重,上手操作来学习它. 目录 一 Docker 下载镜像 1.1 下 ...