【BZOJ2157】旅游(树剖,LCT)

Description

Ray 乐忠于旅游,这次他来到了T 城。T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径。换句话说, T 城中只有N − 1 座桥。Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁。于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的。有时,Ray 看待同一座桥的心情也会发生改变。现在,Ray 想让你帮他计算从u 景点到v 景点能获得的总愉悦度。有时,他还想知道某段路上最美丽的桥所提供的最大愉悦度,或是某段路上最糟糕的一座桥提供的最低愉悦度。


Solution

树剖裸题,但是我是来练LCT的。。。
模板题其实没什么好说的,就是有点坑。
一开始WA了好久,最后HYJdalao帮我查错,发现maintain时,不能写成mn[t]=min(min(val[t], mn[ch[t][0]]), mn[ch[t][1]]),因为权值可能存在负的,需要特判。


Code

/************************************************
 * Au: Hany01
 * Date: Apr 12th, 2018
 * Prob: BZOJ2157
 * Email: [email protected]
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 40000;

int n;

struct LCT
{
    int ch[maxn][2], fa[maxn], mx[maxn], mn[maxn], sum[maxn], val[maxn], rev[maxn], op[maxn];

#define isrt(t) (ch[fa[t]][0] != t && ch[fa[t]][1] != t)
#define dir(t)  (ch[fa[t]][0] != t)

    inline void maintain(int t) {
        mn[t] = min(mn[ch[t][0]], mn[ch[t][1]]);
        mx[t] = max(mx[ch[t][0]], mx[ch[t][1]]);
        if (t > n) chkmax(mx[t], val[t]), chkmin(mn[t], val[t]);
        sum[t] = sum[ch[t][0]] + sum[ch[t][1]] + val[t];
    }

    inline void getrev(int t) {
        mn[t] *= -1, mx[t] *= -1, sum[t] *= -1, val[t] *= -1, op[t] ^= 1, swap(mx[t], mn[t]);
    }
    inline void change(int t) {
        rev[t] ^= 1, swap(ch[t][0], ch[t][1]);
    }

    inline void pushdown(int t) {
        if (op[t]) {
            if (ch[t][0]) getrev(ch[t][0]);
            if (ch[t][1]) getrev(ch[t][1]);
            op[t] = 0;
        }
        if (rev[t]) {
            rev[t] = 0;
            if (ch[t][0]) change(ch[t][0]);
            if (ch[t][1]) change(ch[t][1]);
        }
    }

    inline void rotate(int u) {
        int f = fa[u], gf = fa[f], d = dir(u);
        fa[ch[f][d] = ch[u][d ^ 1]] = f, fa[u] = gf;
        if (!isrt(f)) ch[gf][dir(f)] = u;
        ch[fa[f] = u][d ^ 1] = f,
        maintain(f), maintain(u);
    }

    int stk[maxn], tp;
    inline void splay(int u) {
        stk[tp = 1] = u;
        for (int t = u; !isrt(t); t = fa[t]) stk[++ tp] = fa[t];
        while (tp) pushdown(stk[tp --]);
        for ( ; !isrt(u); rotate(u)) if (!isrt(fa[u])) rotate(dir(u) == dir(fa[u]) ? fa[u] : u);
    }

    inline void access(int u) {
        for (int t = 0; u; t = u, u = fa[u]) splay(u), ch[u][1] = t, maintain(u);
    }

    inline void makeroot(int u) { access(u), splay(u), change(u); }

    inline void link(int u, int v) { makeroot(u), fa[u] = v; }

    inline void split(int u, int v) { makeroot(u), access(v), splay(v); }

}lct;

int main()
{
#ifdef hany01
    File("bzoj2157");
#endif

    static int x, y;
    static char ty[5];

    n = read();
    Set(lct.mn, 127), Set(lct.mx, 128);
    For(i, 1, n - 1)
        x = read() + 1, y = read() + 1, lct.val[n + i] = read(),
        lct.link(n + i, x), lct.link(n + i, y);

    for (static int m = read(); m --; )
    {
        scanf("%s", ty), x = read(), y = read();
        if (*ty == 'S') lct.split(x + 1, y + 1), printf("%d\n", lct.sum[y + 1]);
        else if (*ty == 'M' && *(ty + 1) == 'A') lct.split(x + 1, y + 1), printf("%d\n", lct.mx[y + 1]);
        else if (*ty == 'M' && *(ty + 1) == 'I') lct.split(x + 1, y + 1), printf("%d\n", lct.mn[y + 1]);
        else if (*ty == 'N') lct.split(x + 1, y + 1), lct.getrev(y + 1);
        else lct.access(x + n), lct.splay(x + n), lct.val[x + n] = y, lct.maintain(x + n);
    }

    return 0;
}
//承恩不在貌,教妾若为容。
//    -- 杜荀鹤《春宫怨》

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/79915884