JZOJ 3766【BJOI2014】大融合(lct维护子树大小)
2017-06-26 19:09
513 查看
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之间有一条边。
Output:
对每个查询操作,输出被查询的边的负载。Sample Input:
8 6A 2 3
A 3 4
A 3 8
A 8 7
A 6 5
Q 3 8
Sample Output:
6Data Constraint:
对于40%的数据,N,Q≤1000对于100%的数据,1≤N,Q≤100000
题目大意:
动态的加边,形成森林,其间询问经过一条边的路径有多少条?题解:
设这条边的两个端点是x,y路径数:把这条边割掉后,x所在树的大小乘上y所在树的大小。
由于这道题并不是强制在线,可以先把树建出来,然后用树链剖分。
某ifleaking说可以先求出树的欧拉序列,倒着割边,用splay维护。
当然这些都是离线算法,是否有优秀的在线算法呢?
喜闻乐见lct。
lct如何维护子树大小:
一个点x,它的子树大小等于在splay中,它右节点的子树大小加上所有非偏爱边指向它的点的子树大小。
可以把这两个分别维护。
那么在access时,会有所改变,注意处理。
link的时候,也不能像简单的lct那样 makeroot(x); pf[x] = y;
因为y和它的祖先的子树大小变了,还需要access(y), splay(y, 0),这样才能修改。
Code:
#include<cstdio> #include<cstring> #include<algorithm> #define fo(i, x, y) for(int i = x; i <= y; i ++) using namespace std; const int Maxn = 100005; int t[Maxn][2], fa[Maxn], pf[Maxn], dd[Maxn]; struct node { int rev, s1, s2, s3; }a[Maxn]; int lr(int x) {return t[fa[x]][1] == x;} void update(int x) { if(x) a[x].s2 = a[t[x][1]].s3 + a[x].s1 + 1, a[x].s3 = a[t[x][0]].s3 + a[t[x][1]].s3 + a[x].s1 + 1; } void fan(int x) {if(x) swap(t[x][0], t[x][1]), a[x].rev ^= 1, update(x);} void down(int x) {if(a[x].rev) fan(t[x][0]), fan(t[x][1]), a[x].rev = 0;} void rotate(int x) { int y = fa[x], k = lr(x); t[y][k] = t[x][!k]; if(t[x][!k]) fa[t[x][!k]] = y; fa[x] = fa[y]; if(fa[y]) t[fa[y]][lr(y)] = x; fa[y] = x; t[x][!k] = y; pf[x] = pf[y]; update(y); update(x); } void xc(int x) { for(; x; x = fa[x]) dd[++ dd[0]] = x; for(; dd[0]; dd[0] --) down(dd[dd[0]]); } void splay(int x, int y) { xc(x); while(fa[x] != y) { if(fa[fa[x]] != y) if(lr(x) == lr(fa[x])) rotate(fa[x]); else rotate(x); rotate(x); } } void access(int x) { for(int y = 0; x; update(x), y = x, x = pf[x]) { splay(x, 0), fa[t[x][1]] = 0, pf[t[x][1]] = x; a[x].s1 += a[t[x][1]].s3; t[x][1] = y, fa[y] = x, pf[y] = 0; a[x].s1 -= a[y].s3; } } void makeroot(int x) { access(x); splay(x, 0); fan(x); } void link(int x, int y) { makeroot(x); pf[x] = y; access(y); splay(y, 0); a[y].s1 += a[x].s3; update(y); } int n, Q, x, y; int main() { scanf("%d", &n); fo(i, 1, n) a[i].s2 = 1; for(scanf("%d", &Q); Q; Q --) { char c = ' '; for(; c != 'A' && c != 'Q'; c = getchar()); scanf("%d %d", &x, &y); if(c == 'A') { link(x, y); } else { makeroot(x); access(y); splay(x, 0); splay(y, x); long long s = a[x].s2; printf("%lld\n" ,(long long)(s - a[y].s2) * a[y].s2); } } }
相关文章推荐
- bzoj4530 [Bjoi2014]大融合(LCT维护子树大小)
- 【LCT维护子树信息】BZOJ4530(Bjoi2014)[大融合]题解
- LCT维护子树信息(BZOJ4530:[BJOI2014]大融合)
- BZOJ4530:[BJOI2014]大融合(LCT维护子树)
- bzoj4530 [Bjoi2014]大融合 (LCT维护子树信息)
- bzoj 4530: [Bjoi2014]大融合 lct维护子树信息
- [BZOJ]4530 [BJOI2014] 大融合 LCT维护子树信息
- 【BZOJ4530】[Bjoi2014]大融合 LCT维护子树信息
- BZOJ4530 BJOI 2014 大融合 LCT维护子树信息
- BZOJ 4530: [Bjoi2014]大融合 lct维护子树信息
- [BZOJ4530][Bjoi2014][LCT维护子树信息]大融合
- 【bzoj4530】[Bjoi2014]大融合 LCT维护子树信息
- BZOJ 4530 [Bjoi2014]大融合 LCT维护子树信息
- bzoj4530 [Bjoi2014]大融合(lct维护虚边信息)
- JZOJ 3766. 【BJOI2014】大融合
- 【BJOI2014】大融合 jzoj 3766 树链剖分+并查集
- 【bzoj 4530】大融合(LCT维护子树信息)
- 【bzoj4530】大融合(LCT的子树维护)
- BZOJ.4530.[BJOI2014]大融合(LCT)
- [BZOJ4530]-大融合-LCT维护子树信息