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 ...
随机推荐
- swift3.0 保存图片到本地,申请权限
1.info中写上 <key>NSCameraUsageDescription</key> <string>需要您的同意才能读取媒体资料库</string&g ...
- webuploader的一个页面多个上传按钮实例
借鉴一位大佬的demo 附上他的github地址https://github.com/lishuqi 我把他的cxuploader.js改了不需要预览 直接上传图片后拿到回传地址给img标签显示图 ...
- lamp 安装 apache
lamp安装 httpd-2.2.4.tar.gz :http://download.csdn.net/detail/wulvla020311/8046141 先检查一下装的东西都在不在:rpm -q ...
- 农民工自学java到找到工作的前前后后
我是一名地地道道的农民工,生活在经济落后的农村,有一个哥哥和一个弟弟,父母都是地道的农民,日出而作,日落而息,我从小到大学习一直很好,从小学到高一都,成绩在全级一直名列前茅,这样我也顺利了考上省的重点 ...
- php源码建博客2--实现单入口MVC结构
主要: MVC目录结构 数据库工具类制作 创建公共模型类和公共控制器类 --------------文件结构:-------------------------------------- blog├─ ...
- header()函数用处
header() 函数向客户端发送原始的 HTTP 报头. 认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 PHP 4 以及更高的版本中,您可以使用输出缓存来解决此 ...
- FireDAC的SQLite初探
// uses FireDAC.VCLUI.Wait 之后, 可不用添加 TFDGUIxWaitCursor TFDConnection // 数据连接 TFDQuery ...
- 集合之HashMap、Hashtable
HashMap 基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtabl ...
- Hive(9)-自定义函数
一. 自定义函数分类 当Hive提供的内置函数无法满足你的业务处理需要时,此时就可以考虑使用用户自定义函数. 根据用户自定义函数类别分为以下三种: 1. UDF(User-Defined-Functi ...
- 如何防止index.html首页被篡改
近期发现公司网站首页文件经常被篡改为indax.php或indax.html,导致网站的功能无法正常使用,百度搜索关键词,在显示结果中点击公司网站,打开后跳转到别的网站上去了,尤其我们在百度做的推广, ...