#274. 【清华集训2016】温暖会指引我们前行

题意比较巧妙

裸lct维护最大生成树

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
#define lc t[x].ch[0]
#define rc t[x].ch[1]
#define pa t[x].fa
const int N = 4e5+5, INF = 1e9+5;
inline int read(){
char c=getchar(); int x=0,f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
} int n, m, val[N], id, u, v, tem, w;
struct edge{int u, v;} e[N];
char s[10]; namespace lct {
struct meow{ int ch[2], fa, rev, sum, w, p; } t[N];
inline int wh(int x) {return t[pa].ch[1] == x;}
inline bool isr(int x) {return t[pa].ch[0] != x && t[pa].ch[1] != x;}
inline void rever(int x) {t[x].rev ^= 1; swap(lc, rc);}
inline void pushdn(int x) {
if(t[x].rev) {
if(lc) rever(lc);
if(rc) rever(rc);
t[x].rev = 0;
}
}
inline void pd(int x) {if(!isr(x)) pd(pa); pushdn(x);}
inline void update(int x) {
t[x].sum = t[lc].sum + t[rc].sum + t[x].w;
t[x].p = x;
if(lc && val[ t[lc].p ] < val[ t[x].p ]) t[x].p = t[lc].p;
if(rc && val[ t[rc].p ] < val[ t[x].p ]) t[x].p = t[rc].p;
}
inline void rotate(int x) {
int f = t[x].fa, g = t[f].fa, c = wh(x);
if(!isr(f)) t[g].ch[wh(f)] = x; t[x].fa = g;
t[f].ch[c] = t[x].ch[c^1]; t[ t[f].ch[c] ].fa = f;
t[x].ch[c^1] = f; t[f].fa = x;
update(f); update(x);
}
inline void splay(int x) {
pd(x);
for(; !isr(x); rotate(x))
if(!isr(pa)) rotate(wh(x) == wh(pa) ? pa : x);
}
inline void access(int x) {
for(int y=0; x; y=x, x=pa)
splay(x), rc=y, update(x);
}
inline void maker(int x) { access(x); splay(x); rever(x);}
inline void link(int x, int y) { maker(x); t[x].fa = y; }
inline void cut(int x, int y) {
maker(x); access(y); splay(y);
t[x].fa = t[y].ch[0] = 0; update(y);
}
inline void split(int x, int y) { maker(x), access(y); splay(y); }
} using namespace lct; int fa[N];
inline int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
inline void add() {
id=read()+1+n, u=read()+1, v=read()+1, tem=read(), w=read(); //printf("\nadd %d %d--%d %d %d\n", id, u, v, tem, w);
e[id] = (edge){u, v};
val[id] = tem;
t[id].sum = t[id].w = w; t[id].p = id;
if(find(u) != find(v)) {
fa[find(u)] = find(v);
link(id, u); link(id, v);
} else {
split(u, v);
int a = t[v].p; //printf("aaa %d\n", a);
if(val[a] < val[id]) {
cut(a, e[a].u); cut(a, e[a].v);
link(id, u); link(id, v);
}
}
}
inline void que(int u, int v) {
if(find(u) != find(v)) puts("-1");
else split(u, v), printf("%d\n", t[v].sum);
}
inline void cha(int id, int w) { //printf("\ncha %d %d\n", id, w);
t[id].w = w; splay(id);
}
int main() {
freopen("in", "r", stdin);
n=read(); m=read();
for(int i=1; i<=n; i++) fa[i] = i, val[i] = INF;
for(int i=1; i<=m; i++) { //printf("\nQ %d\n", i);
scanf("%s", s);
if(s[0] == 'f') add();
if(s[0] == 'm') u=read()+1, v=read()+1, que(u, v);
if(s[0] == 'c') id=read()+1+n, w=read(), cha(id, w);
}
}

UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]的更多相关文章

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

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

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

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

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

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

  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. 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT

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

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

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

  8. Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树

    Code: 行#include<bits/stdc++.h> #define ll long long #define maxn 1000000 #define inf 100000000 ...

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

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

随机推荐

  1. Codeforces780C

    题解:n个气球 从1到n染色,如果a.b和c是不同的正方形,a和b在它们之间有一条直接的路径,b和c之间有一条直接的路径,然后在这三个方块上的气球颜色是不同的. AC代码 #include <s ...

  2. codeforce 375_2_b_c

    codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...

  3. c语言基础学习02_windows系统下的cmd命令

    =============================================================================注意:cmd的命令很多,需要用的时候可以查询即 ...

  4. 修改mysql密码的四种方法

    方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...

  5. 利用jQuery移除和添加图片

    1.样式 <style type="text/css">     .changeImage{          background:url(images/right. ...

  6. asp.net -mvc框架复习(4)-ASP.NET MVC中的约定规则

    1.路由规则 using System;using System.Collections.Generic;using System.Linq;using System.Web;using System ...

  7. Lytro 光场相机重对焦C++实现以及CUDA实现

    前面有几篇博客主要介绍了光场和光场相机相关知识,以及重对焦效果和多视角效果的展示.算是自己学习光场过程的一种总结. 这次贴上自己用OpenCV/C++编写的重对焦算法实现(包含CPU版和CUDA GP ...

  8. Node.js进阶:5分钟入门非对称加密方法

    前言 刚回答了SegmentFault上一个兄弟提的问题<非对称解密出错>.这个属于Node.js在安全上的应用,遇到同样问题的人应该不少,基于回答的问题,这里简单总结下. 非对称加密的理 ...

  9. [转]maven打包报错:Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test

    源文URL:http://blog.csdn.net/caiwenfeng_for_23/article/details/44514947 mvn compile  没有问题,mvn package的 ...

  10. jsp页面中的$(param.xxx)

    $(param.user)相当于<%=request.getParameter("user")%> $(paramValues.favorites[0])相当于< ...