#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--2021

    发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  2. Spark环境搭建(中)——Hadoop安装

    1. 下载Hadoop 1.1 官网下载Hadoop http://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-2.9.0/hadoop-2. ...

  3. 1.移植3.4内核-分析内核启动过程,重新分区,烧写jffs2文件系统

    1.在上章-移植uboot里.我们来分析下uboot是如何进入到内核的 首先,uboot启动内核是通过bootcmd命令行实现的,在我们之前移植的bootcmd命令行如下所示: bootcmd=nan ...

  4. HDU 5914 Triangle(打表——斐波那契数的应用)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whos ...

  5. ItemCF_基于物品的协同过滤_MapReduceJava代码实现思路

    ItemCF_基于物品的协同过滤 1.    概念 2.    原理 如何给用户推荐? 给用户推荐他没有买过的物品--103 3.    java代码实现思路 数据集: 第一步:构建物品的同现矩阵 第 ...

  6. 【问题解决】Eclipse中 ctrl+空格 content assist

    改一下你的快捷键设置:window->perferences-->keys--->查找 content assist--->把这个地方改成你想要的就可以了.!

  7. J.U.C CAS

    在JDK1.5之前,也就是J.U.C加入JDK之前,Java是依靠synchronized关键字(JVM底层提供)来维护协调对共享字段的访问,保证对这些变量的独占访问权,并且以后其他线程忽的该锁时,将 ...

  8. 2017-07-02(free uptime cat /proc/cpuinfo uname lsb_release)

    free -b 字节为单位显示 -k KB为单位显示,默认显示就是KB -m 以MB为单位显示 -g 以GB为单位显示 free -m free 显示说明 uptime 跟top命令第一行内容相同 c ...

  9. 爬取知名社区技术文章_setting_5

    # -*- coding: utf-8 -*- # Scrapy settings for JobBole project # # For simplicity, this file contains ...

  10. 爬取知名社区技术文章_items_2

    item中定义获取的字段和原始数据进行处理并合法化数据 #!/usr/bin/python3 # -*- coding: utf-8 -*- import scrapy import hashlib ...