Hdu 1890 Kuangbin代码

    翻别人的题解都是把要翻的最小点splay到根然后旋转左子树然后删去根节点,Kuangbin模板里的代码不知道在干嘛。

研究了半天,旋转是一样的,但是不删除。比如旋转[l, r]区间,就让l-1变根,r+1变根的右儿子,那么[l, r]都在r+1的左子树上了,然后一个个交换左右儿子来翻转。

node[i]表示第i个元素,要得到l-1要调用get_kth(l),同理要得到r+1要调用get_kth(r+2)。

#include <queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 50010
#define M 3645
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const double PI = acos(-1);
const int mod = 10007;
#define fi first
#define se second
#define L o<<1
#define R o<<1|1
#define tl tree[o].l
#define tr tree[o].r
#define tw tree[o].w
#define to tree[o].ok
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

const int MAXN = 100010;
struct Node;
Node * null;
struct Node {
    Node * ch[2], *fa;///sons, father
    int sz, rev, v;///size of two subtrees
                ///rev means whether the node has been reversed
    Node() {
        ch[0] = ch[1] = fa = null;
        rev = 0;
        v = INF;
    }
    inline void push_up() {///When rotated
        if(this == null) return;
        sz = ch[0]->sz + ch[1]->sz + 1;
    }
    inline void setc(Node *p, int d) {///set sons
        ch[d] = p;
        p->fa = this;
    }
    inline bool d() {///is right son
        return fa->ch[1] == this;
    }
    void clear() {///delete node
        sz = 1;
        ch[0] = ch[1] = fa = null;
        rev = 0;
    }
    void Update_Rev() {///swap subtrees
        if(this == null) return;
        swap(ch[0], ch[1]);
        rev ^= 1;
    }
    inline void push_down() {///swap subtrees lazily
        if(this == null) return;
        if(rev) {
            ch[0]->Update_Rev();
            ch[1]->Update_Rev();
            rev = 0;
        }
    }
    inline bool isroot() {
        return fa == null || this != fa->ch[0] && this != fa->ch[1];
    }
};

inline void rotate(Node *x)///rotate x
{
    Node *f = x->fa, *ff = x->fa->fa;///f father ff father of father
    f->push_down();
    x->push_down();
    int c = x->d(), cc = f->d();
    f->setc(x->ch[!c], c);
    x->setc(f, !c);
    if(ff->ch[cc] == f) ff->setc(x, cc);
    else x->fa = ff;
    f->push_up();
}
inline void splay(Node * &root, Node * x, Node * goal)
///rotate x until x is a son of goal
///When goal equals to zero, x will be the root of tree.
{
    while(x->fa != goal) {///until x is a son of goal
        if(x->fa->fa == goal) rotate(x);
        else {
            x->fa->fa->push_down();
            x->fa->push_down();
            x->push_down();
            bool f = x->fa->d();
            x->d() == f?rotate(x->fa):rotate(x);
            rotate(x);
        }
    }
    x->push_up();
    if(goal == null) root = x;
}

Node* get_kth(Node * r, int k)
{///get k-th node
    Node * x  = r;
    x->push_down();
    while(x->ch[0]->sz + 1 != k) {cout << "k " << x->v << endl;
        if(k < x->ch[0]->sz + 1) x = x->ch[0];
        else {
            k -= x->ch[0]->sz + 1;
            x = x->ch[1];
        }
        x->push_down();
    }
    return x;
}

Node * get_next(Node * p)
{///get next node
    p->push_down();
    p = p->ch[1];
    p->push_down();
    while(p->ch[0] != null) {
        p = p->ch[0];
        p->push_down();
    }
    return p;
}
Node pool[MAXN], *tail;
Node *node[MAXN];///tree
Node *root;

void build(Node * &x, int l, int r, Node * fa)
{///build a balanced binary tree
    if(l > r) return;
    int mid = (l + r) >> 1;
    x = tail++;
    x->clear();
    x->fa = fa;
    x->v = mid;
    node[mid] = x;
    build(x->ch[0], l, mid - 1, x);
    build(x->ch[1], mid + 1, r, x);
    x->push_up();
}
void init(int n)
{
    tail = pool;
    null = tail++;
    null->fa = null->ch[0] = null->ch[1] = null;
    null->sz = 0; null->rev = 0;
    Node *p = tail++;
    p->clear();
    root = p;
    p = tail++;
    p->clear();
    root->setc(p, 1);
    build(p->ch[0], 1, n, p);
    root->ch[1]->push_up();
    root->push_up();
}

int a[MAXN], b[MAXN];
bool cmp(int i, int j)
{
    if(a[i] == a[j]) return i < j;
    return a[i] < a[j];
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data.txt", "r", stdin);
    #endif // ONLINE_JUDGE

    int n;
    while(scanf("%d", &n) && n) {
        rep(i, 1, n) {
            scanf("%d", &a[i]);
            b[i] = i;
        }
        init(n);
        sort(b + 1, b + 1 + n, cmp);
//rep(i, 1, n) cout << b[i] << ' ' ; cout << endl;
        rep(i, 1, n) {//cout << node[b[i]]->v << endl;
            splay(root, node[b[i]], null);
            int sz = root->ch[0]->sz;
            printf("%d%c", sz, i == n?'\n':' ');
        //cout << get_kth(root, i)->v <<' ' << i<< endl;
            splay(root, get_kth(root, i), null);//cout << i << endl;
        //cout << get_kth(root, sz + 2)->v << ' ' << sz+2<<endl;
            splay(root, get_kth(root, sz + 2), root);//cout << sz + 2 << endl;
            root->ch[1]->ch[0]->Update_Rev();
        }
        puts("");
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值