其他分享
首页 > 其他分享> > LCT 板子

LCT 板子

作者:互联网

namespace LCT {
int f[N], c[N][2], s[N], st[N];
bool r[N];
inline bool nroot(int);
inline void pushup(int);
inline void pushr(int);
inline void pushdown(int);
inline void rotate(int);
inline void splay(int);
inline void access(int);
inline void makeroot(int);
inline int findroot(int);
inline void split(int, int); // split(x, y) 表示取出 x,y 的路径
inline void link(int, int);
inline void cut(int, int);
}
using LCT::s;

namespace LCT {
#define rc c[x][1]
#define lc c[x][0]
inline bool nroot(int x) { return c[f[x]][0] == x || c[f[x]][1] == x; }
inline void pushup(int x) { s[x] = s[lc] + s[rc]+????; } // s 数组是记录结果。
inline void pushr(int x) {int t = lc; lc = rc; rc = t; r[x] ^= 1;}
inline void pushdown(int x) {
  if (r[x]) {
    if (lc) pushr(lc);
    if (rc) pushr(rc);
    r[x] = 0;
  }
}
inline void rotate(int x) {
  int y = f[x], z = f[y], k = c[y][1] == x, w = c[x][!k];
  if (nroot(y)) c[z][c[z][1] == y] = x; c[x][!k] = y; c[y][k] = w;
  if (w)f[w] = y; f[y] = x; f[x] = z;
  pushup(y);
}
inline void splay(int x) {
  int y = x, z = 0;
  st[++z] = y; 
  while (nroot(y)) st[++z] = y = f[y];
  while (z) pushdown(st[z--]);
  while (nroot(x)) {
    y = f[x]; z = f[y];
    if (nroot(y)) rotate((c[y][0] == x) ^ (c[z][0] == y) ? x : y);
    rotate(x);
  }
  pushup(x);
}
inline void access(int x) {
  for (int y = 0; x; x = f[y = x])
    splay(x), rc = y, pushup(x);
}
inline void makeroot(int x) { access(x); splay(x); pushr(x); }
inline int findroot(int x) {
  access(x); splay(x);
  while (lc) pushdown(x), x = lc;
  splay(x);
  return x;
}
inline void split(int x, int y) { makeroot(x); access(y); splay(y); }
inline void link(int x, int y) {
  makeroot(x);
  if (findroot(y) != x)f[x] = y;
}
inline void cut(int x, int y) {
  makeroot(x);
  if (findroot(y) == x && f[y] == x && !c[y][0]) {
    f[y] = c[x][1] = 0;
    pushup(x);
  }
}
}

标签:LCT,lc,int,void,板子,splay,rc,inline
来源: https://www.cnblogs.com/Oier-GGG/p/16462568.html