loj 2011

对于第 $i$ 天的询问
前 $i - c - 1$ 天都会影响答案
主席树维护

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>

using namespace std;

#define int long long

#define LL long long

#define gc getchar()
inline int read() {int x = 0; char c = gc; while(c < '0' || c > '9') c = gc;
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc; return x;}
inline LL read_LL() {LL x = 0; char c = gc; while(c < '0' || c > '9') c = gc;
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc; return x;}
#undef gc

const int N = 2e5 + 10;

int Root[N], Lson[N * 25], Rson[N * 25], W[N * 25];
struct Node {int v, nxt;} G[N];
int head[N], cnt;
int n, q, Rt;

int fa[N], deep[N], size[N], topp[N], son[N], tree[N], treejs;

inline void Link(int u, int v) {G[++ cnt].v = v; G[cnt].nxt = head[u]; head[u] = cnt;}

void Dfs_1(int u, int f_, int depth) {
    fa[u] = f_, deep[u] = depth, size[u] = 1;
    for(int i = head[u]; ~ i; i = G[i].nxt) {
        int v = G[i].v;
        if(v == f_) continue;
        Dfs_1(v, u, depth + 1);
        size[u] += size[v];
        if(size[v] > size[son[u]]) son[u] = v;
    }
}

void Dfs_2(int u, int tp) {
    topp[u] = tp, tree[u] = ++ treejs;
    if(!son[u]) return ;
    Dfs_2(son[u], tp);
    for(int i = head[u]; ~ i; i = G[i].nxt) {
        int v = G[i].v;
        if(v != fa[u] && v != son[u]) Dfs_2(v, v);
    }
}

inline int Lca(int x, int y) {
    int tpx = topp[x], tpy = topp[y];
    while(tpx != tpy) {
        if(deep[tpx] < deep[tpy]) swap(x, y), swap(tpx, tpy);
        x = fa[tpx], tpx = topp[x];
    }
    return deep[x] < deep[y] ? x : y;
}

int Hjt;

void Fill(int x, int y) {
    Lson[x] = Lson[y], Rson[x] = Rson[y], W[x] = W[y];
}

#define lson jd << 1
#define rson jd << 1 | 1

void Sec_G(int &rt, int l, int r, int x) {
    Fill(++ Hjt, rt);
    rt = Hjt;
    W[rt] ++;
    if(l == r) return ;
    int mid = (l + r) >> 1;
    if(x <= mid) Sec_G(Lson[rt], l, mid, x);
    else Sec_G(Rson[rt], mid + 1, r, x);
}

int Ans;

void Sec_A(int rt, int l, int r, int x, int y) {
    if(!rt) return ;
    if(x <= l && r <= y) {
        Ans += W[rt];
        return ;
    }
    int mid = (l + r) >> 1;
    if(x <= mid) Sec_A(Lson[rt], l, mid, x, y);
    if(y > mid ) Sec_A(Rson[rt], mid + 1, r, x, y);
}

inline int Sec_A_imp(int rt, int x, int y) {
    if(rt <= 0) return 0;
    int ret = 0, tpx = topp[x], tpy = topp[y];
    while(tpx != tpy) {
        if(deep[tpx] < deep[tpy]) swap(x, y), swap(tpx, tpy);
        Ans = 0;
        Sec_A(Root[rt], 1, n, tree[tpx], tree[x]);
        ret += Ans;
        x = fa[tpx], tpx = topp[x];
    }
    if(deep[x] < deep[y]) swap(x, y);
    Ans = 0;
    Sec_A(Root[rt], 1, n, tree[y], tree[x]);
    ret += Ans;
    return ret;
}

main() {
    n = read();
    for(int i = 1; i <= n; i ++) head[i] = -1;
    for(int i = 1; i <= n; i ++) {
        int v = read();
        if(!v) Rt = i;
        else Link(v, i);
    }
    Dfs_1(Rt, 0, 1);
    Dfs_2(Rt, Rt);
    q = read();
    for(int i = 1; i <= q; i ++) {
        int opt = read();
        Root[i] = Root[i - 1];
        if(opt == 1) {
            int x = read(), y = read(), C = read();
            int a = deep[x] + deep[y] - 2 * deep[Lca(x, y)] + 1;
            int b = Sec_A_imp(i - C - 1, x, y);
            cout << a << " " << b << "\n";
        } else {
            int x = read();
            Sec_G(Root[i], 1, n, tree[x]);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shandongs1/p/9578890.html