HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)
Description
1) Deletes an edge from the graph.
The format is [D X], where X is an integer from 1 to M, indicating the ID of the edge that you should delete. It is guaranteed that no edge will be deleted more than once.
2) Queries the weight of the vertex with K-th maximum value among all vertexes currently connected with vertex X (including X itself).
The format is [Q X K], where X is an integer from 1 to N, indicating the id of the vertex, and you may assume that K will always fit into a 32-bit signed integer. In case K is illegal, the value for that query will be considered as undefined, and you should return 0 as the answer to that query.
3) Changes the weight of a vertex.
The format is [C X V], where X is an integer from 1 to N, and V is an integer within the range [-106, 106].
The operations end with one single character, E, which indicates that the current case has ended.
For simplicity, you only need to output one real number - the average answer of all queries.
Input
There will be a blank line between two successive cases. A case with N = 0, M = 0 indicates the end of the input file and this case should not be processed by your program.
Output
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXC = ;
const int MAXN = ;
const int MAXM = ; int key[MAXN], weight[MAXN], child[MAXN][], size[MAXN];
int stk[MAXN], top, poi_cnt;//not use point inline int newNode(int k) {
int x = (top ? stk[top--] : ++poi_cnt);
key[x] = k;
size[x] = ;
weight[x] = rand();
child[x][] = child[x][] = ;
return x;
} inline void update(int &x) {
size[x] = size[child[x][]] + size[child[x][]] + ;//size[0]=0
} inline void rotate(int &x, int t) {
int y = child[x][t];
child[x][t] = child[y][t ^ ];
child[y][t ^ ] = x;
update(x); update(y);
x = y;
} void insert(int &x, int k) {
if (x == ) x = newNode(k);
else {
int t = (key[x] < k);
insert(child[x][t], k);
if (weight[child[x][t]] < weight[x]) rotate(x, t);
}
update(x);
} void remove(int &x, int k) {
if(key[x] == k) {
if(child[x][] && child[x][]) {
int t = weight[child[x][]] < weight[child[x][]];
rotate(x, t); remove(child[x][t ^ ], k);
}
else {
stk[++top] = x;
x = child[x][] + child[x][];
}
}
else remove(child[x][key[x] < k], k);
if(x > ) update(x);
} struct Command {
char type;
int x, p;
} commands[MAXC]; int n, m, value[MAXN], from[MAXM], to[MAXM], removed[MAXM]; int fa[MAXN];
int getfather(int x) {
if(fa[x] == x) return x;
else return fa[x] = getfather(fa[x]);
} int root[MAXN]; void mergeto(int &x, int &y) {
if(child[x][]) mergeto(child[x][], y);
if(child[x][]) mergeto(child[x][], y);
insert(y, key[x]);
stk[++top] = x;
} inline void addEdge(int x) {
int u = getfather(from[x]), v = getfather(to[x]);
if(u != v) {
if(size[root[u]] > size[root[v]]) swap(u, v);
fa[u] = v; mergeto(root[u], root[v]);
}
} int kth(int &x, int k) {
if(x == || k <= || k > size[x]) return ;
int s = ;
if(child[x][]) s = size[child[x][]];
if(k == s + ) return key[x];
if(k <= s) return kth(child[x][], k);
return kth(child[x][], k - s - );
} int query_cnt;
long long query_tot; void query(int x, int k) {
++query_cnt;
query_tot += kth(root[getfather(x)], k);
} inline void change_value(int x, int v) {
int u = getfather(x);
remove(root[u], value[x]);
insert(root[u], value[x] = v);
} int main() {
int kase = ;
size[] = ;
while(scanf("%d%d", &n, &m) != EOF && n) {
for(int i = ; i <= n; ++i) scanf("%d", &value[i]);
for(int i = ; i <= m; ++i) scanf("%d%d", &from[i], &to[i]);
memset(removed, , sizeof(removed)); int c = ;
while(true) {
char type;
int x, p = , v = ;
scanf(" %c", &type);
if(type == 'E') break;
scanf("%d", &x);
if(type == 'D') removed[x] = ;
if(type == 'Q') scanf("%d", &p);
if(type == 'C') {
scanf("%d", &v);
p = value[x];
value[x] = v;
}
commands[c++] = (Command) {type, x, p};
} top = poi_cnt = ;
for(int i = ; i <= n; ++i) {
fa[i] = i;
root[i] = newNode(value[i]);
}
for(int i = ; i <= m; ++i) if(!removed[i]) addEdge(i); query_tot = query_cnt = ;
for(int i = c - ; i >= ; --i) {
if(commands[i].type == 'D') addEdge(commands[i].x);
if(commands[i].type == 'Q') query(commands[i].x, commands[i].p);
if(commands[i].type == 'C') change_value(commands[i].x, commands[i].p);
}
printf("Case %d: %.6f\n", ++kase, query_tot/(double)query_cnt);
}
return ;
}
HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)的更多相关文章
- HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest) - from lanshui_Yang
感慨一下,区域赛的题目果然很费脑啊!!不过确实是一道不可多得的好题目!! 题目大意:给你一棵有n个节点的树,让你移动树中一条边的位置,即将这条边连接到任意两个顶点(边的大小不变),要求使得到的新树的直 ...
- HDU 3726 Graph and Queries (离线处理+splay tree)
Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3726 Graph and Queries treap树
题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring ...
- HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )
Sum of divisors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3726 Graph and Queries 平衡树+前向星+并查集+离线操作+逆向思维 数据结构大综合题
Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)
Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...
- HDU 3685 Rotational Painting(多边形质心+凸包)(2010 Asia Hangzhou Regional Contest)
Problem Description Josh Lyman is a gifted painter. One of his great works is a glass painting. He c ...
- HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)
Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...
随机推荐
- BZOJ1023: [SHOI2008]cactus仙人掌图(仙人掌dp)
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3467 Solved: 1438[Submit][Status][Discuss] Descripti ...
- JS this总结
JS中一切皆对象,this关键字出现在对象定义时的成员(属性和方法)里,因此this指向的是一个JS对象,这个JS对象具体是哪一个的确定是在运行时确定的. 非严格模式: 1.作为对象成员:对象调用对象 ...
- 使用Letsencrypt做SSL certificate
为什么要使用Letsencrypt做SSL certificate? 最简单直接的原因是免费.但是免费存在是否靠谱的问题,尤其是对安全要求比较高的网站,需要考虑使用letsencrypt的安全性是否符 ...
- PHP通过curl向其它服务器发请求并返回数据
在很多时候,我们都需要请求第三方的服务器来获取一些数据,比如token,比如百度的主动推送,那么我们的php如何实现向第三方服务器发请求呢?我们可以通过curl来实现 首先定义请求的url,然后创建h ...
- php 批量去除项目文件bom头
<?php if (isset($_GET['dir'])) { //设置文件目录 $basedir = $_GET['dir']; } else { $basedir = '.'; } $au ...
- IDELPHI是一个MIS系统初学者的乐园空间
DELPHI的长处之一是MIS系统,此空间介绍了MIS中种种问题,及使用DELPHI XE做的办法. 为什么选择DELPHI? DELPHI是开发效率很高的一个工具,但也许很多人都喜欢选择微软.NET ...
- win10安装kali组双系统
一.镜像下载: 根据需求下载自己需要的版本 从官网下载kali 2018.2 的安装包:https://www.kali.org/downloads/ 二.烧录: 这里推荐用 win32 disk i ...
- Jenkins中Publish Over SSH插件使用
Publish Over SSH插件安装 进入插件管理安装插件,我这里已经安装过了所以在installed里面,没安装过去available里面搜索. 系统设置中配置Publish Over SSH ...
- 如何把C盘里的文件默认位置更改到D盘指定目录?
如何把C盘里的文件默认位置更改到D盘指定目录? 1.打开运行,输入 %HOMEPATH% 2.以”桌面”文件转移到D盘目录为例(其他文档类比进行操作) 3.鼠标右键”桌面”----选择属性-----定 ...
- [Real World Haskell翻译]第22章 扩展示例:Web客户端编程
第22章 扩展示例:Web客户端编程 至此,您已经看到了如何与数据库交互,解析一些数据,以及处理错误.现在让我们更进了一步,引入Web客户端库的组合. 在本章,我们将开发一个真正的应用程序:一个播客下 ...