其他分享
首页 > 其他分享> > [HNOI2010] 弾飞绵羊

[HNOI2010] 弾飞绵羊

作者:互联网

题目链接:

传送门

题目分析:

题外话:
我即使是死了,钉在棺材里了,也要在墓里,用这腐朽的声带喊出:
根号算法牛逼!!!

显然,这是一道LCT裸题,然而在下并不会LCT于是采用了分块瞎搞
对于每个点维护两个信息:跳出块的步数\(step[i]\)和跳出块的落点\(lo[i]\)
预处理时使用类似模拟的方法。每次只处理还未处理过的点,并且对于每次处理顺带将向后跳到过的点也处理掉,具体见代码。
对于两种操作:

代码:

#include<bits/stdc++.h>
#define N 200010
using namespace std;
inline int read() {
    int cnt = 0, f = 1; char c;
    c = getchar();
    while (!isdigit(c)) {
        if (c == '-') f = -f;
        c = getchar();
    }
    while (isdigit(c)) {
        cnt = cnt * 10 + c - '0';
        c = getchar();
    }
    return cnt * f;
}
int q, pos[N], n, m, L[N], R[N], step[N], lo[N], sta[N], top, a[N], opr, x, k;
bool flag = false;

void pre_work() {
    q = sqrt(n);
    for (register int i = 1; i <= q; i++) {
        L[i] = (i - 1) * q + 1;
        R[i] = i * q;
    }
    if (R[q] < n) {
        q++;
        L[q] = R[q - 1] + 1;
        R[q] = n;
    }
    
    for (register int i = 1; i <= q; i++) 
        for (register int j = L[i]; j <= R[i]; j++) 
            pos[j] = i;
    
    for (register int i = 1; i <= n; i++) {
        if(step[i]) continue;
        flag = false;
        sta[++top] = i; int now = i;
        while(pos[now] == pos[now + a[now]]) {
            if (step[now + a[now]]) {
                flag = 1;
                break;
            } else {
                now += a[now];
                sta[++top] = now;
            }
        }
        int total = top + 1;
        while (top) {
            if (!flag) {
                lo[sta[top]] = now + a[now];
                step[sta[top]] = total - top;
            } else {
                lo[sta[top]] = lo[now + a[now]];
                step[sta[top]] = total - top + step[now + a[now]];
            }
            --top;
        }
    }
}
void change(int q,int k) {
    int p = pos[q];
    a[q] = k;
    for (register int i = L[p]; i <= R[p]; i++) lo[i] = step[i] = 0;
    for (register int i = L[p]; i <= R[p]; i++) {
        if(step[i]) continue;
        flag = false;
        sta[++top] = i; int now = i;
        while(pos[now] == pos[now + a[now]]) {
            if (step[now + a[now]]) {
                flag = 1;
                break;
            } else {
                now += a[now];
                sta[++top] = now;
            }
        }
        int total = top + 1;
        while (top) {
            if (!flag) {
                lo[sta[top]] = now + a[now];
                step[sta[top]] = total - top;
            } else {
                lo[sta[top]] = lo[now + a[now]];
                step[sta[top]] = total - top + step[now + a[now]];
            }
            --top;
        }
    }
}
int query(int i) {
    int v = i;
    int ans= 0;
    while (pos[v]) {
        ans += step[v];
        v = lo[v];
    }
    return ans;
}
int main() {
    n = read();
    for (register int i = 1; i <= n; i++) a[i] = read();
    m = read();
    pre_work();
    for (register int i = 1; i <= m; i++) {
        opr = read(); x = read();
        if (opr == 1) printf("%d\n", query(x + 1));
        if (opr == 2) {
            k = read();
            change(x + 1, k);
        }
    }
    return 0;
}

标签:cnt,int,lo,复杂度,sqrt,HNOI2010,绵羊,getchar
来源: https://www.cnblogs.com/kma093/p/10808431.html