性感树剖在线求调教
查看原帖
性感树剖在线求调教
246331
mystic_qwq楼主2024/7/17 08:30
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,q;
vector<int> G[N];
int dfn[N],sz[N],dfnCnt,son[N],fa[N],dep[N],top[N];
void dfs1(int u){
  sz[u]=1; //dfn[u]=++dfnCnt;
  for(int v:G[u]) if(v^fa[u]){
    fa[v]=u,dep[v]=dep[u]+1,dfs1(v),sz[u]+=sz[v];
    if(sz[son[u]]<sz[v]) v=son[u];
  }
}
void dfs2(int u,int tp){
  top[u]=tp; 
  dfn[u]=++dfnCnt;
  if(!son[u]) return;
  dfs2(son[u],tp);
  for(int v:G[u]) 
  if(v!=son[u]&&v!=fa[u])
    dfs2(v,v);
}
int lzy[N<<2],sum[N<<2];
void pushdown(int h,int len){
  if(~lzy[h]) return;
  lzy[h<<1]=lzy[h];
  lzy[h<<1|1]=lzy[h];
  sum[h<<1]=(len-len/2)*lzy[h];
  sum[h<<1|1]=(len/2)*lzy[h];
  lzy[h]=-1;
}
void pushup(int h){
  sum[h]=sum[h<<1]+sum[h<<1|1];
}
#define mid (l+r>>1)
void update(int h,int l,int r,int x,int y,int z){
  if(x<=l&&r<=y) 
    return sum[h]=z*(r-l+1),void(lzy[h]=z);
  pushdown(h,r-l+1);
  if(x<=mid) update(h*2,l,mid,x,y,z);
  if(y>mid) update(h*2+1,mid+1,r,x,y,z);
  pushup(h);
}
void updRange(int u,int v,int w){
  while(top[u]!=top[v]) {
    if(dep[top[u]]<dep[top[v]]) swap(u,v);
    update(1,1,n,dfn[top[u]],dfn[u],w);
    u=fa[top[u]];
  }
  if(dep[u]>dep[v]) swap(u,v);
  update(1,1,n,dfn[u],dfn[v],w);
}
main(){
  cin.tie(0)->sync_with_stdio(0);
  cin>>n;
  memset(lzy,-1,sizeof lzy);
  for(int i=1,u;i<n;++i){
    cin>>u; ++u;
    if(u) G[u].push_back(i+1);
  }
  for(int i=1;i<=n;++i)
    if(!dfn[i]) dfs1(i),dfs2(i,i);
  cin>>q;
  while(q--){
    int x; char op[20];
    cin>>op>>x;
    ++x;
    int pre=sum[1];
    if(*op=='i') updRange(1,x,1);
    else if(*op=='u') update(1,1,n,dfn[x],dfn[x]+sz[x]-1,0);
    cout<<abs(pre-sum[1])<<'\n';
  }
}
2024/7/17 08:30
加载中...