欢迎访问~原文出处——博客园-zhouzhendong

去博客园看该题解


题目传送门 - BZOJ2333


题意概括

有N个节点,标号从1到N,这N个节点一开始相互不连通。第i个节点的初始权值为a[i],接下来有如下一些操作:

U x y: 加一条边,连接第x个节点和第y个节点

A1 x v: 将第x个节点的权值增加v

A2 x v: 将第x个节点所在的连通块的所有节点的权值都增加v

A3 v: 将所有节点的权值都增加v

F1 x: 输出第x个节点当前的权值

F2 x: 输出第x个节点所在的连通块中,权值最大的节点的权值

F3: 输出所有节点中,权值最大的节点的权值


题解

  我比较懒。

  这是一道坑坑的细节题。

  我调了大约6个小时。

  对于A3,我们只需要保存一个全局变量,每次输出的时候加上去就可以了。

  对于F3,我们只需要弄一个multiset就可以了。

  对于U、A1、A2、F1、F2,就是左偏树的几个基本操作。

  “嘴巴上AC就是简单”


代码

#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
const int N=300005,Inf=1e8;
int n,m,addv=0;
struct Tree{
int fa,ls,rs,v,add,len;
Tree (){}
Tree (int a,int b,int c,int d,int e,int f){
fa=a,ls=b,rs=c,v=d,add=e,len=f;
}
}t[N];
multiset <int> rts;
void check0(){
t[0]=Tree(0,0,0,-Inf,0,-1);
}
int getroot(int x){
check0();
while (t[x].fa)
x=t[x].fa;
return x;
}
void del(int x){
rts.erase(rts.find(x));
}
void pushdown(int x){
check0();
int s,&add=t[x].add;
if (!t[x].add)
return;
s=t[x].ls;
if (s)
t[s].add+=add,t[s].v+=add;
s=t[x].rs;
if (s)
t[s].add+=add,t[s].v+=add;
add=0;
}
void pushadd(int x){
check0();
if (t[x].fa)
pushadd(t[x].fa);
pushdown(x);
}
int merge(int a,int b){
check0();
if (a==b)
return a;
if (a==0)
return b;
if (b==0)
return a;
if (t[a].v<t[b].v)
swap(a,b);
pushdown(a);
t[a].rs=merge(t[a].rs,b);
t[t[a].rs].fa=a;
if (t[t[a].ls].len<t[t[a].rs].len)
swap(t[a].ls,t[a].rs);
t[a].len=t[t[a].rs].len+1;
return a;
}
void change(int x,int val){
check0();
pushadd(x);
int rt=getroot(x),maxv=t[rt].v;
int fa=t[x].fa,s=merge(t[x].ls,t[x].rs);
if (fa==0&&s==0){
del(t[x].v);
t[x].v=val;
rts.insert(t[x].v);
return;
}
t[s].fa=0;
if (fa){
t[s].fa=fa;
if (t[fa].ls==x)
t[fa].ls=s;
else
t[fa].rs=s;
}
t[x]=Tree(0,0,0,val,0,0);
merge(x,getroot(s?s:fa));
int maxv_=t[getroot(x)].v;
if (maxv_!=maxv){
del(maxv);
rts.insert(maxv_);
}
}
int main(){
scanf("%d",&n);
check0();
rts.clear();
for (int i=1,v;i<=n;i++){
scanf("%d",&v);
t[i]=Tree(0,0,0,v,0,0);
rts.insert(v);
}
scanf("%d",&m);
while (m--){
char op[5],c1,c2;
int a,b;
scanf("%s",op),c1=op[0],c2=op[1];
if (c1=='U'){
scanf("%d%d",&a,&b);
a=getroot(a),b=getroot(b);
if (a==b)
continue;
if (t[a].v<t[b].v)
swap(a,b);
del(t[b].v);
merge(a,b);
}
else if (c1=='A'){
if (c2=='1')
scanf("%d%d",&a,&b),pushadd(a),change(a,t[a].v+b);
else if (c2=='2'){
scanf("%d%d",&a,&b);
a=getroot(a);
del(t[a].v);
t[a].add+=b,t[a].v+=b;
rts.insert(t[a].v);
}
else
scanf("%d",&a),addv+=a;
}
else {
if (c2=='1')
scanf("%d",&a),pushadd(a),printf("%d\n",t[a].v+addv);
else if (c2=='2')
scanf("%d",&a),printf("%d\n",t[getroot(a)].v+addv);
else
printf("%d\n",*--rts.end()+addv);
} }
return 0;
}

  

BZOJ2333 [SCOI2011]棘手的操作 堆 左偏树 可并堆的更多相关文章

  1. 【BZOJ2333】棘手的操作(左偏树,STL)

    [BZOJ2333]棘手的操作(左偏树,STL) 题面 BZOJ上看把... 题解 正如这题的题号 我只能\(2333\) 神TM棘手的题目... 前面的单点/联通块操作 很显然是一个左偏树+标记 ( ...

  2. Luogu P3273 [SCOI2011]棘手的操作(左偏树)

    什么恶心东西啊,又水又烦 两个可并堆维护即可 #include <cstdio> #include <iostream> #include <cstring> #i ...

  3. 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树|可并堆-左偏树)

    2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...

  4. BZOJ2333 [SCOI2011]棘手的操作 【离线 + 线段树】

    题目 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 x v: 将第x个节点的权 ...

  5. [note]左偏树(可并堆)

    左偏树(可并堆)https://www.luogu.org/problemnew/show/P3377 题目描述 一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 ...

  6. bzoj2809 [Apio2012]dispatching——左偏树(可并堆)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2809 思路有点暴力和贪心,就是 dfs 枚举每个点作为管理者: 当然它的子树中派遣出去的忍者 ...

  7. [bzoj2333] [SCOI2011]棘手的操作 (可并堆)

    //以后为了凑字数还是把题面搬上来吧2333 发布时间果然各种应景... Time Limit: 10 Sec  Memory Limit: 128 MB Description 有N个节点,标号从1 ...

  8. bzoj千题计划218:bzoj2333: [SCOI2011]棘手的操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=2333 上次那个是线段树,再发一个左偏树 维护两种左偏树 第一种是对每个联通块维护一个左偏树 第二种是 ...

  9. 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树)

    2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...

随机推荐

  1. webuploader

    https://www.cnblogs.com/study-fanzeng/p/8930939.html http://fex.baidu.com/webuploader/doc/index.html ...

  2. java 调用windows的COM组件举例(使用JACOB)

    java 调用windows的COM组件举例(使用JACOB) (转自这里) 最近公司需要做一个效果,开发一个程序能在程序运行时打开microsoft office的相关软件,实时写入,然后能关闭,你 ...

  3. u-boot移植(十三)---代码修改---裁剪及环境变量 一

    一.内核裁剪 内核的裁剪首先就是修改我们的配置文件,即 include/configs/jz2440.h 文件,里面定义的很多宏,我们也许用不上的就要去掉. /* * (C) Copyright 20 ...

  4. gulp+webpack构建配置

    使用构建工具之前我觉得前端好蠢,css没有变量,不能写循环,为了兼容要写好多前缀,hmtl写多页面中有同一个header,我就粘贴复制,然后修改的时候每个都要改. 我还不会压缩和合并,每次都要按F5刷 ...

  5. Problem F Plug It In!

    题目链接:https://cn.vjudge.net/contest/245468#problem/F 大意:给你插座和电器的对应关系,有多个电器对应一个插座的情况,但是一个插座只能供一个电器使用,现 ...

  6. Attempting to badge the application icon but haven't received permission from the user to badge the application错误解决办法

    今天刚刚学习UIApplication对象,当我希望利用这个对象在我们的应用图标上显示个数字的时候,xcode报了这个错误:  解决办法 : - (IBAction)applicationClicke ...

  7. Dubbo高可用

    高可用:通过设计减少系统不能提供服务的时间 (1).zookeeper宕机 原因:zookeeper宕机 现象:zookeeper注册中心宕机,还可以消费dubbo暴露的服务. 健壮性: 监控中心宕掉 ...

  8. Python中的exec、eval使用实例

    Python中的exec.eval使用实例 这篇文章主要介绍了Python中的exec.eval使用实例,本文以简洁的方式总结了Python中的exec.eval作用,并给出实例,需要的朋友可以参考下 ...

  9. Linux之定时器与时间管理 【转】

    转自:http://blog.chinaunix.net/uid-23228758-id-154820.html 定时器与时间管理: 次,为一秒.一般的情况下编程者不要改变这个值,因为内核编很多代码都 ...

  10. GPIO推挽输出和开漏输出详解

    open-drain与push-pull] GPIO的功能,简单说就是可以根据自己的需要去配置为输入或输出.但是在配置GPIO管脚的时候,常会见到两种模式:开漏(open-drain,漏极开路)和推挽 ...