(右偏树)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: ...
随机推荐
- DOM备忘录
nodeName和nodeValue属性 对于element节点而言,nodeName是标签名,nodeValue是null:而对于textNode节点而言,nodeName是#Text,nodeVl ...
- 从零开始学Python--数据类型之字符串
一.Python中的数据类型 · 整数, 如 1 -100 · 长整数, 是比较大的整数,Python 2里面有long长整数:Python 3里面没有 · 浮点数 如 1.23.3E-2 ...
- JVM性能监控与故障处理命令汇总(jps、jstat、jinfo、jmap、jhat、jstack)
给一个系统定位问题的时候,知识.经验是关键基础,数据是依据,工具才是运用知识处理数据的手段 使用适当的虚拟机监控和分析的工具可以加快我们分析数据.定位解决问题的速度,本文主要介绍了几款服 务器上常用的 ...
- hibernate学习(六) flush()和clean()区别和使用
session.flush()是强制和数据库同步 session.clean()是清除session中的缓存 对于批量数据插入的时候优化:减少cpu和内存(缓存)占用量 @Test public vo ...
- 浏览器的 bfcache 特性
一.bfcache 基本概念 现代浏览器在根据历史记录进行前进/后退操作时,会启用缓存机制,名为"bfcache"(back-forward cache,往返缓存),它使页面导航非 ...
- hdu1242 Rescue bfs+优先队列
直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的.遇到Angle的朋友就退出.只需15ms A ...
- Codeforces475D - CGCDSSQ
Portal Description 给出长度为\(n(n\leq10^5)\)的序列\(\{a_n\}\),给出\(q(q\leq3\times10^5)\)个\(x\),对于每个\(x\),求满足 ...
- java的mac自动化-自动运行java程序
本文旨在帮助读者介绍,如果一个测试工程师拿到了mac本,该如何在本地自动运行java代码 首先如图所示写下如下一段代码 package zlr;import org.junit.Test;public ...
- js基本类型与引用类型,浅拷贝和深拷贝
1. 基本类型: string,number,boolean,null,undefined 2. 引用类型: Function,Array,Object 基本类型:存放在栈内存中的简单数据段,数据大小 ...
- 浅析设备管理的MTTR,MTTF,MTBF计算方法
一般来说,对于设备的关键性指标的统计,国际惯例中有三个指标用来进行统计,它们分别是: MTTR(Mean Time To Repair),平均修复时间.计算方法是:总的故障时间/故障次数.计算公式为: ...