Problem

给你一张图,点的权值,边和几个操作:
D x: 删除第x条边
Q x y: 询问包含x的联通块中权值第y大的权值
C x y: 将x这个点的权值改为y

Solution

一看就要离线处理,把所有操作都倒过来
然后删除操作变为加边操作

Notice

记得: 是改完以后再把点一个一个加入Treap中!!

Code

非旋转Treap

#pragma GCC optimize(2)
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 3000000;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
struct Node
{
    int num1, num2, type;
}Q[N + 5];
int point = 0, fa[N + 5], Root[N + 5], T[N + 5], From[N + 5], To[N + 5], Flag[N + 5];
struct node
{
    int Val[N + 5], Level[N + 5], Size[N + 5], Son[2][N + 5], Num[N + 5];
    inline void up(int u)
    {
        Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + 1;
    }
    int Newnode(int v)
    {
        int u = ++point;
        Val[u] = v, Level[u] = rand();
        Son[0][u] = Son[1][u] = 0, Size[u] = 1;
        return u;
    }
    int Merge(int X, int Y)
    {
        if (X * Y == 0) return X + Y;
        if (Level[X] < Level[Y])
        {
            Son[1][X] = Merge(Son[1][X], Y);
            up(X); return X;
        }
        else
        {
            Son[0][Y] = Merge(X, Son[0][Y]);
            up(Y); return Y;
        }
    }
    void Split(int u, int t, int &x, int &y)
    {
        if (!u)
		{
			x = y = 0;
			return;
		}
        if (Val[u] <= t) x = u, Split(Son[1][u], t, Son[1][u], y);
        else y = u, Split(Son[0][u], t, x, Son[0][u]);
        up(u);
    }
    int Find_num(int u, int v)
    {
        if (!u) return 0;
        if (v <= Size[Son[0][u]]) return Find_num(Son[0][u], v);
        else if (v <= Size[Son[0][u]] + 1) return u;
        else return Find_num(Son[1][u], v - Size[Son[0][u]] - 1);
    }
    void Insert(int &u, int v)
    {
    	int t = Newnode(v), x, y;
        Split(u, v, x, y);
    	u = Merge(Merge(x, t), y);
    }
    void Delete(int &u, int v)
    {
        int x, y, z;
        Split(u, v, x, z), Split(x, v - 1, x, y);
        u = Merge(Merge(x, Merge(Son[0][y], Son[1][y])), z);
    }
}Treap;
int Find(int x)
{
    if (fa[x] != x) fa[x] = Find(fa[x]);
    return fa[x];
}
void Union(int u, int v)
{
    if (Treap.Size[Root[u]] < Treap.Size[Root[v]]) swap(u, v);
    while (Treap.Size[Root[v]])
    {
        int t = Treap.Find_num(Root[v], 1);
        Treap.Insert(Root[u], Treap.Val[t]);
        Treap.Delete(Root[v], Treap.Val[t]);
    }
    fa[v] = u;
    Root[v] = 0;
}
int sqz()
{
    int n, m, cas = 0;
    while (~scanf("%d %d", &n, &m) && (n || m))
    {
        point = 0;
        rep(i, 1, n) T[i] = read(), fa[i] = i, Root[i] = 0;
        rep(i, 1, m) From[i] = read(), To[i] = read(), Flag[i] = 0;
        int q = 0; char op[5];
        while (scanf("%s", op) && op[0] != 'E')
        {
            q++;
            if (op[0] == 'D')
                Q[q].num1 = read(), Flag[Q[q].num1] = 1, Q[q].type = 0;
            else
            {
                Q[q].num1 = read(), Q[q].num2 = read();
                if (op[0] == 'C') swap(T[Q[q].num1], Q[q].num2), Q[q].type = 1;
                else Q[q].type = 2;
            }
        }
        rep(i, 1, n) Treap.Insert(Root[i], T[i]);
        rep(i, 1, m)
            if (!Flag[i])
            {
                int u = Find(From[i]), v = Find(To[i]);
                if (u != v) Union(u, v);
            }
        ll ans = 0; int tot = 0;
        per(i, q, 1)
        {
            if (Q[i].type == 0)
            {
                int u = Find(From[Q[i].num1]), v = Find(To[Q[i].num1]);
                if (u != v) Union(u, v);
            }
            else if (Q[i].type == 1)
            {
                int u = Find(Q[i].num1);
                Treap.Delete(Root[u], T[Q[i].num1]);
                Treap.Insert(Root[u], Q[i].num2);
                T[Q[i].num1] = Q[i].num2;
            }
            else
            {
                int u = Find(Q[i].num1);
                int t = Treap.Find_num(Root[u], Treap.Size[Root[u]] - Q[i].num2 + 1);
                if (t != -INF) ans += Treap.Val[t];
                tot++;
            }
        }
        printf("Case %d: %.6f\n", ++cas, ans * 1.0 / tot);
    }
    return 0;
}

旋转Treap

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 3000000;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
struct Node
{
    int num1, num2, type;
}Q[N + 5];
int point = 0, fa[N + 5], Root[N + 5], T[N + 5], From[N + 5], To[N + 5], Flag[N + 5];
struct node
{
    int Val[N + 5], Level[N + 5], Size[N + 5], Son[2][N + 5], Num[N + 5];
    inline void up(int u)
    {
        Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + Num[u];
    }
    inline void Newnode(int &u, int v)
    {
        u = ++point;
        Level[u] = rand(), Val[u] = v;
        Size[u] = Num[u] = 1, Son[0][u] = Son[1][u] = 0;
    }
    inline void Lturn(int &x)
    {
        int y = Son[1][x]; Son[1][x] = Son[0][y], Son[0][y] = x;
        up(x); up(y); x = y;
    }
    inline void Rturn(int &x)
    {
        int y = Son[0][x]; Son[0][x] = Son[1][y], Son[1][y] = x;
        up(x); up(y); x = y;
    }

    void Insert(int &u, int t)
    {
        if (u == 0)
        {
            Newnode(u, t);
            return;
        }
        Size[u]++;
        if (t == Val[u]) Num[u]++;
        else if (t > Val[u])
        {
            Insert(Son[0][u], t);
            if (Level[Son[0][u]] < Level[u]) Rturn(u);
        }
        else if (t < Val[u])
        {
            Insert(Son[1][u], t);
            if (Level[Son[1][u]] < Level[u]) Lturn(u);
        }
    }
    void Delete(int &u, int t)
    {
        if (!u) return;
        if (Val[u] == t)
        {
            if (Num[u] > 1)
            {
                Num[u]--, Size[u]--;
                return;
            }
            if (Son[0][u] * Son[1][u] == 0) u = Son[0][u] + Son[1][u];
            else if (Level[Son[0][u]] < Level[Son[1][u]]) Rturn(u), Delete(u, t);
            else Lturn(u), Delete(u, t);
        }
        else if (t > Val[u]) Size[u]--, Delete(Son[0][u], t);
        else Size[u]--, Delete(Son[1][u], t);
    }

    int Find_num(int u, int t)
    {
        if (!u) return -INF;
        if (t <= Size[Son[0][u]]) return Find_num(Son[0][u], t);
        else if (t <= Size[Son[0][u]] + Num[u]) return Val[u];
        else return Find_num(Son[1][u], t - Size[Son[0][u]] - Num[u]);
    }
}Treap;
int Find(int x)
{
    if (fa[x] != x) fa[x] = Find(fa[x]);
    return fa[x];
}
void Union(int u, int v)
{
    if (Treap.Size[Root[u]] < Treap.Size[Root[v]]) swap(u, v);
    while (Treap.Size[Root[v]])
    {
        int t = Treap.Find_num(Root[v], 1);
        Treap.Insert(Root[u], t);
        Treap.Delete(Root[v], t);
    }
    fa[v] = u;
    Root[v] = 0;
}
int sqz()
{
    int n, m, cas = 0;
    while (~scanf("%d %d", &n, &m) && (n || m))
    {
        point = 0;
        rep(i, 1, n) T[i] = read(), fa[i] = i, Root[i] = 0;
        rep(i, 1, m) From[i] = read(), To[i] = read(), Flag[i] = 0;
        int q = 0; char op[5];
        while (scanf("%s", op) && op[0] != 'E')
        {
            q++;
            if (op[0] == 'D')
                Q[q].num1 = read(), Flag[Q[q].num1] = 1, Q[q].type = 0;
            else
            {
                Q[q].num1 = read(), Q[q].num2 = read();
                if (op[0] == 'C') swap(T[Q[q].num1], Q[q].num2), Q[q].type = 1;
                else Q[q].type = 2;
            }
        }
        rep(i, 1, n) Treap.Insert(Root[i], T[i]);
        rep(i, 1, m)
            if (!Flag[i])
            {
                int u = Find(From[i]), v = Find(To[i]);
                if (u != v) Union(u, v);
            }
        ll ans = 0; int tot = 0;
        per(i, q, 1)
        {
            if (Q[i].type == 0)
            {
                int u = Find(From[Q[i].num1]), v = Find(To[Q[i].num1]);
                if (u != v) Union(u, v);
            }
            else if (Q[i].type == 1)
            {
                int u = Find(Q[i].num1);
                Treap.Delete(Root[u], T[Q[i].num1]);
                Treap.Insert(Root[u], Q[i].num2);
                T[Q[i].num1] = Q[i].num2;
            }
            else
            {
                int u = Find(Q[i].num1);
                int t = Treap.Find_num(Root[u], Q[i].num2);
                if (t != -INF) ans += t;
                tot++;
            }
        }
        printf("Case %d: %.6f\n", ++cas, ans * 1.0 / tot);
    }
    return 0;
}
posted on 2017-10-10 00:37  WizardCowboy  阅读(137)  评论(0编辑  收藏  举报