Problem

有添加操作和询问操作,每次询问输出第i大的数。

Solution

Treap标准模板题

Notice

非旋转的真心难打。

Code

非旋转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 = 30000;
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');}
int point = 0, T[N + 5], root;
struct node
{
    int Size[N + 5], Val[N + 5], Level[N + 5], Son[2][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);
    }
    void Build(int l, int r)
    {
        int last, u, s[N + 5], top = 0;
        rep(i, l, r)
        {
            int u = Newnode(T[i]);
            last = 0;
            while (top && Level[s[top]] > Level[u])
            {
                up(s[top]);
                last = s[top];
                s[top--] = 0;
            }
            if (top) Son[1][s[top]] = u;
            Son[0][u] = last;
            s[++top] = u;
        }
        while (top) up(s[top--]);
        root = s[1];
    }
    int Find_rank(int v)
    {
        int x, y, t;
    	Split(root, v - 1, x, y);
    	t = Size[x];
    	root = Merge(x, y);
    	return t + 1;
    }
    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 v)
    {
    	int t = Newnode(v), x, y;
        Split(root, v, x, y);
    	root = Merge(Merge(x, t), y);
    }
}Treap;
int sqz()
{
    int n = read(), m = read(), last = 0;
    rep(i, 1, n) T[i] = read();
    rep(i, 1, m)
    {
        int tt = read();
        while (last < tt)
        {
            last++;
            Treap.Insert(T[last]);
        }
        printf("%d\n", Treap.Val[Treap.Find_num(root, i)]);
    }
}

旋转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 = 30000;
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');}
int point = 0, T[N + 5], root;
struct node
{
    int Val[N + 5], Level[N + 5], Son[2][N + 5], Size[N + 5];
    inline void up(int u)
    {
        Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + 1;
    }
    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;
    }
    inline void Newnode(int &u, int v)
    {
        u = ++point;
        Val[u] = v, Level[u] = rand();
        Son[0][u] = Son[1][u] = 0, Size[u] = 1;
    }

    void Insert(int &u, int v)
    {
        if (!u)
        {
            Newnode(u, v);
            return;
        }
        Size[u]++;
        if (v < Val[u])
        {
            Insert(Son[0][u], v);
            if (Level[Son[0][u]] < Level[u]) Rturn(u);
        }
        else
        {
            Insert(Son[1][u], v);
            if (Level[Son[1][u]] < Level[u]) Lturn(u);
        }
    }
    int Find_num(int u, int t)
    {
        if (!u) return 0;
        if (t <= Size[Son[0][u]]) return Find_num(Son[0][u], t);
        else if (t <= Size[Son[0][u]] + 1) return u;
        else return Find_num(Son[1][u], t - Size[Son[0][u]] - 1);
    }
}Treap;
int sqz()
{
    int n = read(), m = read(), last = 0;
    rep(i, 1, n) T[i] = read();
    rep(i, 1, m)
    {
        int tt = read();
        while (last < tt)
        {
            last++;
            Treap.Insert(root, T[last]);
        }
        printf("%d\n", Treap.Val[Treap.Find_num(root, i)]);
    }
}
posted on 2017-10-09 23:29  WizardCowboy  阅读(120)  评论(0编辑  收藏  举报