#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. BZOJ 4734 UOJ #269 [清华集训2016]如何优雅地求和 (多项式)

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

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

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

  3. UOJ 267 [清华集训2016]魔法小程序

    前缀和 非常认真地观察代码之后我们会发现实际上这是一个多维前缀和问题,c就记录前缀和.考虑前缀和的逆操作,即可复原出原数组. 提示:k维前缀和可以看作k-1维前缀和的前缀和,从而递归下去 #inclu ...

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

    题目链接:http://uoj.ac/problem/274 题意概述: 没什么好概述的......概述了题意就知道怎么做了......我懒嘛 分析: 就是用lct维护最大生成树. 然后如果去UOJ上 ...

  5. [清华集训2016]石家庄的工人阶级队伍比较坚强——三进制FWT

    题目链接: [清华集训2016]石家庄的工人阶级队伍比较坚强 题目大意:有$n=3^m$个人玩石头剪刀布,共$t$轮游戏,每轮每个人要和包括自己的所有人各进行$m$次石头剪刀布.每个人在$m$轮中的决 ...

  6. 【清华集训2016】数据交互

    [清华集训2016]数据交互 比较神的\(DDP\). 首先对于给出的一条链我们分两部分统计:\(lca\)以及其他部分. 我们设两个变量\(w_i,g_i\).一条路径的权值就是路径上所有点的\(w ...

  7. P6669 [清华集训2016] 组合数问题

    P6669 [清华集训2016] 组合数问题 题意: 给你n,m,k,问有多少对(i,j)满足K∣CijK|C_{i}^{j}K∣Cij​ (Cij是k的倍数C_{i}^{j}是k的倍数Cij​是k的 ...

  8. [清华集训2016]你的生命已如风中残烛——组合数学

    题目链接: [清华集训2016]你的生命已如风中残烛 题目大意:共有$m+1$张牌,其中有$n$张特殊牌,每张特殊牌有一个权值$w_{i}$表示取到这张牌能获得$w_{i}$次再抽牌的机会,保证$\s ...

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

    寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 "冻死宝宝了!" 这时 远处的天边出现了一位火焰之神 "我将赐予你们温暖和希望! ...

最新文章

  1. iphone smtp服务器没有响应,电子邮件卡在iPhone或iPad上的发件箱?如何修复iOS中的未发送邮件 | MOS86...
  2. Castle ActiveRecord学习(四)延迟加载、分页查询、where条件
  3. 删除android软件,adb shell删除Android系统下的软件
  4. Linux正确的关机方式
  5. python 福利彩票_使用Python买福彩,5个数字,20选5,有没买过
  6. jq父级绑定事件的意义_js、jq事件绑定方式总结——以click事件为例
  7. 实验十一 连接数据库实验(V2.0)
  8. 湖南工程学院毕业论文计算机,bbs毕业设计
  9. mac中强大的快捷键
  10. 华硕笔记本 X550JD4710HQ
  11. php提示返回,PHP指定方法的返回类型提示
  12. 深度学习(DL)-1.3 浅层神经网络 (Shallow neural networks)
  13. 线粒体靶向的纳米递送PCN-224 纳米粒子-瑞禧
  14. 政府不能替代微软“查户口”
  15. over()分析函数
  16. js执行机制经典面试题(一)
  17. Python最详细的 机器学习算法:逻辑回归的推导及实战 你值得拥有!
  18. win10锁屏壁纸文件夹位置
  19. phpcms环境检测MySQL_WNMP nginx+php5+mysql测试环境安装(Windows7)(一)
  20. WordPress绑定多个域名和禁止搜索引擎收录非主域名的方法

热门文章

  1. oracle 前端ui框架,Layui(前端UI框架) 2.6.4 官方最新版
  2. matlab python比较_MATLAB与Python的比较
  3. 疑邻盗斧 - 杭电节能信标限流争论
  4. 2021年春季学期-信号与系统-第十五次作业参考答案-第八小题参考答案
  5. MATLAB在AI方面的应用工具
  6. 利用MATLAB帮助求解作业中的Laplace变换和Z变换
  7. 微型计算机一般按字长进行分类,关于计算机中:字,字节,字长,位的关系
  8. php处理html5文件上传代码,HTML5中文件上传的代码
  9. css媒体查询标准,CSS 媒体查询 - 实例
  10. 东莞厚街工业机器人展会_工业机器人四大家族齐聚!东莞将在12月举办智博会...