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 ...
随机推荐
- PADS的历史版本
1986年:PADS PCB,DOS操作系统 1989年:PADS Logic,DOS操作系统 1990年:PADS 2000,DOS操作系统 1993年:PADS Perform,DOS和Windo ...
- VM(转)
vmplayer && VMworkstation 很多人想尝试一下多种不同的操作系统,例如学习Linux:又或者希望搞一个专门的系统用来测试各种各样东西而不会搞乱搞坏现有的系统. ...
- android中的常见对话框
在android中对话框是一种常见的操作,常见的对话框有下面几种: 以下是xml布局文件: <LinearLayout xmlns:android="http://schemas.an ...
- 利用CH341A编程器刷新BIOS,恢复BIOS,妈妈再也不用担心BIOS刷坏了
前几天,修电脑主析就捣鼓刷BIOS,结果刷完黑屏开不了机,立刻意识到完了,BIOS刷错了.就从网上查资料,各种方法试了个遍,什么用处都没有.终于功夫不负有心人,找到了编码器,知道了怎么用.下面看看具体 ...
- Active Directory的LDAP协议与DN(Distinguished Name)详解
前言 光copy几段代码的文章没什么意思,本章上最基础的代码,主要是为了从编程方面聊LDAP和DN,其它的后面聊,一步步慢慢来吧. Active Directory编程须知 1.域控服务器: Wind ...
- 03 xml封装通信接口
<?php class Response_xml{ /** *按xml方式输出通信 *@param integet $code 状态码 *@param string $message 提示信息 ...
- GreenPlum 安装方法详解
一.安装环境准备 1.磁盘环境准备 磁盘分区典型配置如下: 文件系统 文件格式 大小 / ext3 50GB,Linux系统的根目录,所有的目录都挂在这个目录下面,建议大小为 ...
- JAVA RMI远程方法调用简单实例(转载)
来源:http://www.cnblogs.com/leslies2/archive/2011/05/20/2051844.html RMI的概念 RMI(Remote Method Invocati ...
- 【BZOJ3307】雨天的尾巴 线段树合并
[BZOJ3307]雨天的尾巴 Description N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多 ...
- Requires: libstdc++.so.6(GLIBCXX_3.4.15)(64bit)
Error: Package: mysql-community-server-8.0.12-1.el7.x86_64 (mysql80-community) Requires: libstdc++.s ...