魔法森林高清重置,

只需要维护关于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的更多相关文章

  1. [UOJ#274][清华集训2016]温暖会指引我们前行

    [UOJ#274][清华集训2016]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...

  2. UOJ_274_[清华集训2016]温暖会指引我们前行_LCT

    UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...

  3. [清华集训2016]温暖会指引我们前行——LCT+最大生成树

    题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...

  4. 【bzoj4736/uoj#274】[清华集训2016]温暖会指引我们前行 语文题+LCT

    题目描述 http://uoj.ac/problem/274 题解 语文题+LCT 对于这种语文题建议还是自己读题好一些... 读懂题后发现:由于温度互不相同,最大生成树上的路径必须走(不走的话温度大 ...

  5. UOJ274 [清华集训2016] 温暖会指引我们前行 【LCT】【最大生成树】

    题目分析: 差评,最大生成树裸题.hack数据还卡常. 代码: #include<bits/stdc++.h> using namespace std; ; struct LCT{ ],d ...

  6. 【BZOJ4736】温暖会指引我们前行(Link-Cut Tree)

    [BZOJ4736]温暖会指引我们前行(Link-Cut Tree) ##题面 神TM题面是UOJ的 题解 LCT傻逼维护最大生成树 不会的可以去做一做魔法森林 #include<iostrea ...

  7. BZOJ 4732 UOJ #268 [清华集训2016]数据交互 (树链剖分、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=4732 (UOJ) http://uoj.ac/problem/268 题解 ...

  8. [UOJ#276][清华集训2016]汽水[分数规划+点分治]

    题意 给定一棵 \(n\) 个点的树,给定 \(k\) ,求 \(|\frac{\sum w(路径长度)}{t(路径边数)}-k|\)的最小值. \(n\leq 5\times 10^5,k\leq ...

  9. UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]

    #274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...

  10. 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT

    [UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...

随机推荐

  1. 求矩阵的n次方 c语言实现

    矩阵的n次方,比较容易理解的想法是递归. 思路是这样的,把n分成两部分,当n是偶数的时候,即为左右两边的乘积,如果n是奇数,即为左右两边的乘积再乘a ) matrixn())^*a else matr ...

  2. Jsoup获取全国地区数据(省市县镇村)(续) 纯干货分享

    前几天给大家分享了一下,怎么样通过jsoup来从国家统计局官网获取全国省市县镇村的数据.错过的朋友请点击这里.上文说到抓取到数据以后,我们怎么转换成我们想要格式呢?哈哈,解析方式可能很简单,但是有一点 ...

  3. js 分组数组

    思路: 1.先将数组按照一定规则排序: 2.再拆分数组到Map中,按Key分类: 3.再从Map中遍历取出要显示的内容: sortBroadList: function (broadcastList) ...

  4. halcon相机标定及图像矫正

    https://blog.csdn.net/humanking7/article/details/44756073 相机标定内容详解:转载自 祥的博客 预备知识 标定中的四个坐标系 1.1.平面旋转 ...

  5. 数据库web项目对数据库的操作

    1.0.JSTL与jsp实现对数据库的操作 MySql 数据库: create database if not exists CommodityDB; use CommodityDB; drop ta ...

  6. Open Cascade:AIS_InteractiveContext如何调用函数选择AIS对象

    AIS_InteractiveContext如何调用函数选择AIS对象 myAISContext->MoveTo(point.x, point.y, myView); myAISContext- ...

  7. 优化SQL语句的方法

    首先,对于where语句的注意事项: 1.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:select id from t where nu ...

  8. delphi win7 and high path

    Close DelphiLocate bordbk120N.dll (C:\Program Files (x86)\CodeGear\RAD Studio\6.0\bin)Make a backup ...

  9. win7旗舰版下配置IIS服务器

    选择上述的插件后,Windows 需要更新一段时间,并重启电脑 测试是否安装成功:http://localhost       注意:默认端口号是 80,不能和tomcat 的 80 端口同时重启 常 ...

  10. 随机生成一份试卷,试卷的种类分为单选、多选、判断三种题型。nodejs6.0 mysql

    背景:从数据库中,随机生成一份试卷,试卷的种类分为单选.多选.判断三种题型. 首先我需要生成随机数id(在这之前我需要知道数据库中各个题型的题数,这样我才能设置随机数),并依据生成的随机数id,去查找 ...