LA 5031 Graph and Queries

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3032

n(n<=2e4)个顶点m(m<=6e4)条边,每个顶点有个权值val_i, 然后有Q(Q<=5e5)次操作.

操作分为三类:

D x : 删除第x条边

Q x k : 查询与节点x关联的所有顶点中第k大

C x V : 将节点x的权值更改为V

输出查询的均值  /sum { Query_val } / Query_num

解题思

离线算法

对于删除,可以通过将所有操作读入后,从后往前处理。把删除边转换成插入边。

对于查询第k大顶点,我们可以使用 treap维护的名次树 kth来实现

对于修改操作,我们先将原来的值删除,然后再插入新值。 因为我们使用离线逆向处理,则修改操作也会逆向。

关于名次树,只是对于 treap上增加了一个 size(其子节点的数量和+1)然后来实现求第K大 or 小.


关于Treap:Treap是一颗拥有键值v和优先级r两种权值的树。对于键值而言,这棵树是排序树;对于优先级而言,这棵树是大根堆。

不难证明,如果每个节点的优先级事先给定且不互相等,这棵树的形态也唯一确定了。

关于Treap的模板:

 

struct Node
{
    Node * ch[2];
    int r;//优先级
    int v;//值
    int 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(v == x) 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;
    }
};
//d=0向左转,d=1向右转
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] > o) 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] > 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();
}

 


Treap可以用来实现名次树:

在每次树中,每一个节点有一个附加size,表示以它为根的子树的总节点数。

Kth(k):找出第K小元素:

 

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


本题代码:

 

 

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

struct Node
{
    Node * ch[2];
    int r;//优先级
    int v;//值
    int 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(v == x) 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;
    }
};
//d=0向左转,d=1向右转
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] > o) 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] > 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();
}

#define Maxn 20005
#define Maxm 60005
#define Maxc 500005

struct Command
{
    char type;
    int x,p;
    Command(){}
    Command(char _type,int _x,int _p)
    {
        type = _type;
        x = _x;
        p = _p;
    }
};
Command commands[Maxc];

//边的起始、终止节点编号
int from[Maxm],to[Maxm];
//已经删除的边号
int removed[Maxm];
//每个节点的值
int weight[Maxn];
//父亲节点
int pa[Maxn];
//名次树的根节点
Node * root[Maxn];
//查询的次数
int query_cnt;
//查询得到的值的和
long long query_tot;

int findset(int x)
{
    if(x == pa[x]) return x;
    pa[x] = findset(pa[x]);
    return pa[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)
{
    if(x->ch[0]!=NULL) removeTree(x->ch[0]);
    if(x->ch[1]!=NULL) removeTree(x->ch[1]);
    delete x;
    x = NULL;
}
void add_edge(int x)
{
    int u = findset(from[x]);
    int v = findset(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 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);
}

void query(int x,int k)
{
    query_cnt++;
    query_tot += kth(root[findset(x)],k);
    //printf()
}
void change_weight(int x,int v)
{
    int u = findset(x);
    remove(root[u],weight[x]);
    insert(root[u],v);
    weight[x] = v;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int n,m;
    int x,p,v;
    char type;
    int cas = 0;
    while(scanf(" %d %d",&n,&m)!=EOF && n!=0)
    {
        cas++;
        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(scanf(" %c",&type)!=EOF && type != 'E')
        {
            scanf(" %d",&x);
            if(type == 'C')
            {
                scanf(" %d",&v);
                p = weight[x];
                weight[x] = v;
            }
            if(type == 'D') removed[x] = 1;
            if(type == 'Q') scanf(" %d",&p);
            Command a(type,x,p);
            commands[c++] = a;
        }
        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]) add_edge(i);
        //反向操作
        query_cnt = query_tot = 0;
        for(int i=c-1;i>=0;i--)
        {
            if(commands[i].type == 'D') add_edge(commands[i].x);
            if(commands[i].type == 'Q') query(commands[i].x,commands[i].p);
            if(commands[i].type == 'C') change_weight(commands[i].x,commands[i].p);
        }
        printf("Case %d: %.6lf\n",cas,query_tot/(double)query_cnt);
    }
    return 0;
}



 

 

posted @ 2013-06-01 20:16  javawebsoa  Views(197)  Comments(0Edit  收藏  举报