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

逆序就变成了合并,用并存集判断连通,用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. Gym - 101611D Decoding of Varints(边界值处理)

    Decoding of Varints Statements Varint is a type used to serializing integers using one or more bytes ...

  2. HRBUST - 1819 石子合并问题--圆形版(区间dp+环形+四边形优化)

    石子合并问题--圆形版 在圆形操场上摆放着一行共n堆的石子.现要将石子有序地合并成一堆.规定每次只能选相邻的两堆合并成新的一堆,并将新的一堆石子数记为该次合并的得分.请编辑计算出将n堆石子合并成一堆的 ...

  3. sqlserver2012——INTERSECT交查询

    1. select a.成绩编号,a.分数,b.姓名 From 成绩信息 a,学生信息 b ' 一般的查询 a.成绩编号,a.分数,b.姓名 From 成绩信息 a,学生信息 b ' order a. ...

  4. 洛谷P2939 [USACO09FEB]改造路Revamping Trails

    题意翻译 约翰一共有\(N\))个牧场.由\(M\)条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场\(1\)出发到牧场\(N\)去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰 ...

  5. C# 文件异步操作

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. 一篇关于完全动态凸包的paper(侵删)

    先放原文,挖个坑,到时候再来说人话ε=(´ο`*))) 作者:Franco P. Preparata 出处:Computational geometry An introduction The tec ...

  7. Java带token验证的注册登录

    http://blog.csdn.net/huqingpeng321/article/details/52900550 http://blog.csdn.net/l18710006370/articl ...

  8. (转)Linux内核参数设置sysctl命令详解

    Linux内核参数设置sysctl命令详解 原文:https://www.zhukun.net/archives/8064 sysctl是一个允许您改变正在运行中的Linux系统的接口. 它包含一些 ...

  9. 专题《一》 mysql优化

    从今天开始,在这里记录面试会问的问题,针对java高级开发,架构师方向. 1.数据库设计要合理.开发经验不同  设计表水平不同  影响后面操作 三范式:1------------原子约束,每列不可分割 ...

  10. java中两个map比较

    一 /** * 用map的keySet()的迭代器(性能效率较低) * */ public void compareMap1 (){ Map<String, String> m1 = ne ...