UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]
#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]的更多相关文章
- [UOJ#274][清华集训2016]温暖会指引我们前行
[UOJ#274][清华集训2016]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...
- [清华集训2016]温暖会指引我们前行——LCT+最大生成树
题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...
- UOJ_274_[清华集训2016]温暖会指引我们前行_LCT
UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...
- 【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 ...
- 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT
[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...
- bzoj 4736 /uoj274【清华集训2016】温暖会指引我们前行 lct
[清华集训2016]温暖会指引我们前行 统计 描述 提交 自定义测试 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了 ...
- Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树
Code: 行#include<bits/stdc++.h> #define ll long long #define maxn 1000000 #define inf 100000000 ...
- BZOJ 4732 UOJ #268 [清华集训2016]数据交互 (树链剖分、线段树)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=4732 (UOJ) http://uoj.ac/problem/268 题解 ...
随机推荐
- Windows系统下文件的概念及c语言对其的基本操作(乙)
- jquery dataTimePicker日历插件(精确到小时)
效果图: 下载地址:https://github.com/WangChangyao/jquery-dataTimePicker.git <!DOCTYPE html> <h ...
- Linux中git的使用
之前在windows中一直采用github的桌面版,库的建立更新都是借助软件的帮助.所使用的的功能也非常局限,仅仅只是创建库再提交自己的代码.至于版本管理.回滚.分支以及git的结构都没有清楚的认识. ...
- 宝塔服务器管理助手Linux面版-使用教程
在顺利安装宝塔服务器linux面板之后,我们打开这个面板,UI界面设计的很简介,所有命令一看就知道是干什么用的,和我们以前用过的虚拟主机管理后台是很像的. 方法/步骤 1 使用方法如下: 面板地址:h ...
- angular 选中切换面板
此方法采用的是没有路由的方式: html5 代码: <div [hidden]="flag"> <li class="music-list-datail ...
- Thrift compiler代码生成类解析
代码生成类解析: Thrift--facebook RPC框架,介绍就不说了,百度,google一大把,使用也不介绍,直接上结构和分析吧. Hello.thrift文件内容如下: namespace ...
- 实现鼠标hover动画效果自己理解的两种方法——练习笔记
练习前端技术学院的任务,需要实现"导航栏中的链接,随着鼠标悬浮的位置,相应的链接下出现红色线段"的效果(如图1),我的理解有简易与稍显复杂一些的方法: 首先想到的就是直接利用伪元素 ...
- get最简单直接粗爆git与github教程
Git是分布式版本控制系统(可以理解为文件管理拓展工具) github一个在线文件托管系统(可以理解为一个在线云盘) 准备工作,在git官网下载git软件件,安装git软件,以windows.为例,下 ...
- dBm与Vpp相互转换公式
dBm = 10 + 20lg(0.5Vpp) Vpp = 2x10 以上公式均为阻抗为50欧的情况下计算得出的
- python_如何快速下载安装第三方库?
如何快速下载安装第三方库? --通过 淘宝源 https://mirrors.aliyun.com/pypi/simple/ 本国网络进行快速安装 如何执行安装命令? pip install Dja ...