bzoj2959: 长跑(LCT+并查集)
题解
LCT常数大得真实
没有环,就是\(lct\)裸题吧
有环,我们就可以绕环转一圈,缩点
怎么搞?
当形成环时,把所有点的值全部加到一个点上,用并查集维护加到哪个点上
判断连通性再用一个并查集
Code
#include<bits/stdc++.h>
#define LL long long
#define RG register
using namespace std;
template<class T> inline void read(T &x) {
x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
x = f ? -x : x;
return ;
}
template<class T> inline void write(T x) {
if (!x) {putchar(48);return ;}
if (x < 0) x = -x, putchar('-');
int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}
const int N = 150010;
struct node {
int v, s, fa, ch[2];
bool rev;
}t[N];
int S[N], top;
#define lson (t[x].ch[0])
#define rson (t[x].ch[1])
void pushup(int x) {t[x].s = (t[lson].s + t[rson].s + t[x].v);return ;}
void putrev(int x) {
swap(lson, rson);
t[x].rev ^= 1;
return ;
}
void pushdown(int x) {
if (t[x].rev) {
if (lson) putrev(lson);
if (rson) putrev(rson);
t[x].rev = 0;
}
}
int fa[N], Fa[N];
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int Find(int x) {
return Fa[x] == x ? x : Fa[x] = Find(Fa[x]);
}
bool isroot(int x) {
return (t[find(t[x].fa)].ch[0] != x) && (t[find(t[x].fa)].ch[1] != x);
}
void rotate(int x) {
int y = find(t[x].fa), z = find(t[y].fa), k = t[y].ch[1] == x;
if (!isroot(y)) t[z].ch[t[z].ch[1] == y] = x;
t[x].fa = z;
t[t[x].ch[k^1]].fa = y, t[y].ch[k] = t[x].ch[k^1];
t[x].ch[k^1] = y; t[y].fa = x;
pushup(y);
return ;
}
void splay(int x) {
S[top = 1] = x;
for (RG int i = x; !isroot(i); i = find(t[i].fa)) S[++top] = find(t[i].fa);
for (RG int i = top; i; i--) pushdown(S[i]);
while (!isroot(x)) {
int y = find(t[x].fa), z = find(t[y].fa);
if (!isroot(y))
(t[y].ch[1] == x) ^ (t[z].ch[1] == y) ? rotate(x) : rotate(y);
rotate(x);
}
pushup(x);
return ;
}
void access(int x) {
for (int y = 0; x; y = x, x = find(t[x].fa))
splay(x), t[x].ch[1] = y, pushup(x);
}
void makeroot(int x) {
access(x); splay(x); putrev(x);
return ;
}
void link(int x, int y) {
makeroot(x);
t[x].fa = y;
}
void split(int x, int y) {
makeroot(x), access(y), splay(y);
return ;
}
int a[N], n, m;
void dfs(int x, int y) {
fa[x] = y;
pushdown(x);
if (lson) dfs(lson, y);
if (rson) dfs(rson, y);
return ;
}
int main() {
read(n), read(m);
for (int i = 1; i <= n; i++) read(a[i]), fa[i] = Fa[i] = i, t[i].s = t[i].v = a[i];
while (m--) {
int x, y, op;
read(op), read(x), read(y);
if (op == 1) {
x = find(x), y = find(y);
if (x == y) continue;
if (Find(x) != Find(y)) {
link(x, y);
Fa[Find(y)] = Find(x);
}
else {
split(x, y);
t[y].v = t[y].s;
dfs(y, y);
t[y].ch[0] = 0;
}
}
else if (op == 2) {
int tx = find(x);
splay(tx);
t[tx].s += y - a[x];
t[tx].v += y - a[x];
a[x] = y;
}
else {
x = find(x), y = find(y);
if (Find(x) != Find(y)) {
puts("-1");
continue;
}
split(x, y);
printf("%d\n", t[y].s);
}
}
return 0;
}
bzoj2959: 长跑(LCT+并查集)的更多相关文章
- BZOJ2959长跑——LCT+并查集(LCT动态维护边双连通分量)
题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前. 为 ...
- bzoj2959: 长跑 LCT+并查集+边双联通
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2959 题解 调了半天,终于调完了. 显然题目要求是求出目前从 \(A\) 到 \(B\) 的可 ...
- 【bzoj2959】长跑 LCT+并查集
题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前.为了 ...
- BZOJ 2959 长跑 (LCT+并查集)
题面:BZOJ传送门 当成有向边做的发现过不去样例,改成无向边就忘了原来的思路.. 因为成环的点一定都能取到,我们把它们压成一个新点,权值为环上所有点的权值和 这样保证了图是一颗森林 每次询问转化为, ...
- 【bzoj4998】星球联盟 LCT+并查集
题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两个 ...
- 【BZOJ2959】长跑 (LCT+并查集)
Time Limit: 1000 ms Memory Limit: 256 MB Description 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室 ...
- bzoj 2959: 长跑【LCT+并查集】
如果没有环的话直接LCT 考虑有环怎么办,如果是静态的话就tarjan了,但是这里要动态的缩环 具体是link操作的时候看一下是否成环(两点已联通),成环的话就用并查集把这条链缩到一个点,把权值加给祖 ...
- bzoj4998 星球联盟 LCT + 并查集
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4998 题解 根据题意,就是要动态维护点双,求出一个点双的权值和. 所以这道题就是和 bzoj2 ...
- BZOJ4998星球联盟——LCT+并查集(LCT动态维护边双连通分量)
题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成 联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两 ...
随机推荐
- p1501 [国家集训队]Tree II
传送门 分析 lct板子题 单独维护一下加和乘的情况即可 维护方法和维护翻转差不多 代码 #include<iostream> #include<cstdio> #includ ...
- 约瑟夫问题的变种 LA3882
题目大意: N个数排成一圈,第一次删除m,以后每k个数删除一次,求最后一被删除的数. 如果这题用链表或者数组模拟整个过程的话,时间复杂度都将高达O(nk),而n<=10000,k<=100 ...
- Python 与 Javascript 比较
最近由于工作的需要开始开发一些Python的东西,由于之前一直在使用Javascript,所以会不自觉的使用一些Javascript的概念,语法什么的,经常掉到坑里.我觉得对于从Javascript转 ...
- 一张图让你看懂HDMI针脚定义
一张图让你看懂HDMI针脚定义 摘自:http://www.hdmiaoc.com/cjwt-175.html 什么是HDMI线,HDMI插头的每根PIN脚是什么意思? 大部分使用HDMI标准的研发及 ...
- python 输入输出,file, os模块
Python 输入和输出 输出格式美化 Python两种输出值的方式: 表达式语句和 print() 函数. 第三种方式是使用文件对象的 write() 方法,标准输出文件可以用 sys.stdout ...
- 高并发Redis(Mac)环境配置(一)
一.产生原因: SNS交互型网站的兴起,对于高并发,大负载数据的操作,海量数据的存储和访问 NoSql四种类型: 键值存储(Redis优点可以快速查询,缺点缺少存储的结构化) ...
- 【更新】用word文档来发布到csdn等博客上边免去一张张上传图片的烦恼
目前大部分的博客作者在写博客这件事情上都会遇到以下3个痛点:1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.2.发布到博客或公众号平台 ...
- .net 有参属性 index (索引)
public class IndexTempte { public ArrayList nameList = new ArrayList(); public string this[int index ...
- github 上中国互联网公司的开源项目
github上 那个 watch和 follow功能 不太好用啊. 是我用的 不好,还是 怎么的.有时候 找不到 watch 和 follow. 秉持 开源 精神,省的大家 和 我 查找. 我只关注 ...
- [Postgres]合并多行到一列(转)
转自http://csk83.sinaapp.com/?p=104 在实际应用中常常遇见这样的情况,见下表,我们现在需要统计出来每年每个人的工资总和以及发放月份. user_name year mon ...