题目链接:https://www.luogu.org/problemnew/show/P2146
变量名真毒瘤 我真的再也不把l,left,r,right弄反了
反向思维更好做一些
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define lson left, mid, rt<<1
#define rson mid + 1, right, rt<<1|1
using namespace std;
const ll maxn = 100000 + 10;
char opt[9];
ll n, m, root, res;
ll fa[maxn], deep[maxn], size[maxn], top[maxn], son[maxn], seg[maxn], rev[maxn], num;
ll tree[maxn<<2], lazy[maxn<<2];
struct edge{
ll to, next;
}e[maxn<<2];
ll head[maxn], cnt;
void add(ll u, ll v)
{
e[++cnt].next = head[u];
e[cnt].to = v;
head[u] = cnt;
}
//-----segment_tree-----
void PushUP(ll rt)
{
tree[rt] = tree[rt<<1] + tree[rt<<1|1];
}
void build(ll left, ll right, ll rt)
{
lazy[rt] = -1;
if(left == right)
{
tree[rt] = 1;
return;
}
ll mid = (left + right)>>1;
build(lson);
build(rson);
PushUP(rt);
}
void PushDOWN(ll left, ll right, ll rt, ll mid)
{
lazy[rt<<1] = lazy[rt];
lazy[rt<<1|1] = lazy[rt];
tree[rt<<1] = lazy[rt]*(mid - left + 1);
tree[rt<<1|1] = lazy[rt]*(right - mid);
lazy[rt] = -1;
}
void update(ll l, ll r, ll add, ll left, ll right, ll rt)
{
if(l <= left && r >= right)
{
tree[rt] = add*(right - left + 1);
lazy[rt] = add;
return;
}
ll mid = (left + right)>>1;
if(lazy[rt] != -1) PushDOWN(left, right, rt, mid);
if(l <= mid) update(l,r,add,lson);
if(r > mid) update(l,r,add,rson);
PushUP(rt);
}
ll query(ll l, ll r, ll left, ll right, ll rt)
{
ll res = 0;
if(l <= left && r >= right)
{
return tree[rt];
}
ll mid = (left + right)>>1;
if(lazy[rt] != -1) PushDOWN(left, right, rt, mid);
if(l <= mid) res += query(l,r,lson);
if(r > mid) res += query(l,r,rson);
return res;
}
//----------------------
void dfs1(ll u, ll f, ll d)
{
ll maxson = -1;
size[u] = 1;
deep[u] = d;
fa[u] = f;
for(ll i = head[u]; i != -1; i = e[i].next)
{
ll v = e[i].to;
if(f != v)
{
dfs1(v, u, d+1);
size[u] += size[v];
if(maxson < size[v]) son[u] = v, maxson = size[v];
}
}
}
void dfs2(ll u, ll t)
{
seg[u] = ++num;
rev[num] = 1;
top[u] = t;
if(!son[u]) return;
dfs2(son[u], t);
for(ll i = head[u]; i != -1; i = e[i].next)
{
ll v = e[i].to;
if(v == son[u] || v == fa[u]) continue;
dfs2(v, v);
}
}
ll qRange(ll x, ll y)
{
ll ans = 0;
while(top[x] != top[y])
{
if(deep[top[x]] < deep[top[y]]) swap(x, y);
res = 0;
res = query(seg[top[x]], seg[x], 1, n, 1);
ans = ans + res;
x = fa[top[x]];
}
if(deep[x] > deep[y]) swap(x, y);
res = 0;
res = query(seg[x], seg[y], 1, n, 1);
ans = ans + res;
return ans;
}
void updRange(ll x, ll y, ll k)
{
while(top[x] != top[y])
{
if(deep[top[x]] < deep[top[y]]) swap(x, y);
update(seg[top[x]], seg[x], k, 1, n, 1);
x = fa[top[x]];
}
if(deep[x] > deep[y]) swap(x, y);
update(seg[x], seg[y], k, 1, n, 1);
}
ll qSon(ll x)
{
res = 0;
res = query(seg[x], seg[x]+size[x]-1, 1, n, 1);
return res;
}
void updSon(ll x, ll y)
{
update(seg[x], seg[x]+size[x]-1, y, 1, n, 1);
}
int main()
{
memset(lazy, -1, sizeof(lazy));
memset(head, -1, sizeof(head));
scanf("%lld",&n); root = 1;
for(ll i = 2; i <= n; i++)
{
ll u;
scanf("%lld",&u);u++;
add(u, i), add(i,u);
}
dfs1(root, 0, 1);
dfs2(root, root);
build(1, n, 1);
//for(ll i = 1; i <= n; i++) cout<<seg[i]<<" ";
scanf("%lld",&m);
for(ll i = 1; i <= m; i++)
{
ll u;
cin>>opt;
if(opt[0] == 'i')
{
scanf("%lld",&u);u++;
printf("%lld
",qRange(u, root));
updRange(u, root, 0);
}
else
{
scanf("%lld",&u);u++;
//cout<<qSon(u)<<"qwq
";
printf("%lld
",size[u]-qSon(u));
updSon(u, 1);
}
}
//for(int i = 1; i <= n; i++) cout<<tree[i]<<" ";
}