删除边的操作不容易实现,那么就先离线然后逆序来做。

逆序就变成了合并,用并存集判断连通,用Treap树来维护一个连通分量里的名次。

Treap = Tree + Heap。用一个随机的优先级来平衡搜索树。

名次查询需要维护树的结点数量,假设当前在u点,u的左子树有n个结点,那么u的就是以u为根的树上第n+1小的。

如果查询的不是n+1,那么根据结点数量判断一下在哪颗子树上,然后去查询。

树的合并就将结点数少的树上的点往结点数多的树里面插,然后删掉结点少的树。

修改权值就分解成删除点和插点。

写的时候要分清哪些指针本身是要修改的,要用引用。哪些指针可能是NULL,不应该访问。

试过将null设定为常指针,快了25ms,用内存池模拟new 分配内存,快了50ms,但是结点数要开到maxn的两倍,(在这RE了很多次)。

如果new 的效率足够的话还是不要用内存池了,搞不好就RE了。

这题刷新了挂题发数,不过总算是会写Treap了。

#include<bits/stdc++.h>
using namespace std; struct Node
{
Node *ch[];
int v,r,s;
void maintain()
{
s = +ch[]->s+ch[]->s;
}
}; Node *const null = new Node(); inline Node *newNode(int x)
{
Node* t = new Node();
t->ch[]=t->ch[] = null;
t->s = ; t->r = rand(); t->v = x;
return t;
} void rot(Node*&o,int d)
{
Node*t = o->ch[d^]; o->ch[d^] = t->ch[d]; t->ch[d] = o;
o->maintain(); t->maintain();
o = t;
} void inst(Node*&o,int x)
{
if(o==null){
o = newNode(x);
}else {
int d = x > o->v ? :;
inst(o->ch[d],x);
if(o->ch[d]->r > o->r) rot(o,d^);
}
o->maintain();
} inline int tcmp(int a,int b)
{
if(a == b) return -;
return a > b? : ;
} void rmov(Node*&o,int x)
{
//if(o == null) return;
int d = tcmp(o->v,x);
if(~d){
rmov(o->ch[d],x);
}else {
Node*lc = o->ch[],*rc = o->ch[];
if(lc!=null &&rc != null){
int d2 = lc->r > rc->r? :;
rot(o,d2); rmov(o->ch[d2],x);
}else {
Node *t = o;
if(lc == null) o = rc;
else o = lc;
delete t;
}
}
if(o != null) o->maintain();
} const int maxc = 5e5+, maxn = 2e4+, maxm = 6e4+;
struct Cmd
{
char tp;
int x,p;
}cmd[maxc]; int n,m,wei[maxn],fro[maxm],to[maxm],rmvd[maxm]; int pa[maxn];
int fdst(int x){ return x==pa[x]?x:pa[x]=fdst(pa[x]); } Node *rt[maxn]; //k>0
int kth(Node*o,int k)
{
if(o == null || k <= || k > o->s) return ; //
int s = o->ch[]->s;
if(k == s+) return o->v;
if(k <= s) return kth(o->ch[],k);
return kth(o->ch[],k-s-);
} void mgto(Node*&u,Node*&v)
{
if(u->ch[] != null) mgto(u->ch[],v);
if(u->ch[] != null) mgto(u->ch[],v);
inst(v,u->v);
delete u;
u = null;
} void rmvTree(Node*&o)
{
if(o->ch[] != null) rmvTree(o->ch[]);
if(o->ch[] != null) rmvTree(o->ch[]);
delete o;
o = null;
} inline void addEdge(int i)
{
int u = fdst(fro[i]), v = fdst(to[i]);
if(u != v){
if(rt[u]->s < rt[v]->s){
pa[u] = v;
mgto(rt[u],rt[v]);
}else {
pa[v] = u;
mgto(rt[v],rt[u]);
}
}
} int qct;
long long qtot; inline void qry(int x,int p)
{
if(p>){
qtot+=kth(rt[fdst(x)],p);
}
qct++;
} inline void chgw(int x,int p)
{
int u = fdst(x);
rmov(rt[u],wei[x]);
inst(rt[u],wei[x]=p);
} int main()
{
//freopen("in.txt","r",stdin);
int kas = ;
null->s = ; null->ch[] = null->ch[] = null;
fill(rt,rt+maxn,null);
while(~scanf("%d%d",&n,&m)&&n){
for(int i = ; i <= n; i++) scanf("%d",wei+i);
for(int i = ; i <= m; i++) scanf("%d%d",fro+i,to+i);
kas++; int c = ;
while(true){
char tp; int x,p;
scanf(" %c",&tp);
if(tp == 'E') break;
scanf("%d",&x);
if(tp == 'D') rmvd[x] = kas;
else if(tp == 'Q') scanf("%d",&p);
else if(tp == 'C') {
p = wei[x];
scanf("%d",wei+x);
}
cmd[c++] = {tp,x,p};
} for(int i = ; i <= n; i++){
pa[i] = i;
if(rt[i] != null) rmvTree(rt[i]);
rt[i] = newNode(wei[i]);
}
for(int i = ; i <= m; i++) if(rmvd[i] != kas) addEdge(i); qtot = qct = ;
while(c--){
Cmd &cq = cmd[c];
if(cq.tp == 'Q') qry(cq.x,cq.p);
else if(cq.tp == 'C') chgw(cq.x,cq.p);
else if(cq.tp == 'D')addEdge(cq.x);
}
printf("Case %d: %.6lf\n",kas,qtot/(double)qct);
}
return ;
}

UVALive 5031 Graph and Queries (Treap)的更多相关文章

  1. UVA 1479 Graph and Queries (Treap)

    题意: 给一个无向图,再给一系列操作(以下3种),输出最后的平均查询结果. (1)D X 删除第x条边. (2)Q X k  查询与点X相连的连通分量中第k大的点的权值. (3)C X v  将点X的 ...

  2. UVaLive 5031 Graph and Queries (Treap)

    题意:初始时给出一个图,每个点有一个权值,三种操作:(1)删除某个边:(2)修改每个点的权值:(3)询问与节点x在一个连通分量中所有点的第K大的权值. 析:首先是要先离线,然后再倒着做,第一个操作就成 ...

  3. uvalive 5031 Graph and Queries 名次树+Treap

    题意:给你个点m条边的无向图,每个节点都有一个整数权值.你的任务是执行一系列操作.操作分为3种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...

  4. HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)

    Description You are given an undirected graph with N vertexes and M edges. Every vertex in this grap ...

  5. UVALive - 5031 Graph and Queries (并查集+平衡树/线段树)

    给定一个图,支持三种操作: 1.删除一条边 2.查询与x结点相连的第k大的结点 3.修改x结点的权值 解法:离线倒序操作,平衡树or线段树维护连通块中的所有结点信息,加个合并操作就行了. 感觉线段树要 ...

  6. 2021.12.07 P4291 [HAOI2008]排名系统(Treap)

    2021.12.07 P4291 [HAOI2008]排名系统(Treap) https://www.luogu.com.cn/problem/P4291 双倍经验: https://www.luog ...

  7. HDU 3726 Graph and Queries (离线处理+splay tree)

    Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. 洛谷P3369普通平衡树(Treap)

    题目传送门 转载自https://www.cnblogs.com/fengzhiyuan/articles/7994428.html,转载请注明出处 Treap 简介 Treap 是一种二叉查找树.它 ...

  9. 树堆(Treap)

    平衡树 简介: 平衡二叉树(Balanced Binary Tree)具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.平衡二叉树的常用实现方 ...

随机推荐

  1. Do not have XXX handler in current page

    这种错误没有什么技术含量,也很容易解决. 一般就是wxml里面的button/form之类的,你用bindtap/bindsubmit给它绑了一个XXX函数,但是呢,你没有在相关js页面里面定义这个函 ...

  2. PostgreSQL 务实应用(五/5)常用表达

    在实际应用中,对于具体的数据计算我们会找相应的函数来实现.而计算需求不同的表达,往往会使得我们使用不同的函数或方式来实现.或者也可以说,同一计算可以使用多种不同的表达方式实现. PostgreSQL ...

  3. Maven修改默认仓库为阿里云仓库

    Maven 仓库默认在国外, 国内使用难免很慢,我们可以更换为阿里云的仓库. 第一步:修改 maven 根目录下的 conf 文件夹中的 setting.xml 文件,在 mirrors 节点上,添加 ...

  4. [原创]内网SSH密码爆破工具sshcrack(配合Cscan批量弱口令检测)

    0x000 前言 sshcrack是一个命令行下的SSH密码爆破工具,适用于内渗中SSH密码检测 当然也可用于外网SSH密码爆破,支持Windows/Linux,其它系统未测.Tip1 0x001 目 ...

  5. SpringBoot2.0 基础案例(03):配置系统全局异常映射处理

    一.异常分类 这里的异常分类从系统处理异常的角度看,主要分类两类:业务异常和系统异常. 1.业务异常 业务异常主要是一些可预见性异常,处理业务异常,用来提示用户的操作,提高系统的可操作性. 常见的业务 ...

  6. HDMI和VGA接口

    HDMI英文全称为:High Definition Multimedia Interface,它是一种全数字化视频和声音发送接口,可以发送未压缩的音频及视频信号. HDMI接口与VGA接口区别如下: ...

  7. 源码分析(一) 进程cleos的命令解析

    EOS版本:4.0   一.进程cleos的作用   cleos,即为client eos.从名字就可以猜出来,它是一个标准的客户端程序,而实际上,它也确实为一个标准的client^_^   准确地说 ...

  8. Java - 一道关于整型和字符类型相加的题目

    题目 public class Test { public static void main(final String[] args) { final int a = 10; final int b ...

  9. oralce9i部署安装

    为什么还学习oracle9i,因为目前大多数企业的数据依然存储在oracle9i上面,对于数据升级存在很大风险,因此在学习oralce之前,首先熟悉oracle9i也是很有必要的.现在我们先来学习or ...

  10. ES6新特性使用小结(四)

    十一.Proxy .Reflect ①.Proxy 的概念和常用方法 { let obj = { //1.定义原始数据对象 对用户不可见 time: '2017-09-20', name: 'net' ...