(右偏树)Bzoj2333: [SCOI2011]棘手的操作
题面
Sol
右偏树滑稽+并查集
再在全局开一个可删除的堆(priority_queue)
注意细节
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(3e5 + 10);
IL ll Read(){
RG ll x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, a[_], fa[_], add, S[_], top, rt[_];
struct Right_Heap{ int fa, ls, rs, dis, val, tag; } t[_];
struct Heap{
priority_queue <int> A, B;
IL void Push(RG int x){ A.push(x); }
IL void Del(RG int x){ B.push(x); }
IL int Top(){ while(!B.empty() && A.top() == B.top()) A.pop(), B.pop(); return A.top(); }
} Q;
IL void Update(RG int x){ t[x].dis = t[t[x].ls].dis + 1; }
IL void Adjust(RG int x){ if(t[t[x].ls].dis > t[t[x].rs].dis) swap(t[x].ls, t[x].rs); }
IL void Add(RG int x, RG int d){ if(!x) return; t[x].tag += d; t[x].val += d; }
IL void Pushdown(RG int x){ if(!t[x].tag) return; Add(t[x].ls, t[x].tag); Add(t[x].rs, t[x].tag); t[x].tag = 0; }
IL int Find(RG int x){ return fa[x] == x ? x : fa[x] = Find(fa[x]); }
IL int Merge(RG int x, RG int y){
if(!x || !y) return x + y;
Pushdown(x); Pushdown(y);
if(t[x].val < t[y].val) swap(x, y);
RG int tmp = Merge(t[x].ls, y);
t[tmp].fa = x; t[x].ls = tmp;
Adjust(x); Update(x);
return x;
}
IL void Pushall(RG int x){ for(RG int y = x; y; y = t[y].fa) S[++top] = y; while(top) Pushdown(S[top--]); }
IL void Modify(RG int x, RG int d){
Pushall(x);
RG int tmp = Merge(t[x].ls, t[x].rs);
if(t[x].fa){
if(t[t[x].fa].ls == x) t[t[x].fa].ls = tmp;
else t[t[x].fa].rs = tmp;
for(RG int y = t[x].fa; y; y = t[y].fa) Adjust(y), Update(y);
}
t[tmp].fa = t[x].fa; t[x].fa = t[x].ls = t[x].rs = 0;
RG int fx = Find(x);
Q.Del(t[rt[fx]].val);
if(x == rt[fx]) rt[fx] = tmp; t[x].val += d;
rt[fx] = Merge(rt[fx], x); t[rt[fx]].fa = 0;
Q.Push(t[rt[fx]].val);
}
IL void Query(RG int x){ Pushall(x); printf("%d\n", t[x].val + add); }
int main(RG int argc, RG char* argv[]){
n = Read();
for(RG int i = 1; i <= n; ++i) t[i].val = Read(), Q.Push(t[i].val), fa[i] = rt[i] = i;
for(RG int m = Read(); m; --m){
RG char op[5]; RG int x, y, fx, fy;
scanf(" %s", op);
if(op[0] == 'U'){
x = Read(); y = Read(); fx = Find(x); fy = Find(y);
if(fx == fy) continue;
Q.Del(t[rt[fx]].val); Q.Del(t[rt[fy]].val);
fa[fx] = fy; rt[fy] = Merge(rt[fx], rt[fy]); t[rt[fy]].fa = 0;
Q.Push(t[rt[fy]].val);
}
else if(op[0] == 'A'){
if(op[1] == '1') x = Read(), y = Read(), Modify(x, y);
else if(op[1] == '2'){
x = Read(); y = Read(); fx = Find(x);
Q.Del(t[rt[fx]].val); Add(rt[fx], y); Q.Push(t[rt[fx]].val);
}
else x = Read(), add += x;
}
else{
if(op[1] == '1') x = Read(), Query(x);
else if(op[1] == '2') x = Read(), fx = Find(x), printf("%d\n", t[rt[fx]].val + add);
else printf("%d\n", Q.Top() + add);
}
}
return 0;
}
(右偏树)Bzoj2333: [SCOI2011]棘手的操作的更多相关文章
- BZOJ2333 [SCOI2011]棘手的操作 堆 左偏树 可并堆
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2333 题意概括 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i ...
- [bzoj2333] [SCOI2011]棘手的操作 (可并堆)
//以后为了凑字数还是把题面搬上来吧2333 发布时间果然各种应景... Time Limit: 10 Sec Memory Limit: 128 MB Description 有N个节点,标号从1 ...
- 真--可并堆模板--BZOJ2333: [SCOI2011]棘手的操作
n<=300000个点,开始是独立的,m<=300000个操作: 方法一:单点修改.查询,区间修改.查询?等等等等这里修改是块修改不是连续的啊,那就让他连续呗!具体方法:离线后,每次连接两 ...
- bzoj千题计划218:bzoj2333: [SCOI2011]棘手的操作
http://www.lydsy.com/JudgeOnline/problem.php?id=2333 上次那个是线段树,再发一个左偏树 维护两种左偏树 第一种是对每个联通块维护一个左偏树 第二种是 ...
- BZOJ2333 [SCOI2011]棘手的操作 【离线 + 线段树】
题目 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 x v: 将第x个节点的权 ...
- bzoj千题计划217:bzoj2333: [SCOI2011]棘手的操作
http://www.lydsy.com/JudgeOnline/problem.php?id=2333 读入所有数据,先模拟一遍所有的合并操作 我们不关心联通块长什么样,只关心联通块内有谁 所以可以 ...
- bzoj2333 [SCOI2011]棘手的操作(洛谷3273)
题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作:U x y: 加一条边,连接第x个节点和第y个节点A1 x v: 将第x个节点的权 ...
- 2019.01.17 bzoj2333: [SCOI2011]棘手的操作(启发式合并)
传送门 启发式合并菜题. 题意:支持与连通块有关的几种操作. 要求支持连边,单点修改,连通块修改,全局修改和单点查值,连通块查最大值和全局最大值. 我们对每个连通块和答案用可删堆维护最大值,然后用启发 ...
- BZOJ2333:[SCOI2011]棘手的操作(Splay)
Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 x v: ...
随机推荐
- nginx上支持.htaccess伪静态的配置实例
本文介绍下,在nginx上配置.htaccess伪静态的方法,有需要的朋友参考下吧. 在apache上.htaccess转向,只要apache编译的时候指明支持rewrite模块即可. 但是换到ngi ...
- elasticsearch 6 在 centos 6 上的安装问题
ERROR: bootstrap checks failed max file descriptors [4096] for elasticsearch process likely too low, ...
- win7本地连接消失
可能原因一 驱动程序错误: 右键"计算机"图标 - "属性" - 设备管理器现在在设备管理器下面找到"网络适配器",在下面的网卡驱动中发现有 ...
- FFT模板(多项式乘法)
FFT模板(多项式乘法) 标签: FFT 扯淡 一晚上都用来捣鼓这个东西了...... 这里贴一位神犇的博客,我认为讲的比较清楚了.(刚好适合我这种复数都没学的) http://blog.csdn.n ...
- Activt工作流数据库对应表的作用
1.资源库流程规则表 1) act_re_deployment 部署信息表 2) act_re_model 流程设计模型部署表 3) ...
- Django在form提交CSRF验证失败. 相应中断问题
CSRF验证失败. 相应中断. 1).首先,我们可以先看一下出现问题的所在的原因. Your browser is accepting cookies. The view function passe ...
- perl的foreach循环的坑
最近在写perl脚本的时候用foreach遍历hash的时候,出现遇到了一个问题,就是说当hash为一层的时候,并不会有问题,但是当hash类型结构比较复杂的时候,就会有需要注意的地方了. 还是举例子 ...
- react-dom.js 源码
/** *以下这是 react-dom.js 的源代码 * ReactDOM v15.3.1 * * Copyright 2013-present, Facebook, Inc. * All righ ...
- R语言︱LDA主题模型——最优主题数选取(topicmodels)+LDAvis可视化(lda+LDAvis)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 笔者寄语:在自己学LDA主题模型时候,发现该模 ...
- OkHttp使用教程——网络操作之OkHttp, Volley以及Gson
写这篇文章的动机 在安卓项目中有一个问题可能无法避免:网络.不管你是加载图片,请求API数据还是从因特网上获得一个字节,你都是在使用网络. 鉴于网络在安卓中的重要性与基础性,当今安卓开发者面临的问题之 ...