名次树 - 模板

UvaLive 5031

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxnode = 20005;
const int maxw  = 60005;
const int maxc = 500005;

struct node
{
    node *ch[2];
    int r, v, s; //优先级, 值, 当前节点为子树根的子树节点数(contain this)
    node(int v) : v(v) { ch[0] = ch[1] = NULL; r = rand(); s = 1; }

    bool operator< (const node &x) const { return r < x.r; }

    int cmp(int x) const
    {
        if (v == x) return -1;
        return x < v ? 0 : 1;
    }

    void maintain() //维护s操作
    {
        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) //插入值为x的节点
{
    if (o == NULL) o = new node(x);
    else
    {
        int d = (x < o->v ? 0 : 1); //可能会有相同的节点, 不能用cmd
        insert(o->ch[d], x);
        if (o->ch[d] > o) rotate(o, d ^ 1);
    }
    o->maintain();
}

void remove(node *&o, int x) //删除值为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] > o->ch[1] ? 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();
}

struct Cmd
{
    char type;
    int x, p;
} cmd[maxc];

int n, m;
int w[maxnode], from[maxw], to[maxw], removeflag[maxw];

int pa[maxnode];
int find(int x) { return pa[x] != x ? pa[x] = find(pa[x]) : x; }

node *root[maxnode];

int kth(node *o, int k) //求第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);
}
int rank(node *o, int x)//k是第几大 !
{
    if (o == NULL || x <= 0 || x > o->s)
        return 0;
    int tmp = o->ch[0]->s;
    if (o->v == x)
    {
        return tmp + 1;
    }
    else if (o->v > x)
    {
        return rank(o->ch[0], x);
    }
    else
    {
        return tmp + 1 + rank(o->ch[1], x);
    }
}
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) //删除以x为根的子树
{
    if (x->ch[0] != NULL) removetree(x->ch[0]);
    if (x->ch[1] != NULL) removetree(x->ch[1]);
    delete x;
    x = NULL;
}

void add(int x) //添加编号为x的边 u->v
{
    int u = find(from[x]), v = find(to[x]);
    if (u != v)
    {   //节点个数少的数, 合并到节点多的子树. 启发式合并
        if (root[u]->s > root[v]->s) { pa[v] = u; mergeto(root[v], root[u]); }
        else { pa[u] = v; mergeto(root[u], root[v]); }
    }
}

int cnt;
long long ans;
void query(int x, int k) //查询第k大的元素
{
    cnt++;
    ans += kth(root[find(x)], k);
}

void change(int x, int v) //改变编号为x的节点的值
{
    int u = find(x);
    remove(root[u], w[x]); //将原来的节点删除
    insert(root[u], v); //插入新的节点
    w[x] = v; //相应修改节点的值
}

int main()
{
// freopen("input.txt", "r", stdin);
    int num = 1;
    while (scanf("%d %d", &n, &m) != EOF)
    {
        if (n == 0 && m == 0) break;

        for (int i = 1; i <= n; ++i) scanf("%d", &w[i]);
        for (int i = 1; i <= m; ++i) scanf("%d %d", &from[i], &to[i]);
        memset(removeflag, 0, sizeof(removeflag));

        int cur = 0;
        char type[2];
        int x, p, v;
        while (true)
        {
            p = 0, v = 0;
            getchar();
            scanf("%s", type);
            if (type[0] == 'E') break;
            scanf("%d", &x);
            if (type[0] == 'D') removeflag[x] = 1;
            else if (type[0] == 'Q') scanf("%d", &p);
            else if (type[0] == 'C')
            {
                scanf("%d", &v);
                p = w[x];
                w[x] = v;
            }
            cmd[cur++] = (Cmd) {type[0], x, p};
        }

        for (int i = 1; i <= n; ++i)
        {
            pa[i] = i;
            if (root[i] != NULL) removetree(root[i]);
            root[i] = new node(w[i]);
        }

        for (int i = 1; i <= m; ++i)
            if (!removeflag[i]) add(i);

        cnt = ans = 0;
        for (int i = cur - 1; i >= 0; --i)
        {
            if (cmd[i].type == 'D') add(cmd[i].x);
            if (cmd[i].type == 'Q') query(cmd[i].x, cmd[i].p);
            if (cmd[i].type == 'C') change(cmd[i].x, cmd[i].p);
        }

        printf("Case %d: %.6lf\n", num++, ans / (1.0 * cnt));
    }

    return 0;
}

你可能感兴趣的:(名次树)