题意:给出一个无向图,每一个节点都有一个权值。有三种操作,1.删掉某一条边。2.将一个点的权值改变。3.询问某一点的连通分量中第k大的点。

最后输出每次询问的值之和/询问次数。(无效的询问的值为0).

思路:Splay,这题需要离线处理,我们把操作全部存起来,然后逆序的来做,那删除边就等于加边,改变点的权值也是逆向的来做。我这里用的是邻接表来存的点的权值的变化。

对于每一个联通分量我们建立一棵伸展树,左孩子的点的权值都比父亲的小。

对于加边操作,如果两个节点在一个联通分量中,就不需要加边。如果不在的话,我们需要把这两棵伸展树合并,为了减少复杂度,我把节点少的合并在节点多的伸展树中,这里是逐点合并。

对于修改点权值的操作,我们先把该点转到root,然后再删掉,最后再插入点。

对于询问,就是简单的树的遍历的事情了。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 20008
#define M 60008
#define lc (tr[d].c[0])
#define rc (tr[d].c[1])struct Tr{int c[2], cnt, v, fa;
}tr[N];struct E{int v, u;
}e[M];struct Q{char op;int x, y;
}que[N*20];struct V{int v, ne;
}up[N*10];int head[N], size, f[N], val[N], root, use[M] = {0};void init() {memset(head, -1, sizeof(head));memset(f, -1, sizeof(f));size = 0;root = 0;tr[0].cnt = tr[0].c[0] = tr[0].c[1] = 0;
}void Push(int d) {tr[d].cnt = tr[lc].cnt+tr[rc].cnt+1;
}void Rotate(int x, int k) {if (tr[x].fa == 0) return;int fa = tr[x].fa, w;tr[fa].c[!k] = tr[x].c[k];if (tr[x].c[k] != 0) tr[tr[x].c[k]].fa = fa;tr[x].fa = tr[fa].fa, tr[x].c[k] = fa;if (tr[fa].fa != 0) {w = tr[tr[fa].fa].c[1]==fa;tr[tr[fa].fa].c[w] = x;}tr[fa].fa = x;Push(fa);Push(x);
}void Splay(int x, int goal) {//将x节点转到goal的儿子上if (x == 0) return;while (tr[x].fa != goal) {int y = tr[x].fa;bool w = x==tr[y].c[1];if (tr[y].fa != goal && w == (y==tr[tr[y].fa].c[1]))Rotate(y, !w);Rotate(x, !w);}if (goal == 0) root = x;Push(x);
}void newtr(int &r, int fa, int id, int k) {r = id;tr[r].fa = fa, tr[r].v = k;tr[r].c[0] = tr[r].c[1] = 0;tr[r].cnt = 1;
}void Insert(int r, int key) {int d = root;if (d == 0) {newtr(d, 0, r, key);return;}while (tr[d].c[key>tr[d].v])d = tr[d].c[key>tr[d].v];newtr(tr[d].c[key>tr[d].v], d, r, key);Splay(r, 0);
}int Get_Min(int d) {while (lc) d = lc;return d;
}void Delete() {int d = root;if (lc == 0 || rc == 0) {root = lc+rc;tr[root].fa = 0;return;}int tm = Get_Min(rc);Splay(tm, root);tr[tm].c[0] = lc;tr[lc].fa = tm;tr[tm].fa = 0;root = tm;Push(tm);
}void erase(int d) {if (d == 0) return;erase(lc), erase(rc);Insert(d, up[head[d]].v);
}int find(int x) {if (f[x] == -1) return x;return f[x] = find(f[x]);
}void bing(int u, int v) {int t1 = find(u), t2 = find(v);if (t1 == t2) return;f[t1] = t2;Splay(t1, 0);Splay(t2, 0);if (tr[t1].cnt < tr[t2].cnt) swap(t1, t2);root = t1;erase(t2);
}int query(int d, int k) {int s = tr[lc].cnt+1;if (s == k) return tr[d].v;if (s > k) return query(lc, k);else return query(rc, k-s);
}int main() {int n, m, i, j, u, v, ca = 1, k;while (scanf("%d%d", &n, &m), n||m) {init();for (i = 1;i <= n;i++) {scanf("%d", &v);up[size].v = v, up[size].ne = head[i];head[i] = size++;}for (i = 1;i <= m;i++) scanf("%d%d", &e[i].u, &e[i].v);int qc = 0;while (scanf(" %c", &que[qc].op), que[qc].op != 'E') {if (que[qc].op == 'D') {scanf("%d", &que[qc].x);use[que[qc].x] = ca;}scanf("%d%d", &que[qc].x, &que[qc].y);if (que[qc].op == 'C') {up[size].v = que[qc].y, up[size].ne = head[que[qc].x];head[que[qc].x] = size++;}qc++;}for (i = 1;i <= n;i++) {newtr(root, 0, i, up[head[i]].v);}for (i = 1;i <= m;i++) {if (use[i] == ca) continue;bing(e[i].u, e[i].v);}double ans = 0;int cnt = 0;for (i = qc-1;i >= 0;i--) {if (que[i].op == 'D') {bing(e[que[i].x].u, e[que[i].x].v);}else if (que[i].op == 'C') {u = que[i].x, v = que[i].y;Splay(u, 0);Delete();head[u] = up[head[u]].ne;Insert(u, up[head[u]].v);}else {u = que[i].x, k = que[i].y;Splay(u, 0);cnt++;if (tr[root].cnt >= k && k > 0) {ans += query(u, tr[root].cnt-k+1);}}}printf("Case %d: %.6lf\n", ca++, (cnt==0)?0:(ans/cnt));}
}

【TOJ 3755】 Graph and Queries【Splay】相关推荐

  1. 【Elasticsearch】 Full text queries query_string 等 字符串查询

    1.概述 转载:https://zhuanlan.zhihu.com/p/143957734 并且修改 起因是我查询的时候遇到一个这样的查询 GET /xxx-000001/_search {&quo ...

  2. 【论文翻译】Graph Convolution over Pruned Dependency Trees Improves Relation Extraction

    [论文翻译]Graph Convolution over Pruned Dependency Trees Improves Relation Extraction 摘要 1 引言 2 模型 2.1 依 ...

  3. Module-based visualization of large-scale graph network data【论文阅读】

    基于模块化的大规模图网络数据可视化(2016) 关键词 网络可视化(Network visualization) 模块分组(Module grouping) 社区检测(Community detect ...

  4. 2021年 第12届 蓝桥杯 Java B组 省赛真题详解及小结【第1场省赛 2021.04.18】

    蓝桥杯 Java B组 省赛决赛 真题详解及小结汇总[题目下载.2013年(第4届)~2020年(第11届)] CSDN 蓝桥杯 专栏 2013年 第04届 蓝桥杯 Java B组 省赛真题详解及小结 ...

  5. 性能监控与调优篇之【3. JVM 监控及诊断工具-GUI 篇】

    文章目录 3. JVM 监控及诊断工具-GUI 篇 3.1. 工具概述 3.2. JConsole 3.3. Visual VM 3.4. Eclipse MAT 3.5. JProfiler 3.6 ...

  6. 【视频直播场景下P2P对等网技术②】任意两节点的联通性能评估

    [视频直播场景下P2P对等网技术②]任意两节点的联通性能评估 如上文([视频直播场景下P2P对等网技术①])所述,当一个新的节点 F F F加入现有的网络 G G G的时候,若 m = ∣ V G ∣ ...

  7. 【KylinTOP:国内高端性能测试工具学习】

    [KylinTOP:国内高端性能测试工具学习] Brief Introduction Platform Structure Performance Testing Feature Installati ...

  8. springsocial/oauth2---绑定和解绑处理【QQ绑定异常,微信解绑302/ERR_TOO_MANY_REDIRECTS】

    文章目录 1 事先声明 2 ConnectController简介 3 获取当前用户所有第三方账号的绑定状态 3.1 当前用户所有第三方账号绑定状态绑定状态的处理视图 3.2 测试 4 将当前用户与第 ...

  9. 成长轨迹44 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2799、2976、2975、2742】...

    一次ac的就不说啥了.. 2799:浮点数格式 View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include ...

最新文章

  1. 为什么123 and 456结果是456而123 or 456结果是123?
  2. MySQL之索引分类
  3. webpack 单独打包指定JS文件
  4. docker mysql 容器无故停止的原因
  5. WCF中的Stream操作
  6. 在 VS 类库项目中 Add Service References 和 Add Web References 的区别
  7. ffmpeg-0.8 开源编码解码库从linux下移植到windows vs2005
  8. HttpUtility.UrlEncode 方法 (String) 对 URL 字符串进行编码 NET Framework 4.6 and 4.5
  9. httpclient base64 文件上传_代码级别的上传下载神器
  10. Spring Boot返回的数据格式是XML 而不是JSON之原因探求的和解决
  11. 云南民大java期中考试_中南民族大学Java语言程序设计期末试卷A卷
  12. 上海富勒wms_冷库推荐|上海1800托低温库出租
  13. [HCNA] IP地址和子网划分
  14. 欧美大脑计划存在的问题和忽视的一个重要元素,互联网大脑计划系列三
  15. springboot集成socket.io通过jwt-token身份认证鉴权
  16. 鼠标悬停放大图片特效
  17. iOS知识分享 — iOS 13上的暗模式
  18. ZCMU-1411:喜闻乐见的a+b(大整数)
  19. CSCD+北大核心《计算机工程与应用》期刊投稿经验,2022年10月最新
  20. Windebug的一个缺点

热门文章

  1. matlab编写转台程序,基于Matlab三轴惯导测试转台结构分析.doc
  2. 全景感知、智能融合|视图计算平台全新发布
  3. Google鲜为人知的强大功能!(陆续更新)
  4. 【Hive任务优化】—— Map、Reduce数量调整
  5. Oracle 大数据量查询优化
  6. 【VOLTE】SPS 半持续调度
  7. 流体力学发展史(转)
  8. linux搭建pptpd服务器,最简单的Linux系统上的pptpd服务器安装
  9. c#FileStream文件读写可能会出现乱码
  10. python统计英文文章中单词的个数无文件_求Python统计英文文件内单词个数的思路...