Description

You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer value assigned to it at the beginning. You're also given a sequence of operations and you need to process them as requested. Here's a list of the possible operations that you might encounter:
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 are multiple test cases in the input file. Each case starts with two integers N and M (1 <= N <= 2 * 104, 0 <= M <= 6 * 104), the number of vertexes in the graph. The next N lines describes the initial weight of each vertex (-106 <= weight[i] <= 106). The next part of each test case describes the edges in the graph at the beginning. Vertexes are numbered from 1 to N. The last part of each test case describes the operations to be performed on the graph. It is guaranteed that the number of query operations [Q X K] in each case will be in the range [1, 2 * 105], and there will be no more than 2 * 105 operations that change the values of the vertexes [C X V].

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

For each test case, output one real number � the average answer of all queries, in the format as indicated in the sample output. Please note that the result is rounded to six decimal places.
 
题目大意:给一个n个点m条边的无向图,有三种询问,分别为删边、询问某子集内第k大权、修改某点权值,问数次询问后,第二种询问的值的平均数
思路:用treap树维护一个强联通分量。离线处理所有询问,先删掉图中将会被删掉的边,从后往前询问,修改权值的询问也要稍作处理。
PS:因为打错一个字母RE了半天……
 
 #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)的更多相关文章

  1. HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest) - from lanshui_Yang

    感慨一下,区域赛的题目果然很费脑啊!!不过确实是一道不可多得的好题目!! 题目大意:给你一棵有n个节点的树,让你移动树中一条边的位置,即将这条边连接到任意两个顶点(边的大小不变),要求使得到的新树的直 ...

  2. HDU 3726 Graph and Queries (离线处理+splay tree)

    Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. HDU 3726 Graph and Queries treap树

    题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring ...

  4. 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) ...

  5. HDU 3726 Graph and Queries 平衡树+前向星+并查集+离线操作+逆向思维 数据结构大综合题

    Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. chromium之scoped_ptr

    看看怎么使用 // Scopers help you manage ownership of a pointer, helping you easily manage the // a pointer ...

  2. vue 引入 mint-ui 简单使用

    一 npm 方式 1,安装依赖 (已有项目) 如果想简单体验:基于vue-cli /* npm install vue -g npm install vue-cli   -g   // -g 是否全局 ...

  3. windows下MySQL免安装版配置教程mysql-8.0.12-winx64.zip版本

    引用1:https://blog.csdn.net/weixin_42831477/article/details/81589325 引用2:https://blog.csdn.net/qq_3193 ...

  4. FMX有一套自己的消息处理机制。类似这样:

    unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Var ...

  5. C语言实验报告(五) 两个正整数的最大公约数

    编程实现求两个正整数的最大公约数,要求计算最大公约数用函数fun(int a,int b)实现. #include<stdio.h>void main(){  int n,a,b;  in ...

  6. 使用MATLAB设计FIR滤波器

    1.      采用fir1函数设计,fir1函数可以设计低通.带通.高通.带阻等多种类型的具有严格线性相位特性的FIR滤波器.语法形式: b = fir1(n, wn) b = fir1(n, wn ...

  7. APP如何发布到Google play 商店

    APP如何发布到Google play 商店?以及有哪些需要注意的点 2015-05-13 10:07 19773人阅读 评论(1) 收藏 举报  分类: iPhone游戏开发(330)  链接:ht ...

  8. C++ STL中的 Set的用法

    https://blog.csdn.net/yas12345678/article/details/52601454 -----源头此处 1.关于set的概念   set   是STL中的集合. 集合 ...

  9. 初识主席树_Prefix XOR

    主席树刚接触觉得超强,根本看不懂,看了几位dalao的代码后终于理解了主席树. 先看一道例题:传送门 题目大意: 假设我们预处理出了每个数满足条件的最右边界. 先考虑暴力做法,直接对x~y区间暴枚,求 ...

  10. 青岛Uber优步司机奖励政策(12月28日到1月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...