【题目链接】http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20332

【PS】算法入门经典Treap的例题,树上讲得非常仔细,在P234。再大致说一下这一个题,很经典。

【题意】给定无向图,有三种操作,①删除第i条边②查询节点x所在的连通分量节点中第K大的权值③改变节点x的权值。现在问你最终的查询结果的平均值

【解题方法】先读入所有的操作,删除所有边得到最终的图,然后反过来操作,在适合的时候加上边,在适合的时候改变点的权值,然后用名次树来维护每个连通分量,加边的时候就用并查集判一下就可以了,然后再将两棵树合并,这里用了启发式合并。

【复杂度】O(n*logn*logn)

【代码君】

//
//Created by just_sort 2016/10/14
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 22222;
const int maxm = 66666;
const int maxq = 555555;
struct node{
    node *ch[2];
    int r,v,s;
    node(int v) : v(v) { ch[0] = ch[1] = NULL; r = rand(); s = 1;}
    bool operator < (const node &rhs) const{
        return r < rhs.r;
    }
    int cmp (int x) const {
        if(x == v) return -1;
        return x < v ? 0 : 1;
    }
    void maintain(){
        s = 1;
        if(ch[0] != NULL) s += ch[0]->s;
        if(ch[1] != NULL) s += ch[1]->s;
    }
};
void rotate(node* &o, int d){
    node *k = o->ch[d^1]; o->ch[d^1] = k->ch[d]; k->ch[d] = o;
    o->maintain(); k->maintain(); o = k;
}
void insert(node* &o,int x){
    if(o == NULL) o = new node(x);
    else{
        int d = (x < o->v ? 0 : 1);
        insert(o->ch[d], x);
        if(o->ch[d]->r > o->r) rotate(o, d^1);
    }
    o->maintain();
}
void remove(node* &o,int x){
    int d = o->cmp(x);
    if(d == -1){
        node *u = o;
        if(o->ch[0] != NULL && o->ch[1] != NULL)
        {
            int d2 = (o->ch[0]->r > o->ch[1]->r ? 1 : 0);
            rotate(o, d2); remove(o->ch[d2], x);
        }
        else
        {
            if(o->ch[0] == NULL) o = o->ch[1];
            else o = o->ch[0];
            delete u;
        }
    }
    else{
        remove(o->ch[d], x);
    }
    if(o != NULL) o->maintain();
}
int Kth(node *o, int k)
{
    if(o == NULL || k <= 0 || k > o->s) return 0;
    int s = (o->ch[1] == NULL ? 0 : o->ch[1]->s);
    if(k == s + 1) return o->v;
    else if(k <= s) return Kth(o->ch[1], k);
    else return Kth(o->ch[0], k-s-1);
}
struct Q{
    char type;
    int x, p;
}q[maxq];
int n,m;
int from[maxm],to[maxm],removed[maxm],weight[maxm];
int pa[maxn];
int Find(int x)
{
    if(x == pa[x]) return x;
    return pa[x] = Find(pa[x]);
}
node *root[maxn]; //Treap
void mergeto(node* &src, node* &dest){
    if(src->ch[0] != NULL) mergeto(src->ch[0],dest);
    if(src->ch[1] != NULL) mergeto(src->ch[1],dest);
    insert(dest,src->v);
    delete src;
    src = NULL;
}
void removetree(node* &x){
    if(x->ch[0] != NULL) removetree(x->ch[0]);
    if(x->ch[1] != NULL) removetree(x->ch[1]);
    delete x;
    x = NULL;
}
void addedge(int x)
{
    int u = Find(from[x]), v = Find(to[x]);
    if(u != v){
        if(root[u]->s < root[v]->s) {pa[u] = v; mergeto(root[u],root[v]);}
        else {pa[v] = u; mergeto(root[v],root[u]);}
    }
}
int cnt;
LL tot;
void query(int x,int k){
    cnt++;
    tot += Kth(root[Find(x)], k);
}
void change_weight(int x,int v)
{
    int u = Find(x);
    remove(root[u], weight[x]);
    insert(root[u], v);
    weight[x] = v;
}

void work()
{
    int ks = 1;
    while(scanf("%d%d", &n,&m)!=EOF)
    {
        if(n == 0 && m == 0) break;
        for(int i = 1; i <= n; i++) scanf("%d", &weight[i]);
        for(int i = 1; i <= m; i++) scanf("%d%d", &from[i], &to[i]);
        memset(removed, 0, sizeof(removed));
        int c = 0;
        while(1)
        {
            char op[10];
            int x, p = 0, v = 0;
            scanf("%s", op);
            if(op[0] == 'E') break;
            scanf("%d", &x);
            if(op[0] == 'D') removed[x] = 1;
            if(op[0] == 'Q') scanf("%d", &p);
            if(op[0] == 'C') {
                scanf("%d", &v);
                p = weight[x];
                weight[x] = v;
            }
            q[c++] = (Q){op[0], x, p};
        }
        //删除之后最终的图
        for(int i = 1; i <= n; i++){
            pa[i] = i;
            if(root[i] != NULL) removetree(root[i]);
            root[i] = new node(weight[i]);
        }
        for(int i = 1; i <= m; i++) if(!removed[i]) addedge(i);
        //反向操作
        tot = cnt = 0;
        for(int i = c - 1; i >= 0; i--){
            if(q[i].type == 'D') addedge(q[i].x);
            if(q[i].type == 'Q') query(q[i].x, q[i].p);
            if(q[i].type == 'C') change_weight(q[i].x, q[i].p);
        }
        printf("Case %d: %.6f\n",ks++,tot/(double)cnt);
    }
}
int main()
{
    work();
    return 0;
}