BZOJ2002-弹飞绵羊 LCT

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

#define REP(i, a, b) for (int i = (a), _end_ = (b); i < _end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template<typename T> inline bool chkmin(T &a, const T &b){ return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b){ return a < b ? a = b, 1 : 0; }

typedef long long LL;

const int dmax = 300100, oo = 0x3f3f3f3f;

int N, M;

inline int read()
{
    int x = 0, f = 1, c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

struct node
{
    node *f, *ch[2];
    int rev, size;
}c[dmax], *cur, *Null;

#define L ch[0]
#define R ch[1]

node *new_node()
{
    cur->rev = 0;
    cur->size = 1;
    cur->f = cur->L = cur->R = Null;
    return cur++;
}

inline void init()
{
    Null = new node;
    Null->f = Null->L = Null->R = Null;
    Null->rev = Null->size =  0;
    cur = c;
}

struct lct
{
    inline bool is_root(node *t) { return t == Null || t->f->L != t && t->f->R != t; }
    inline void push_up(node *t) { t->size = t->L->size + t->R->size + 1; }
    inline void push_down(node *t)
    {
        if (t->rev)
        {
            if (t->L != Null) t->L->rev ^= 1;
            if (t->R != Null) t->R->rev ^= 1;
            t->rev = 0;
            swap(t->L, t->R);
        }
    }
    inline void rotate(node *x)
    {
        if (is_root(x)) return;
        node *y = x->f;
        int k = x == y->R;
        x->f = y->f;
        y->ch[k] = x->ch[!k];
        if (x->ch[!k] != Null) x->ch[!k]->f = y;
        if (!is_root(y))
        {
            if (y == y->f->L)
                y->f->L = x;
            else y->f->R = x;
        }
        x->ch[!k] = y;
        y->f = x;
        push_up(y);
    }
    void splay(node *x)
    {
        static node *S[dmax];
        int top = 1;
        S[0] = x;
        for (node *Cur = x; !is_root(Cur); Cur = Cur->f)
            S[top++] = Cur->f;
        while (top > 0) push_down(S[--top]);
        while (!is_root(x))
        {
            node *y = x->f;
            if (!is_root(y))
                rotate(x);
            else{
                if (x == y->f->L->L || x == y->f->R->R)
                    rotate(y);
                else rotate(x);
                rotate(x);
            }
        }
        push_up(x);
    }
    node *access(node *x)
    {
        node *y = Null;
        while (x != Null)
        {
            splay(x);
            x->R = y;
            y->f = x;
            push_up(x);
            y = x;
            x = x->f;
        }
        return y;
    }
    inline void change_root(node *x) { access(x)->rev = 1; splay(x); }
    inline void link(node *x, node *y)
    {
        change_root(x);
        x->f = y;
        access(x);
    }
    inline void cut(node *x)
    {
        access(x);
        splay(x);
        x->L = x->L->f = Null;
        push_up(x);
    }
}t;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    init();
    N = read();
    REP(i, 0, N)
        new_node();
    new_node();
    REP(i, 0, N)
        t.link(c + i, c + min(N, i + read()));
    M = read();
    while (M--)
        if (read() == 1)
        {
            int x = read();
            t.change_root(c + N);
            t.access(c + x);
            t.splay(c + x);
            printf("%d\n", (c + x)->L->size);
        } else {
            int x = read(), y = read();
            t.cut(c + x);
            t.link(c + x, c + min(N, x + y));
        }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值