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 ...
随机推荐
- linux系统基础之---文件系统(基于centos7.4 1708)
- (七)json序列化
在spring boot项目中已经包含有json序列化的框架,具体在包com.fasterxml.jackson.annotation中,建议看看详细源码. 但在项目应用上还是会有一些坑会出现的,举个 ...
- Python对文件目录的操作
python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()返回指定目录下的所有文件和目录 ...
- 基于Python的飞机大战游戏
前几天决定学Python,上网找了教程看了两天,和C比起来面向对象的特性真的都很便捷,有了类开发各种敌机,子弹什么的都很方便. 在此要感谢开发pygame模块的开发人员,真的很好用(逃 效果图↓ 主函 ...
- Django学习之模拟架构页面跳转
背景知识,需要有一定量的HTTP基础知识 在客户端游览器通过URL向服务端发送请求时,经历了两次过程.一次是URL向服务端发起请求,一次是服务端向客户端回发响应. 由图可知,客户端一共传递两个信息,一 ...
- aes并发加密Cipher not initialized 异常
javax.crypto.Cipher 每次都要实例化,大量的实例化导致 Cipher 实例化失败. 解决办法:将已经实例化的Cipher对象,放在hashmap中,每次实例化的时候从MAP 获取,不 ...
- 分布式专题(二)——Zookeeper的ZAB协议介绍
- postgresql 日期类型处理实践
---- 日期+1 select date '2018-01-14' + integer '1'; 结果: 2018-01-15 ---- 日期+1 后 转 20180101 日期字符串 select ...
- Android APP架构设计——MVP的使用示例
0. 前言 为了更好地进行移动端架构设计,我们最常用的就是MVC.MVP和MVVM,作为三个最耳熟能详的三大架构,应用可谓非常广泛.对于这三种架构设计以及优缺点已经在Android APP架构设计-- ...
- BZOJ3293_分金币_KEY
题目传送门 设x[i]表示i+1向i传的糖果数,x[n]表示1向n传的糖果数,a'=(a[1]+...a[N])/N a[1]+x[1]−x[n]=a' a[2]+x[2]−x[1]=a' a[3]+ ...