HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)

时间:2023-03-09 18:25:23
HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)

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 ;
}