#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. hdu_1027(好吧。。。牛。。。next_permutation也可以水过)

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; int ...

  2. C. The Smallest String Concatenation

    C. The Smallest String Concatenation time limit per test 3 seconds memory limit per test 256 megabyt ...

  3. tinyxml的封装与使用(转载)

    tinyxml是个高效精简的xml解析开源代码. 针对tinyxml直接使用对于对xml不是很熟悉的入门新手来说,有些概念难以理解,因此我将其封装后,供大家使用. 头文件: #include &quo ...

  4. 2017年 JavaScript 框架回顾 -- React生态系统

    前一篇文章中,我们介绍了2017年 JavaScript 框架的整体情况.我们也了解到在众多的前端框架中,目前最为庞大又在快速增长的当属 React 了,本文就来重点介绍 React 的生态系统. 首 ...

  5. 这个时间格式2017-09-26-T04:00:00Z php识别不出来

    这应该不对吧 这是什么格式?看起来不标准,一般不都是传 2017-09-26 04:00:00 这种吗?不行用正则筛吧.echo date('Y-m-d H:i:s',date_create_from ...

  6. PHPMailer发送邮件失败:SMTP connect failed

    标签: PHPMailersmtp邮件服务器邮件发送失败 2015-05-22 19:29 1755人阅读 评论(0) 收藏 举报 分类: Apache php+mysql(2) 版权声明:本文为博主 ...

  7. electron 学习笔记

    一.快速搭建一个electron 项目结构 # 克隆示例项目的仓库 $ git clone https://github.com/electron/electron-quick-start # 进入这 ...

  8. ASIHTTPRequest

    ASIHTTPRequest,是一个直接在CFNetwork上做的开源项目,提供了一个比官方更方便更强大的HTTP网络传输的封装.

  9. ap web

    apapplication端吧    应用程序端   也C-S架构Cweb网页端   般封装httpservletrequest和httpservletresponse对象处理些操作  b-s架构

  10. Angular 4+ Http

    HTTP: 使应用能够对远端服务器发起相应的Http调用: 你要知道: HttpModule并不是Angular的核心模块,它是Angualr用来进行Web访问的一种可选方式,并位于一个名叫@angu ...