题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2333

。。题意概述就不写了,各位老爷如果是看着玩的可以去搜一下,如果是做题找来的也知道题干的。实际上是题干无法缩减懒得复制ORZ

首先处理一下集合的合并和单点值查询的问题。使用并查集,记录两个数组w,d:w记录对这个点单点操作的值,d记录对这个点代表的集合进行的操作累计的值,对于每个点find的时候把这个点到代表元路径上的点的d(不包括代表元)的d加起来更新这个点的d,每一次查询某个点的当前值的时候就先find就可以直接用w+d+代表元的d(特判这个点是不是代表元)回答。特别注意为了保证正确性在merge的时候要把双方中的某一个点建立成另外一个新点,原来两个点的pa是这个新点。这样值的集合修改和查询就解决。

接下来是最大值的问题。这里用的是可并堆。开两个可并堆,一个维护每个集合(称为hp1),另一个维护每个集合中的最大值(称为hp2)。有点lazy的思想,因为单点修改只会影响这个点的值,所以说直接在hp1中调整这个点的位置(注意到可能是向下,也可能是向上),然后看此集合中最大值对应的元素编号是否改变。改变的话就在hp2中删掉原来的最大元素编号加入新的,否则如果修改的这个点就是其集合中的最大值元素就在hp2中调整位置;如果是集合修改的话思路同单点直接调整被修改集合中最大值在hp2中的位置;对于所有值修改的操作直接单独记录一个数输出的时候加上就可以了(不影响单调性)。

这样调整之后任意时刻可并堆中的所有元素的位置都是正确的,正确性得以保证(虽然这个自己yy出来的东西代码有点长?)

最后说一件事情,自己乱搞数据结构的时候一定注意。。。。。指针要改完改对。。。。。

细节参见代码。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
using namespace std;
const int MAXN=; int N,Q,A[MAXN];
struct union_find{
static const int maxn=;
int pa[maxn<<],stk[maxn<<],d[maxn<<],id[maxn<<],w[maxn<<],stk_top,np,ADD;
union_find(){ np=stk_top=ADD=; };
int newnode(int x) { w[++np]=x,pa[np]=np,d[np]=; return np; }
void initial(int n,int *a){
for(int i=;i<=n;i++) pa[i]=id[i]=i,w[i]=a[i],d[i]=;
np=n;
}
int find(int x)
{
while(pa[x]!=x) stk[++stk_top]=x,x=pa[x];
int rt=x,add=;
while(stk_top) x=stk[stk_top],add+=d[x],d[x]=add,pa[x]=rt,stk_top--;
return rt;
}
int val(int x) { find(x); return w[x]+d[x]+(pa[x]==x?:d[pa[x]])+ADD; }
bool judge(int x,int y) { return find(x)==find(y); }
void merge(int x,int y) { pa[find(x)]=pa[find(y)]=newnode(val(y)-ADD); }
}uf;
struct mergeable_heap{
static const int maxn=;
int chd[maxn][],fa[maxn];
void initial(int n) { for(int i=;i<=n;i++) chd[i][]=chd[i][]=fa[i]=; }
int val(int x) { return uf.val(uf.id[x]); }
void link(int x,int d,int y) { chd[x][d]=y,fa[y]=x; }
int root(int x) { while(fa[x]) x=fa[x]; return x; }
int merge(int A,int B)
{
if(!A||!B) return A+B;
if(val(A)<val(B)) swap(A,B);
link(A,,merge(chd[A][],B)); swap(chd[A][],chd[A][]);
return A;
}
void ins(int A,int B) { fa[A]=chd[A][]=chd[A][]=; merge(A,B); }
void del(int A)
{
if(A==root(A)) fa[merge(chd[A][],chd[A][])]=;
else{
int d=A==chd[fa[A]][];
link(fa[A],d,merge(chd[A][],chd[A][]));
}
}
int top(int x) { return val(root(x)); }
void rot(int x)
{
int p=fa[x],e=x==chd[p][];
int a=chd[x][],b=chd[x][],c=chd[p][e],d=fa[p];
link(p,,a); link(p,,b);
link(x,,e?p:c); link(x,,e?c:p);
link(d,chd[d][]==p,x);
}
void adjust(int x)
{
while(fa[x]&&val(x)>val(fa[x])) rot(x);
while(chd[x][]||chd[x][]){
int y;
if(!chd[x][]||!chd[x][]) y=chd[x][]?chd[x][]:chd[x][];
else y=val(chd[x][])>val(chd[x][])?chd[x][]:chd[x][];
if(val(y)<=val(x)) break;
rot(y);
}
}
}hp1,hp2; void _scanf(char &x)
{
x=getchar();
while(x!='U'&&x!='A'&&x!='F') x=getchar();
}
void data_in()
{
scanf("%d",&N);
for(int i=;i<=N;i++) scanf("%d",&A[i]);
scanf("%d",&Q);
}
void work()
{
uf.initial(N,A);
hp1.initial(N); hp2.initial(N);
for(int i=;i<N;i++)
hp2.merge(hp2.root(i),i+);
char op1; int op2,x,y,v,rx,ry;
for(int i=;i<=Q;i++){
_scanf(op1);
if(op1=='U'){
scanf("%d%d",&x,&y);
if(!uf.judge(uf.id[x],uf.id[y])){
rx=hp1.root(x),ry=hp1.root(y);
uf.merge(uf.id[x],uf.id[y]);
hp1.merge(rx,ry);
if(rx!=hp1.root(x)) hp2.del(rx); else hp2.del(ry);
}
}
else if(op1=='A'){
scanf("%d",&op2);
if(op2==){
scanf("%d%d",&x,&v);
rx=hp1.root(x);
uf.w[uf.id[x]]+=v; hp1.adjust(x);
if(rx!=hp1.root(x)){
int rt=max(hp2.fa[rx],max(hp2.chd[rx][],hp2.chd[rx][]));
hp2.del(rx); rt=hp2.root(rt);
hp2.ins(hp1.root(x),rt);
}
else if(rx==x) hp2.adjust(x);
}
else if(op2==){
scanf("%d%d",&x,&v);
uf.d[uf.find(uf.id[x])]+=v;
hp2.adjust(hp1.root(x));
}
else if(op2==) scanf("%d",&v),uf.ADD+=v;
}
else if(op1=='F'){
scanf("%d",&op2);
if(op2==) scanf("%d",&x),printf("%d\n",hp1.val(x));
else if(op2==) scanf("%d",&x),printf("%d\n",hp1.top(x));
else if(op2==) printf("%d\n",hp2.top(hp1.root()));
}
}
}
int main()
{
data_in();
work();
return ;
}

BZOJ 2333 SCOI2011 棘手的操作 并查集+可并堆的更多相关文章

  1. BZOJ 2333: [SCOI2011]棘手的操作

    题目描述 真的是个很棘手的操作.. 注意每删除一个点,就需要clear一次. #include<complex> #include<cstdio> using namespac ...

  2. BZOJ 2333 [SCOI2011]棘手的操作 (可并堆)

    码农题.. 很显然除了两个全局操作都能用可并堆完成 全局最大值用个multiset记录,每次合并时搞一搞就行了 注意使用multiset删除元素时 如果直接delete一个值,会把和这个值相同的所有元 ...

  3. BZOJ 2333: [SCOI2011]棘手的操作 可并堆 左偏树 set

    https://www.lydsy.com/JudgeOnline/problem.php?id=2333 需要两个结构分别维护每个连通块的最大值和所有连通块最大值中的最大值,可以用两个可并堆实现,也 ...

  4. bzoj 2333 [SCOI2011]棘手的操作 —— 可并堆

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2333 稍微复杂,参考了博客:http://hzwer.com/5780.html 用 set ...

  5. 2333: [SCOI2011]棘手的操作[离线线段树]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2325  Solved: 909[Submit][Stat ...

  6. 2333: [SCOI2011]棘手的操作[写不出来]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1979  Solved: 772[Submit][Stat ...

  7. 2333: [SCOI2011]棘手的操作[我不玩了]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1979  Solved: 772[Submit][Stat ...

  8. 【BZOJ】2333: [SCOI2011]棘手的操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=2333 题意: 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i], ...

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

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

随机推荐

  1. 为什么IP检验和发现错误直接丢弃而不是要求源站重发

    纠错控制由上层(传输层)执行IP首部中的源站地址也可能出错,请错误的源地址重传数据报是 没有意义的

  2. C/C++使用Socket通信UDP

    接收端 #include <stdio.h> #include <WinSock2.h> #pragma comment(lib,"WS2_32.lib") ...

  3. Zookeeper 面试总结

    1:Zookeeper是什么? 答:ZooKeeper是一个开源的分布式协调服务,是集群的管理者,监视集群节点反馈信息进行下一步合理操作. Zookeeper提供的服务:管理用户程序提交的数据:为用户 ...

  4. shell编程基础进阶

    为什么学习shell编程 shell脚本语言是实现linux/unix 系统管理机自动化运维所必备的重要工具,linux/unix系统的底层及基础应用软件的核心大部分涉及shell脚本的内容.每一个合 ...

  5. (转)Dubbo 简单Dome搭建

    (转)原地址https://blog.csdn.net/noaman_wgs/article/details/70214612/ Dubbo背景和简介 Dubbo开始于电商系统,因此在这里先从电商系统 ...

  6. jQuery 切换图片(图标)效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. is 和 isinstance的区别 and issubclass

    定义一个子类和父类 class A: pass class B(A): pass is print(type(b) is B) # 结果: True print(type(b) is A) # 结果: ...

  8. PHP入门笔记--基础语法二

    一.函数 自定义函数 任何有效的 PHP 代码都有可能出现在函数内部,甚至包括其它函数和类定义. <?php function foo() { function bar() { echo &qu ...

  9. JavaScript之原型 Prototype

    1.我们所创建的每一个函数,解析器都会向函数中添加一个属性prototype.这个属性对应着一个对象,这个对象就是我们所谓的原型对象.如果函索作为普通函数调用prototype没有任何作用. 当函数以 ...

  10. 用filter()筛选出素数

    'use strict'; function get_primes(arr) { return arr.filter(function isPrime(number) { if (typeof num ...