早上考试想用\(LCT\)维护联通块\(size\),现在才发现\(LCT\)的\(size\)有虚实之分

\(Link\)与\(Acess\)中虚实变,干他丫的

\(Splay\)中只是相对关系,没有虚实变,因此不搞它

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b)) #define ON_DEBUGG #ifdef ON_DEBUGG #define D_e_Line printf("-----------\n")
#define D_e(x) std::cout << (#x) << " : " <<x << "\n"
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define Pause() system("pause")
#include <ctime>
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", clock() * 1000.0 / CLOCKS_PER_SEC) #else #define D_e_Line ;
#define D_e(x) ;
#define FileOpen() ;
#define FilSave ;
#define Pause() ;
#define TIME() ; #endif struct ios {
template<typename ATP> ios& operator >> (ATP &x) {
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x *= f;
return *this;
}
}io; using namespace std; template<typename ATP> inline ATP Min(ATP a, ATP b) {
return a < b ? a : b;
}
template<typename ATP> inline ATP Max(ATP a, ATP b) {
return a > b ? a : b;
}
template<typename ATP> inline ATP Abs(ATP a) {
return a < 0 ? -a : a;
} const int N = 1e5 + 7; struct LCT {
int ch[2], fa, siz, sz; // siz : true edge, sz : virtual edge
bool rev;
} t[N];
#define ls t[u].ch[0]
#define rs t[u].ch[1]
inline int Ident(int &u) {
return t[t[u].fa].ch[1] == u;
} inline bool IsRoot(int &u) {
return t[t[u].fa].ch[0] != u && t[t[u].fa].ch[1] != u;
} inline void Pushup(int &u) {
if(u) t[u].siz = t[ls].siz + t[rs].siz + t[u].sz + 1;
// left + right + virtual + self
} inline void Pushrev(int u) {
Swap(ls, rs);
t[u].rev ^= 1;
} inline void Pushdown(int u) {
if(!t[u].rev) return;
if(ls) Pushrev(ls);
if(rs) Pushrev(rs);
t[u].rev = 0;
} inline void Rotate(int x) {
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[x].fa = z; if(!IsRoot(y)) t[z].ch[Ident(y)] = x;
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
} int sta[N], top; inline void Splay(int u) {
int x = u;
while(!IsRoot(u)){
sta[++top] = u;
u = t[u].fa;
}
sta[++top] = u;
while(top) Pushdown(sta[top--]);
while(!IsRoot(x)){
int y = t[x].fa;
if(!IsRoot(y)){
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
}
Rotate(x);
}
Pushup(x);
} inline void Access(int u) {
for(register int v = 0; u; v = u, u = t[u].fa){
Splay(u);
t[u].sz += t[rs].siz - t[v].siz;
t[u].ch[1] = v;
Pushup(u);
}
} inline void MakeRoot(int u) {
Access(u);
Splay(u);
Pushrev(u);
} inline void Split(int u, int v) {
MakeRoot(u);
Access(v);
Splay(v);
} inline void Link(int u, int v) {
Split(u, v);
t[u].fa = v;
t[v].sz += t[u].siz;
}
inline long long Query(int u, int v) {
Split(u, v);
return 1ll * (t[u].sz + 1) * (t[v].sz + 1);
} //inline void Link(int u, int v) {
// MakeRoot(u);
// MakeRoot(v);
// t[u].fa = v;
// t[v].sz += t[u].siz;
// Pushup(v);
//}
//inline long long Query(int u, int v) {
// MakeRoot(u);
// MakeRoot(v);
// return 1ll * (t[u].sz + 1) * (t[v].sz + 1);
//} char opt[13];
int main() {
int n, m;
io >> n >> m;
R(i,1,n){
t[i].siz = 1;
t[i].sz = 0;
}
while(m--){
scanf("%s", opt + 1);
int x, y;
io >> x >> y;
if(opt[1] == 'A'){
Link(x, y);
}
else{
printf("%lld\n", Query(x, y));
}
}
return 0;
}

最新文章

  1. 漫步ASP.NET MVC的处理管线
  2. HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法
  3. DSL 或者说是抽象 或者说是沉淀 ,我看到的不错的一篇文章
  4. 常见的HTTP返回码如4xx, 5xx
  5. dojo/query源码解析
  6. 使用jquery的append(content)方法的注意事项
  7. SQL Server中常用的SQL语句
  8. ibatis 到 MyBatis区别
  9. Angularjs总结(二)过滤器使用
  10. ecshop数据表
  11. HTML, CSS学习笔记(完整版)
  12. Request对象 --web浏览器向web服务端的请求
  13. docker study
  14. SAE设置记录:修改config.yaml实现地址重写和修改固定链接
  15. [Machine Learning] 深度学习中消失的梯度
  16. linux根目录扩容
  17. The 16th Zhejiang provincial collegiate programming contest
  18. UCloud首尔机房整体热迁移是这样炼成的
  19. npm 构建时,次要版本变化引起的问题
  20. IE hack 汇总

热门文章

  1. node-sass,sass-loader和node之间的关系
  2. mac安装git、node
  3. 用树莓派USB摄像头做个监控
  4. 2021.06.19【NOIP提高B组】模拟 总结
  5. 使用 content-visibility 优化渲染性能
  6. 快速保存Win10锁屏壁纸,收获美丽瞬间
  7. 互联网研发效能之去哪儿网(Qunar)核心领域DevOps落地实践
  8. ConcurrentHashMap树化链表treeifyBin
  9. go grpc: connection reset by peer 的一种解决方案
  10. zookeeper Caused by: java.lang.IllegalArgumentException: myid file is missing