博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Bzoj2959: 长跑
阅读量:5809 次
发布时间:2019-06-18

本文共 2892 字,大约阅读时间需要 9 分钟。

题面

Sol

\(LCT+\)并查集维护双联通分量,每次加边\(x,y\),如果已经相连就把这条路径缩成一个点,赋上权值

\(LCT\)调用\(fa\)时一定要在并查集中\(find\)一下
细节很多,常数很大

# include 
# define IL inline# define RG register# define ls ch[0][x]# define rs ch[1][x]# define Fill(a, b) memset(a, b, sizeof(a))using namespace std;typedef long long ll;const int _(2e5 + 10);IL ll Read(){ RG char c = getchar(); RG ll x = 0, z = 1; for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1; for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); return x * z;}int n, Q, ch[2][_], fa[_], sum[_], rev[_], S[_], val[_], scc[_], bel[_], w[_];IL int Find(RG int x){ return x == scc[x] ? x : scc[x] = Find(scc[x]); }IL int _Find(RG int x){ return x == bel[x] ? x : bel[x] = _Find(bel[x]); }IL bool Son(RG int x){ return ch[1][Find(fa[x])] == x; }IL bool Isroot(RG int x){ RG int pa = Find(fa[x]); return ch[0][pa] != x && ch[1][pa] != x; }IL void Reverse(RG int x){ if(!x) return; swap(ls, rs); rev[x] ^= 1; }IL void Update(RG int x){ sum[x] = sum[ls] + sum[rs] + val[x]; }IL void Pushdown(RG int x){ if(!rev[x]) return; Reverse(ls); Reverse(rs); rev[x] = 0; }IL void Rotate(RG int x){ RG int y = Find(fa[x]), z = Find(fa[y]), c = Son(x); if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z; ch[c][y] = ch[!c][x]; fa[ch[c][y]] = y; ch[!c][x] = y; fa[y] = x; Update(y);}IL void Splay(RG int x){ S[S[0] = 1] = x; for(RG int y = x; !Isroot(y); y = Find(fa[y])) S[++S[0]] = Find(fa[y]); while(S[0]) Pushdown(S[S[0]--]); for(RG int y = Find(fa[x]); !Isroot(x); Rotate(x), y = Find(fa[x])) if(!Isroot(y)) Son(x) ^ Son(y) ? Rotate(x) : Rotate(y); Update(x);}IL void Access(RG int x){ for(RG int y = 0; x; y = x, x = Find(fa[x])) Splay(x), ch[1][x] = y, Update(x); }IL void Makeroot(RG int x){ Access(x); Splay(x); Reverse(x); }IL void Link(RG int x, RG int y){ Makeroot(x); fa[x] = y; }IL void Split(RG int x, RG int y){ Makeroot(x); Access(y); Splay(y); }IL void Dfs(RG int x, RG int y){ if(!x) return; scc[Find(x)] = y; Dfs(ls, y); Dfs(rs, y); }int main(RG int argc, RG char* argv[]){ n = Read(); Q = Read(); for(RG int i = 1; i <= n; ++i) bel[i] = scc[i] = i, w[i] = val[i] = Read(); while(Q--){ RG int p = Read(), a = Read(), b = Read(), fx; if(p == 1){ a = Find(a); b = Find(b); if(_Find(a) != _Find(b)) Link(a, b), bel[_Find(a)] = _Find(b); else{ Split(a, b); val[b] = sum[b]; Dfs(b, b); ch[0][b] = ch[1][b] = 0; } } else if(p == 2) fx = Find(a), Makeroot(fx), val[fx] += b - w[a], w[a] = b, Update(fx); else{ a = Find(a); b = Find(b); if(_Find(a) != _Find(b)){ puts("-1"); continue; } Split(a, b); printf("%d\n", sum[b]); } } return 0;}

转载于:https://www.cnblogs.com/cjoieryl/p/8331356.html

你可能感兴趣的文章
白话算法(7) 生成全排列的几种思路(二) 康托展开
查看>>
d3 v4实现饼状图,折线标注
查看>>
微软的云策略
查看>>
Valid Parentheses
查看>>
【ES6】数值的扩展
查看>>
性能测试之稳定性测试
查看>>
ES6的 Iterator 遍历器
查看>>
2019届高二(下)半期考试题(文科)
查看>>
【REDO】删除REDO LOG重做日志组后需要手工删除对应的日志文件(转)
查看>>
nginx 301跳转到带www域名方法rewrite(转)
查看>>
AIX 配置vncserver
查看>>
windows下Python 3.x图形图像处理库PIL的安装
查看>>
【IL】IL生成exe的方法
查看>>
network
查看>>
SettingsNotePad++
查看>>
centos7安装cacti-1.0
查看>>
3个概念,入门 Vue 组件开发
查看>>
没有JS的前端:体积更小、速度更快!
查看>>
数据指标/表现度量系统(Performance Measurement System)综述
查看>>
GitHub宣布推出Electron 1.0和Devtron,并将提供无限制的私有代码库
查看>>