您的位置:首页 > 运维架构

[BZOJ]4530 [BJOI2014] 大融合 LCT维护子树信息

2017-11-28 10:14 531 查看

4530: [Bjoi2014]大融合

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 525  Solved: 311

[Submit][Status][Discuss]

Description

小强要在N个孤立的星球上建立起一套通信系统。这套通信系统就是连接N个点的一个树。
这个树的边是一条一条添加上去的。在某个时刻,一条边的负载就是它所在的当前能够
联通的树上路过它的简单路径的数量。



例如,在上图中,现在一共有了5条边。其中,(3,8)这条边的负载是6,因
为有六条简单路径2-3-8,2-3-8-7,3-8,3-8-7,4-3-8,4-3-8-7路过了(3,8)。
现在,你的任务就是随着边的添加,动态的回答小强对于某些边的负载的
询问。

Input

第一行包含两个整数N,Q,表示星球的数量和操作的数量。星球从1开始编号。
接下来的Q行,每行是如下两种格式之一:
A x y 表示在x和y之间连一条边。保证之前x和y是不联通的。
Q x y 表示询问(x,y)这条边上的负载。保证x和y之间有一条边。
1≤N,Q≤100000

Output

对每个查询操作,输出被查询的边的负载。

Sample Input

8 6

A 2 3

A 3 4

A 3 8

A 8 7

A 6 5

Q 3 8

Sample Output

6

HINT

Source

鸣谢佚名上传

[Submit][Status][Discuss]

HOME Back

题解

我们可以发现这道题需要维护子树信息~ 因为答案就是子树大小乘以联通块其他的size. 因为要加边想到LCT... 考虑如何维护子树信息. 对于每个点x, 维护一个总信息sum, 在splay update的时候更新 . sum等于splay子树的信息+虚子树的信息. 那么将一个点access之后, 那么他的虚子树信息+自己信息就是子树信息. 虚子树信息只在link和access中改变.
access加减即可(详见代码), 但link的时候要注意要将x makeroot还要将y makeroot(或者access加splay). 这样就只会是y的虚子树信息改变, 不会影响其他点.

统计答案的时候split改了一下, access y再splay x, 可以自己思考一下为什么.

#include<bits/stdc++.h>
#define ls c[x][0]
#define rs c[x][1]
#define Boc register char
#define Acce register int
using namespace std;
const int maxn = 2e5 + 5;
char ss[2];
bool rev[maxn];
int n, Q, top;
int fa[maxn], c[maxn][2], s[maxn], em[maxn], sum[maxn];
inline const int read()
{
Acce x = 0;
Boc ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x;
}
inline bool isroot(int x)
{ return c[fa[x]][0] != x && c[fa[x]][1] != x;}
inline void update(int x)
{ sum[x] = sum[c[x][0]] + sum[c[x][1]] + em[x] + 1; }
inline void rever(int x)
{ swap(ls, rs), rev[x] ^= 1; }
inline void pushdown(int x)
{
if (rev[x])
rever(ls), rever(rs), rev[x] = 0;
}
inline void rotate(int x)
{
int y = fa[x], z = fa[y];
int l = (c[y][1] == x), r = l ^ 1;
if (!isroot(y)) c[z][c[z][1] == y] = x;
fa[x] = z, fa[y]= x, fa[c[x][r]] = y;
c[y][l] = c[x][r], c[x][r] = y;
update(y), update(x);
}
inline void splay(int x)
{
top = 0;
s[++ top] = x;
for (int i = x; !isroot(i); i = fa[i]) s[++ top] = fa[i];
for (int i = top; i; -- i) pushdown(s[i]);
for (int f; !isroot(x); rotate(x))
if(!isroot(f = fa[x]))
rotate((c[fa[f]][0] == f ^ c[f][0] == x) ? x : f);
}
inline void access(int x)
{
for (int t = 0; x; x = fa[x])
splay(x), em[x] += sum[c[x][1]] - sum[t], c[x][1] = t, update(x), t = x;
}
inline void makeroot(int x)
{ access(x), splay(x), rever(x); }
inline void link(int x, int y)
{
makeroot(x), makeroot(y);
fa[x] = y, em[y] += sum[x], update(y);
}
inline void split(int x, int y)
{
makeroot(x);
access(y), splay(x);
}
int main()
{
int x, y;
n = read(), Q = read();
for (Acce i = 1; i <= n; ++ i) sum[i] = 1;
for (Acce i = 1; i <= Q; ++ i)
{
scanf("%s", ss);
x = read(), y = read();
if (ss[0] == 'A') link(x, y);
else
{
split(x, y);
printf("%lld\n", 1ll * (em[y] + 1) * (sum[x] - em[y] - 1));
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: