bzoj2959
lct+并查集
联赛之后忘了很多东西 复习一下
这并不是一棵树,所以我们不能直接上lct
但是把双联通分量缩了以后就是一棵树了
怎么缩呢 就是把splay拆了合并到一个点上
连通性和双联通分量拿两个并查集维护
access的时候x=find(fa[x])
#include<bits/stdc++.h>
using namespace std;
const int N = 1.5e5 + ;
struct ufs {
int fa[N];
ufs() { for(int i = ; i < N; ++i) fa[i] = i; }
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void link(int u, int v)
{
fa[find(u)] = find(v);
}
bool con(int u, int v)
{
return find(u) == find(v);
}
} a, b;
int n, m;
int val[N];
queue<int> q;
namespace lct
{
struct node {
int rev, sum, w, f;
int ch[];
} t[N];
bool isr(int x) { return !t[x].f || (x != t[t[x].f].ch[] && x != t[t[x].f].ch[]); }
int wh(int x) { return x == t[t[x].f].ch[]; }
void paint(int x)
{
t[x].rev ^= ;
swap(t[x].ch[], t[x].ch[]);
}
void upd(int x)
{
t[x].sum = t[t[x].ch[]].sum + t[t[x].ch[]].sum + t[x].w;
}
void pd(int x)
{
if(!t[x].rev) return;
paint(t[x].ch[]);
paint(t[x].ch[]);
t[x].rev ^= ;
}
void rotate(int x)
{
int y = t[x].f, z = t[y].f, w = wh(x);
if(!isr(y)) t[z].ch[wh(y)] = x;
t[x].f = z;
t[y].ch[w] = t[x].ch[w ^ ];
t[t[x].ch[w ^ ]].f = y;
t[x].ch[w ^ ] = y;
t[y].f = x;
upd(y);
upd(x);
}
void ptd(int x)
{
if(!isr(x)) ptd(t[x].f);
pd(x);
}
void splay(int x)
{
ptd(x);
for(; !isr(x); rotate(x))
if(!isr(t[x].f)) rotate(wh(x) == wh(t[x].f) ? t[x].f : x);
}
void access(int x)
{
for(int p = ; x; p = x, x = b.find(t[x].f))
{
splay(x);
t[x].ch[] = p;
t[p].f = x;
upd(x);
}
}
void rever(int x)
{
access(x);
splay(x);
paint(x);
}
void expose(int x, int y)
{
rever(y);
access(x);
splay(x);
}
void link(int u, int v)
{
rever(u);
t[u].f = v;
}
void rebuild(int x, int y)
{
int rt = x;
q.push(x);
expose(x, y);
while(!q.empty())
{
x = q.front();
b.link(x, rt);
q.pop();
if(t[x].ch[]) q.push(t[x].ch[]);
if(t[x].ch[]) q.push(t[x].ch[]);
t[x].ch[] = t[x].ch[] = ;
}
t[rt].w = t[rt].sum;
}
}
void link(int u, int v)
{
u = b.find(u);
v = b.find(v);
if(u == v) return;
if(!a.con(u, v)) lct::link(u, v), a.link(u, v);
else lct::rebuild(u, v);
}
int query(int u, int v)
{
u = b.find(u);
v = b.find(v);
if(!a.con(u, v)) return -;
if(u == v) return lct::t[u].w;
lct::expose(u, v);
return lct::t[u].sum;
}
void add(int x, int v)
{
x = b.find(x);
lct::t[x].w += v;
lct::t[x].sum += v;
lct::splay(x);
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i) scanf("%d", &lct::t[i].w), val[i] = lct::t[i].sum = lct::t[i].w;
while(m--)
{
int opt, u, v;
scanf("%d%d%d", &opt, &u, &v);
if(opt == ) link(u, v);
if(opt == ) add(u, v - val[u]), val[u] = v;
if(opt == ) printf("%d\n", query(u, v));
}
return ;
}
bzoj2959的更多相关文章
- [BZOJ2959]长跑——新技能:LCT+缩圈
[BZOJ2959]长跑 试题描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘 ...
- 【BZOJ2959】长跑(Link-Cut Tree,并查集)
[BZOJ2959]长跑(Link-Cut Tree,并查集) 题面 BZOJ 题解 如果保证不出现环的话 妥妥的\(LCT\)傻逼题 现在可能会出现环 环有什么影响? 那就可以沿着环把所有点全部走一 ...
- bzoj2959: 长跑
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- 【BZOJ2959】长跑 (LCT+并查集)
Time Limit: 1000 ms Memory Limit: 256 MB Description 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室 ...
- BZOJ2959长跑——LCT+并查集(LCT动态维护边双连通分量)
题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前. 为 ...
- bzoj2959: 长跑(LCT+并查集)
题解 动态树Link-cut tree(LCT)总结 LCT常数大得真实 没有环,就是\(lct\)裸题吧 有环,我们就可以绕环转一圈,缩点 怎么搞? 当形成环时,把所有点的值全部加到一个点上,用并查 ...
- 【bzoj2959】长跑 LCT+并查集
题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前.为了 ...
- bzoj2959: 长跑 LCT+并查集+边双联通
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2959 题解 调了半天,终于调完了. 显然题目要求是求出目前从 \(A\) 到 \(B\) 的可 ...
- BZOJ3069: [Pa2011]Hard Choice 艰难的选择
Description Byteasar是一个很纠结的人.每次他经过Bytetown的时候都知道有至少2条不同的路径可以选择,这导致他必须花很长时间来决定走哪条路.Byteasar最近听说了Bytet ...
随机推荐
- vue2.0 自定义 弹窗(MessageBox)组件
组件模板 src/components/MessageBox/index.vue <!-- 自定义 MessageBox 组件 --> <template> <div c ...
- 合并SO为单独交货单
本场景为单步交货 为客户建立专用的route. 增加一个pull rule 在做订单的时候,为订单行选择 上面建立好的route, 连续建立了 2个 订单 SO ...
- 整合Hibernate3.x
As of Spring 3.0, Spring requires Hibernate 3.2 or later. Hibernate 3和Hibernate 4有一些区别,所以对于spring而已, ...
- AMD单双桥时序简叙
芯片组(双桥)时序 VBAT :RTC电路的供电3V(RTC电路有问题会导致没复位或不跑码等故障) RTCCLK :晶振起振给南桥提供32.768KHz频率(RTC电路有问题会导致没复位或不跑码等故障 ...
- Java、C++、Python、Ruby、PHP、C#和JavaScript的理解
Java.C++.Python.Ruby.PHP.C#和JavaScript和日本动漫里的那些大家熟悉的动漫人物结合起来.依据他们的身世.个人经历来生动的表达各编程语言的发展历程.原文内容例如以下: ...
- Canvas学习笔记——动画中的三角学
示例1,跟随鼠标的键头: 需要掌握一个重要的公式,这个方法返回从 x 轴到点 (x,y) 之间的角度 Math.atan2(dy,dx); 关键代码: function Arrow() { thi ...
- vim 参数文件配置
下面是我配置的遇到问题不能修改配置文件时的解决方案 1 /usr/share/vim/vimrc 2 这个是系统型的vimrc配置文件,为了保证vim的正常使用,一般并不会修改这个文件, 而是应该在你 ...
- C语言宏定义时#(井号)和##(双井号)作用
#的功能是将其后面的宏参数进行字符串化操作(Stringfication),简单说就是在对它所引用的宏变量 通过替换后在其左右各加上一个双引号. #define example(instr) prin ...
- mvc 发送QQ邮件
试图部分代码: @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } ...
- CI框架常识
1.有两种方法来加载自定义配置文件(如enums.php): <?php if (! defined('BASEPATH')) exit('No direct script access all ...