[BJOI2014]大融合【LCT维护子树信息】

题目链接


  本题保证不会构成环。——此为前提

  然后操作是查询,或者接上一条边(保证之前两点不连通)。

  好了,接下去就是正经事儿了,在此之前,已经有了利用LCT来维护树链信息了,现在只要在这基础上稍加改变,就可以维护某点(也可以是不定根)的子树信息了。

  我们知道,改变LCT树的连接关系,只会在Access、link、cut这几种操作为基础的操作上,因为我们知道LCT树为多个Splay树的森林,他们通过虚链进行连接,先来举例一下Access时候。

Access函数与子树关系

  正常写LCT的Access我们都是用下面这样的做法来写的,但是现在由于要维护子树关系,所以,当我们断开原来的连接(c[x][1]),以及更新新的连接关系(y)进来的时候,我们需要记录这样的关系,以此来达到维护子树信息,所以,稍加改变!

    void Access(int x)
    {
        int y = 0;
        while(x)
        {
            Splay(x);
            c[x][1] = y;
            pushup(x);
            y = x;
            x = fa[x];
        }
    }

  我们对于Access函数进行一定的改变:

    void Access(int x)
    {
        int y = 0;
        while(x)
        {
            Splay(x);
            other[x] += s[c[x][1]];
            other[x] -= s[y];
            c[x][1] = y;
            pushup(x);
            y = x;
            x = fa[x];
        }
    }

  可以知道,other[x]就是维护x点的虚链的信息关系的,因为实链通过pushup来进行维护,所以我们只需要记录other[x]的关系就可以了。

 

Link函数与子树信息关系

    void link(int x, int y)
    {
        makeroot(x);
        if(findroot(y) != x)
        {
            fa[x] = y;
        }
    }

  正常情况,我们就根据这样的方式来进行对链的维护就可以了,但是现在,因为新加进来的节点,一定是通过虚边进行连接的,所以,我们需要直接把它的信息推给它的父亲节点(虚边指向)。

    void link(int x, int y)
    {
        makeroot(x);
        if(findroot(y) != x)
        {
            Splay(y);   /*--Important--*/
            other[y] += s[x];
            fa[x] = y;
            pushup(y);
        }
    }

  记住,这里有个非常重要的东西,就是一定要把y给Splay到它的Splay树的根节点上去,这是因为要让y充分的得到它的子树的信息,并且也是可以维护子树的稳定,然后再让y得到另一棵子树的根节点的指向,同时继承它的答案,因为是通过虚边连接的。

  因为本题不需要cut操作,所以,到这里就完了,当然,cut操作跟Link操作是类似的。

测试数据

7 7
A 1 2
A 1 3
A 2 4
A 5 4
A 6 5
A 7 6
Q 4 5
ans:12 = 4(4) * 3(5)
20 20
A 1 11
A 13 20
A 18 17
A 20 1
A 12 19
A 2 7
A 19 13
A 10 14
A 11 18
A 14 2
A 9 10
A 5 4
A 4 3
A 16 5
A 8 6
A 15 8
A 3 9
A 6 16
A 7 12
Q 10 14
ans:99 = 9(10) * 11(14)

Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, M, Q;
namespace LCT
{
    int fa[maxN], c[maxN][2];
    int r[maxN];
    int s[maxN], other[maxN];
    bool isroot(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; }
    void pushup(int x)
    {
        s[x] = s[c[x][0]] + s[c[x][1]] + other[x] + 1;
    }
    void pushr(int x) { swap(c[x][0], c[x][1]); r[x] ^= 1; }
    void pushdown(int x)
    {
        if(r[x])
        {
            if(c[x][0]) pushr(c[x][0]);
            if(c[x][1]) pushr(c[x][1]);
            r[x] = 0;
        }
    }
    void Rotate(int x)
    {
        int y = fa[x], z = fa[y], k = c[y][1] == x;
        if(!isroot(y)) c[z][c[z][1] == y] = x;
        fa[x] = z;
        c[y][k] = c[x][k ^ 1];
        fa[c[x][k ^ 1]] = y;
        c[x][k ^ 1] = y;
        fa[y] = x;
        pushup(y);
        pushup(x);
    }
    int Stap[maxN];
    void Splay(int x)
    {
        int y = x, z = 0;
        Stap[++z] = y;
        while(!isroot(y)) Stap[++z] = y = fa[y];
        while(z) pushdown(Stap[z--]);
        while(!isroot(x))
        {
            y = fa[x]; z = fa[y];
            if(!isroot(y)) (c[z][0] == y) ^ (c[y][0] == x) ? Rotate(x) : Rotate(y);
            Rotate(x);
        }
    }
    void Access(int x)
    {
        int y = 0;
        while(x)
        {
            Splay(x);
            other[x] += s[c[x][1]];
            other[x] -= s[y];
            c[x][1] = y;
            pushup(x);
            y = x;
            x = fa[x];
        }
    }
    void makeroot(int x)
    {
        Access(x);
        Splay(x);
        pushr(x);
    }
    int findroot(int x)
    {
        Access(x);
        Splay(x);
        while(c[x][0])
        {
            pushdown(x);
            x = c[x][0];
        }
        Splay(x);
        return x;
    }
    void Split(int x, int y)
    {
        makeroot(x);
        Access(y);
        Splay(y);
    }
    void link(int x, int y)
    {
        makeroot(x);
        if(findroot(y) != x)
        {
            Splay(y);   /*--Important--*/
            other[y] += s[x];
            fa[x] = y;
            pushup(y);
        }
    }
    void cut(int x, int y)
    {
        makeroot(x);
        if(findroot(y) != x || fa[y] != x || c[y][0]) return;
        fa[y] = c[x][1] = 0;
        pushup(x);
    }
};
using namespace LCT;
int main()
{
    scanf("%d%d", &N, &Q);
    s[0] = 0; other[0] = 0;
    for(int i=1; i<=N; i++) { s[i] = 1; other[i] = 0; }
    char op[3]; int u, v;
    for(int i=1; i<=Q; i++)
    {
        scanf("%s%d%d", op, &u, &v);
        if(op[0] == 'A')
        {
            link(u, v);
        }
        else
        {
            Split(u, v);
            printf("%lld\n", 1LL * (other[u] + 1) * (other[v] + 1));
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值