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

逆序就变成了合并,用并存集判断连通,用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. ASP.NET自定义控件组件开发

    ASP.NET的开发都是事件驱动的,现在我们就来为控件添加事件.在事件之前 对委托事件要要熟悉. 其实定义事件的步骤很简单: 1.声明一个委托. 2.定义一个携带事件信息的类. 3.定义事件 4.定义 ...

  2. ASP.NET学习笔记(二)语法

    基本的 ASP 语法规则 通常情况下,ASP 文件包含 HTML 标签,类似 HTML 文件.不过,ASP 文件也能够包含服务器端脚本,这些脚本被分隔符 <% 和 %> 包围起来. 在 A ...

  3. IOSerialize,xml和json,soap序列化器,二进制序列化器,XML序列化器,文件 检查、新增、复制、移动、删除

    1 文件夹/文件 检查.新增.复制.移动.删除,2 文件读写,记录文本日志/读取配置文件3 三种序列化器4 xml和json1.文件夹/文件 检查.新增.复制.移动.删除,2 文件读写,记录文本日志/ ...

  4. [poj]1050 To the Max dp

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  5. JS 识别生日、性别、年龄

    <script> function IdCard(UUserCard,num){ if(num==1){ //获取出生日期 birth=UUserCard.substring(6, 10) ...

  6. linux mysql 简单记录

    mysql 1.linux下启动mysql的命令:mysqladmin start/ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令: ...

  7. 洛谷P1556 幸福的路

    P1556 幸福的路 题目描述 每天,John都要为了农场里N(1≤N≤10)头牛的健康和幸福四处奔波. 每头牛的位置可以描述为一个二维坐标,John从坐标原点(0,0)出发.为了使路径更有趣,Joh ...

  8. MySQL学习基础之一 — 数据库查询

    廖大神的练手神器:在线SQL: https://www.liaoxuefeng.com/wiki/1177760294764384/1179611432985088 运行MySQL等实际的数据库软件, ...

  9. MCP|BFY|Proteome Analysis of Human Neutrophil Granulocytes From Patients With Monogenic Disease Using Data-independent Acquisition(单基因疾病患者中性粒细胞的DIA蛋白质组分析)

    文献名:Proteome Analysis of Human Neutrophil Granulocytes From Patients With Monogenic Disease Using Da ...

  10. MySql提示:The server quit without updating PID file(…)失败之解决办法(来源网络仅供参考)

    1.可能是/usr/local/mysql/data/rekfan.pid文件没有写的权限 解决方法 :给予权限,执行 “chown -R mysql:mysql /var/data” “chmod ...