UOJ 274 【清华集训2016】温暖会指引我们前行 ——Link-Cut Tree
魔法森林高清重置,
只需要维护关于t的最大生成树,然后链上边权求和即可。
直接上LCT
调了将近2h
吃枣药丸
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define maxn 1000005
#define inf 0x3f3f3f3f
struct Link_Cut_Tree{
int ch[maxn][2],fa[maxn],rev[maxn],mn[maxn],val[maxn],sum[maxn],len[maxn];
int sta[maxn],top;
bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
void update(int x)
{
mn[x]=x;
if (val[mn[ch[x][0]]]<val[mn[x]]) mn[x]=mn[ch[x][0]];
if (val[mn[ch[x][1]]]<val[mn[x]]) mn[x]=mn[ch[x][1]];
sum[x]=len[x]+sum[ch[x][0]]+sum[ch[x][1]];
}
void pushdown (int x)
{
if (rev[x])
{
rev[x]^=1;
rev[ch[x][0]]^=1;
rev[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
}
}
void rot(int x)
{
int y=fa[x],z=fa[y],l,r;
if (ch[y][0]==x) l=0; else l=1; r=l^1;
if (!isroot(y))
{
if (ch[z][0]==y) ch[z][0]=x;
else ch[z][1]=x;
}
fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
ch[y][l]=ch[x][r]; ch[x][r]=y;
update(y);update(x);
}
void splay(int x)
{
top=0;sta[++top]=x;
for (int i=x;!isroot(i);i=fa[i]) sta[++top]=fa[i];
while (top) pushdown(sta[top--]); while (!isroot(x))
{
int y=fa[x],z=fa[y];
if (!isroot(y))
{
if (ch[z][0]==y^ch[y][0]==x) rot(x);
else rot(y);
}
rot(x);
}
}
void access(int x)
{
for (int t=0;x;t=x,x=fa[x])
splay(x),ch[x][1]=t,update(x);
}
void makeroot(int x)
{
access(x); splay(x); rev[x]^=1;
}
int find(int x)
{
access(x); splay(x); while (ch[x][0]) x=ch[x][0];
return x;
}
void link(int x,int y)
{
makeroot(x);
fa[x]=y;
}
void cut(int x,int y)
{
makeroot(x);
access(y);
ch[y][0]=0;
fa[x]=0;
}
int query(int x,int y)
{
if (find(x)!=find(y)) return -1;
makeroot(x);access(y);splay(y);
return sum[y];
}
int qmn(int x,int y)
{
makeroot(x);access(y);splay(y);
return mn[y];
}
void modify(int x,int l)
{
access(x);splay(x);
len[x]=l;update(x);
}
}LCT; int n,m,u[maxn],v[maxn],id,t,l,x,y,in[maxn];
char opt[11]; int main()
{
scanf("%d%d",&n,&m);
F(i,0,n) LCT.val[i]=inf;
F(i,1,m)
{
scanf("%s",opt);
switch(opt[0])
{
case 'f':
scanf("%d",&id); id++;
scanf("%d%d%d%d",&u[id],&v[id],&t,&l);u[id]++;v[id]++;
LCT.len[id+n]=l;LCT.val[id+n]=t;LCT.mn[id+n]=id+n;
if (LCT.find(u[id])!=LCT.find(v[id]))
{
LCT.link(id+n,u[id]);
LCT.link(id+n,v[id]);
in[id]=1;
}
else
{
int tmp=LCT.qmn(u[id],v[id]);
if (LCT.val[tmp]<t)
{
LCT.cut(u[tmp-n],tmp);
LCT.cut(v[tmp-n],tmp);
in[tmp-n]=0;
LCT.link(id+n,u[id]);
LCT.link(id+n,v[id]);
in[id]=1;
}
}
break;
case 'm':
scanf("%d%d",&x,&y);x++;y++;
printf("%d\n",LCT.query(x,y));
break;
case 'c':
scanf("%d%d",&id,&l); id++;
if (in[id]) LCT.modify(id+n,l);
break;
}
}
}
UOJ 274 【清华集训2016】温暖会指引我们前行 ——Link-Cut Tree的更多相关文章
- [UOJ#274][清华集训2016]温暖会指引我们前行
[UOJ#274][清华集训2016]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...
- UOJ_274_[清华集训2016]温暖会指引我们前行_LCT
UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...
- [清华集训2016]温暖会指引我们前行——LCT+最大生成树
题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...
- 【bzoj4736/uoj#274】[清华集训2016]温暖会指引我们前行 语文题+LCT
题目描述 http://uoj.ac/problem/274 题解 语文题+LCT 对于这种语文题建议还是自己读题好一些... 读懂题后发现:由于温度互不相同,最大生成树上的路径必须走(不走的话温度大 ...
- UOJ274 [清华集训2016] 温暖会指引我们前行 【LCT】【最大生成树】
题目分析: 差评,最大生成树裸题.hack数据还卡常. 代码: #include<bits/stdc++.h> using namespace std; ; struct LCT{ ],d ...
- 【BZOJ4736】温暖会指引我们前行(Link-Cut Tree)
[BZOJ4736]温暖会指引我们前行(Link-Cut Tree) ##题面 神TM题面是UOJ的 题解 LCT傻逼维护最大生成树 不会的可以去做一做魔法森林 #include<iostrea ...
- BZOJ 4732 UOJ #268 [清华集训2016]数据交互 (树链剖分、线段树)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=4732 (UOJ) http://uoj.ac/problem/268 题解 ...
- [UOJ#276][清华集训2016]汽水[分数规划+点分治]
题意 给定一棵 \(n\) 个点的树,给定 \(k\) ,求 \(|\frac{\sum w(路径长度)}{t(路径边数)}-k|\)的最小值. \(n\leq 5\times 10^5,k\leq ...
- UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]
#274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...
- 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT
[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...
随机推荐
- 指定ip地址登陆服务器
[root@localhost ~]# cat /etc/hosts.allow ## hosts.allow This file contains access rules which are ...
- (九)mybatis之生命周期
生命周期 SqlSessionFactoryBuilder SqlSessionFactoryBuilder的作用就是生成SqlSessionFactory对象,是一个构建器.所以我们一旦构建 ...
- Ubuntu 16.04下Java环境安装与配置
首先下载linux下的安装包 登陆网址https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.h ...
- python 基础之运算符
运算符 a=10 ,b=20 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 - 两个数相乘 ...
- 设置与使用SQL Server的字符集(Collation,即排序规则)
目录 目录 正确认识SQL Server的字符集 选择合适的SQL Server字符集 错误使用SQL Server的字符集 参考资料 正确认识SQL Server的字符集 SQL Server作为一 ...
- iOS开发各种证书问题
引言 写在前面 一.App ID(bundle identifier) 二.设备(Device) 三.开发证书(Certificates) 四.供应配置文件(Provisioning ...
- Use-After-Free
0x00 UAF利用原理 uaf漏洞产生的主要原因是释放了一个堆块后,并没有将该指针置为NULL,这样导致该指针处于悬空的状态(这个指针可以称为恶性迷途指针),同样被释放的内存如果被恶意构造数据,就有 ...
- SniperOJ-leak-advanced-x86-64
借助DynELF实现无libc的漏洞利用小结 1.leak-advance与leak的区别在于一个可用函数是write,一个可用函数是puts.write比puts更容易利用,虽然write需要的参数 ...
- github+hexo+themes搭建简易个性主题博客
0x00 install Node.js and git 安装Node.js:http://www.runoob.com/nodejs/nodejs-install-setup.html 安装git ...
- Paxos算法与Zookeeper分析,zab (zk)raft协议(etcd) 8. 与Galera及MySQL Group replication的比较
mit 分布式论文集 https://github.com/feixiao/Distributed-Systems wiki上描述的几种都明白了就出师了 raft 和 zab 是类似的,都是1.先选举 ...