[清华集训] 温暖会指引我们前行

同样是LCT维护一个类似最大生成树的东西。
题目链接:戳我
emmm其实我在uoj上过不去,加的数据我TLE了。。。。。。

关于push_up的小trick:初始化的时候给0节点也初始化成最大值,然后push_up的时候不用管自己的左右儿子是否为空,直接返回左右儿子中比较小的一个就可以了,然后再和自己作比较。qwqwq

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 400010
using namespace std;
int n,m,tot;
int s[MAXN];
char cur[10];
struct Edge{int ff,val,rev,minn,sum,tmp,ch[2];}t[MAXN];
struct Edge2{int id,u,v,a,b;}edge[MAXN];
inline int ls(int x){return t[x].ch[0];}
inline int rs(int x){return t[x].ch[1];}
inline void push_up(int x)
{
    t[x].minn=(t[t[ls(x)].minn].tmp<t[t[rs(x)].minn].tmp?t[ls(x)].minn:t[rs(x)].minn);
    t[x].minn=(t[x].tmp<t[t[x].minn].tmp)?x:t[x].minn;
    t[x].sum=t[t[x].ch[0]].sum+t[t[x].ch[1]].sum+t[x].val;
}
inline bool isroot(int x){return t[t[x].ff].ch[0]!=x&&t[t[x].ff].ch[1]!=x;}
inline bool cmp(struct Edge2 x,struct Edge2 y){return x.b>y.b;}
inline void rotate(int x)
{
    int y=t[x].ff;
    int z=t[y].ff;
    int k=t[y].ch[1]==x;
    if(!isroot(y)) t[z].ch[t[z].ch[1]==y]=x; t[x].ff=z;
    t[y].ch[k]=t[x].ch[k^1]; t[t[x].ch[k^1]].ff=y;
    t[x].ch[k^1]=y; t[y].ff=x;
    push_up(y),push_up(x);
}
inline void push_down(int x)
{
    if(t[x].rev)
    {
        if(t[x].ch[0]) t[t[x].ch[0]].rev^=1;
        if(t[x].ch[1]) t[t[x].ch[1]].rev^=1;
        swap(t[x].ch[0],t[x].ch[1]);
        t[x].rev^=1;
    }
}
inline void splay(int x)
{
    s[tot=1]=x;
    for(int i=x;!isroot(i);i=t[i].ff) s[++tot]=t[i].ff;
    while(tot) push_down(s[tot--]);
    while(!isroot(x))
    {
        int y=t[x].ff;
        int z=t[y].ff;
        if(!isroot(y))
            ((t[y].ch[0]==x)^(t[z].ch[0]==y))?rotate(x):rotate(y);
        rotate(x);
    }
}
inline void access(int x)
{
    for(int y=0;x;y=x,x=t[x].ff)
        splay(x),t[x].ch[1]=y,push_up(x);
}
inline void makeroot(int x){access(x);splay(x);t[x].rev^=1;}
inline void split(int x,int y){makeroot(x);access(y);splay(y);}
inline void cut(int x,int y){split(x,y);t[y].ch[0]=t[x].ff=0;}
inline void link(int x,int y){makeroot(x);t[x].ff=y;}
inline int findroot(int x)
{
    access(x);splay(x);
    while(t[x].ch[0]) push_down(x),x=t[x].ch[0];
    return x;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("ce.in","r",stdin);
    #endif
    scanf("%d%d",&n,&m);
    for(int i=0;i<=n;i++) t[i].tmp=1e9;
    for(int i=1;i<=m;i++)
    {
        scanf("%s",cur);
        if(cur[0]=='f')
        {
            scanf("%d%d%d%d%d",&edge[i].id,&edge[i].u,&edge[i].v,&edge[i].a,&edge[i].b);
            edge[i].id++,edge[i].u++,edge[i].v++;
            int u=edge[i].u,v=edge[i].v;
            if(findroot(u)==findroot(v))
            {
                split(u,v);
                int cur=t[v].minn;
                if(edge[i].a<=t[cur].tmp) continue;
                cut(u,cur),cut(v,cur);
            }
            t[edge[i].id+n].tmp=edge[i].a;
            t[edge[i].id+n].val=edge[i].b;
            link(u,edge[i].id+n),link(v,edge[i].id+n);
        }
        else if(cur[0]=='m')
        {
            int u,v;
            scanf("%d%d",&u,&v);
            u++,v++;
            if(findroot(u)!=findroot(v)) printf("-1\n");
            else split(u,v),printf("%d\n",t[v].sum);
        }
        else
        {
            int x,p;
            scanf("%d%d",&x,&p);
            x++;
            makeroot(x+n);
            t[x+n].val=p;
            push_up(x+n);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/fengxunling/p/10290020.html

时间: 2024-08-01 20:55:44

[清华集训] 温暖会指引我们前行的相关文章

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

[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很低. 小R的宿舍楼中有n个地点和一些路,一条路连接了两个地点,小R可以通过这条路从其中任意一个地点到达另外一个地点.但在刚开始,小R还不熟悉宿舍楼中的任何一条路,所以他会慢慢地发现这些路,他在发现一条路时还会知道这条路的温度和长度.每条路的温度都是互不相同的. 小R需要在宿舍楼中活动,每次他都需要从

bzoj 4736 /uoj274【清华集训2016】温暖会指引我们前行 lct

[清华集训2016]温暖会指引我们前行 统计 描述 提交 自定义测试 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一位火焰之神 “我将赐予你们温暖和希望!” 只见他的身体中喷射出火焰之力 通过坚固的钢铁,传遍了千家万户 这时,只听见人们欢呼 “暖气来啦!” 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很低. 小R的

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&g

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

题目分析: 差评,最大生成树裸题.hack数据还卡常. 代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 402000; 5 6 struct LCT{ 7 int fa[maxn],lazy[maxn],ch[maxn][2],d1[maxn],d2[maxn]; 8 int val[maxn],tot[maxn],num; 9 stack<int> sta; 10 void pus

【BZOJ4736】温暖会指引我们前行(LCT)

题意:有一张图,每条边有一个不同的编号,长度和权值,维护以下操作: 1.加边 2.修改边长 3.询问两点之间在最小权值最大的前提下的唯一路径长度 n<=100000 m<=300000 思路:RYZ作业 BZOJ上有四组数据的输入不完整,输出没问题 LCT维护最大生成树,维护子树和即可和子树中权值最小的位置即可 1 var t:array[0..700000,0..1]of longint; 2 sum:array[0..700000]of int64; 3 f:array[1..700000

[UOJ274]温暖会指引我们前行

看春晚不如写题... 第一次写维护边权的题,因为懒所以没学边权lct,写的是插入虚点存边权,但我猜两种写法的效率应该差不多 要求最低温度尽量高,所以只能走最高温度生成树上的边,用lct维护就行了 lct维护最大生成树,每加一条边$\left(x,y,T\right)$,如果两边不连通就直接连,如果连通且树上$x\rightarrow y$的最低温度$\geq T$,那么不用加边,否则删掉树上路径最低温的边,加入新边 个人觉得这种插入虚点维护边权的写法挺方便的 #include<stdio.h>

BZOJ 4736 温暖会指引我们前行 LCT+最优生成树+并查集

题目链接:http://uoj.ac/problem/274 题意概述: 没什么好概述的......概述了题意就知道怎么做了...... 分析: 就是用lct维护最大生成树. 然后如果去UOJ上面交发现如果不用并查集判断连通性就要T?! 然后我就默默改了并查集...(hash表并查集输入输出占据了一半的行数?!) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdl

uoj 41 【清华集训2014】矩阵变换 婚姻稳定问题

[清华集训2014]矩阵变换 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/41 Description 给出一个 N 行 M 列的矩阵A, 保证满足以下性质: M>N.    矩阵中每个数都是 [0,N] 中的自然数.    每行中, [1,N] 中每个自然数都恰好出现一次.这意味着每行中 0 恰好出现 M−N 次.    每列中,[1,N] 中每个自然数至多出现一次. 现在我们要在每行中选取一个非零数,

AC日记——【清华集训2014】奇数国 uoj 38

#38. [清华集训2014]奇数国 思路: 题目中的number与product不想冲: 即为number与product互素: 所以,求phi(product)即可: 除一个数等同于在模的意义下乘以一个数的逆元: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 100005