初识lct——洛谷p3203 bzoj2002 [HNOI2010]BOUNCE 弹飞绵羊

11 篇文章 0 订阅

题目大意:

现在有n个装置,每个装置会弹到一个地方(编号一定在它之后),也可能直接弹了出去,动态修改会弹到的地方,和动态查询从一个装置出发多少次会被弹出去。
1 <= n <= 200000, 1 <= m <= 100000

题解:

lct裸题不解释。
我就是来放模板的。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i, x, y) for(int i = x; i <= y; i ++)
#define min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;

const int Maxn = 200050;

int n, b[Maxn], fa[Maxn], pf[Maxn], t[Maxn][2], st[Maxn];
struct node {
    int siz, rev;
}a[Maxn];

void chan(int x) {if(x) a[x].rev ^= 1, swap(t[x][0], t[x][1]);}
void down(int x) {if(a[x].rev) chan(t[x][0]), chan(t[x][1]), a[x].rev = 0;}
void update(int x) {if(x) a[x].siz  = a[t[x][0]].siz + a[t[x][1]].siz + 1;}
void xc(int x) {
    for(; x; x = fa[x]) st[++ st[0]] = x;
    for(; st[0]; st[0] --) down(st[st[0]]);
}
int lr(int x) {return t[fa[x]][1] == x;}
void rotate(int x) {
    int y = fa[x], k = lr(x);
    t[y][k] = t[x][!k]; if(t[x][!k]) fa[t[x][!k]] = y;
    fa[x] = fa[y]; if(fa[y]) t[fa[y]][lr(y)] = x;    
    t[x][!k] = y; fa[y] = x; pf[x] = pf[y];
    update(y); update(x);
}
void splay(int x, int y) {
    xc(x);
    while(fa[x] != y) {
        if(fa[fa[x]] != y)
            if(lr(x) == lr(fa[x])) rotate(fa[x]); else rotate(x);
        rotate(x);
    }
}
void access(int x) {
    for(int y = 0; x; update(x), y = x, x = pf[x])
        splay(x, 0), fa[t[x][1]] = 0, pf[t[x][1]] = x,
        t[x][1] = y, fa[y] = x, pf[y] = 0;
}
void makeroot(int x) {
    access(x); splay(x, 0); chan(x);
}
void link(int x, int y) {
    makeroot(x); pf[x] = y;
}
void cut(int x, int y) {
    makeroot(x); access(y);
    splay(y, 0);
    t[y][0] = fa[x] = pf[x] = 0;
    update(y);
}
int main() {
    scanf("%d", &n);
    fo(i, 1, n) scanf("%d", &b[i]), link(i, min(n + 1, i + b[i]));
    int Q; for(scanf("%d", &Q); Q; Q --) {
        int z, x; scanf("%d %d", &z, &x); x ++;
        if(z == 1) {
            makeroot(n + 1); access(x); splay(x, 0);
            printf("%d\n", a[x].siz - 1);
        } else {
            cut(x, min(x + b[x], n + 1));
            scanf("%d", &b[x]);
            link(x, min(x + b[x], n + 1));
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值