Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
30743 22fhq 【S】T3 C++ 运行出错 0 1985 MS 336 KB 2060 2024-07-30 13:07:30

Tests(0/12):


#include<bits/stdc++.h> using namespace std; #define int long long int n,m,u,v,w; int fa[400005],lz[400005],ch[400005][2],dp[400005][2],d[400005][2],vl[400005],op,k; bool nr(int x){ return x==ch[fa[x]][0]||x==ch[fa[x]][1]; } void pu(int p){ if(vl[p]==0){ dp[p][0]=dp[ch[p][0]][0]+dp[ch[p][1]][0]+d[p][0]+1; dp[p][1]=dp[ch[p][0]][1]+dp[ch[p][1]][1]+d[p][1]; } if(vl[p]==1){ dp[p][0]=0; dp[p][1]=dp[ch[p][0]][0]+dp[ch[p][1]][0]+d[p][0]+1; } return; } void tr(int x){ swap(ch[x][0],ch[x][1]); lz[x]^=1; return; } void pd(int x){ if(!lz[x])return; if(ch[x][0])tr(ch[x][0]); if(ch[x][1])tr(ch[x][1]); lz[x]=0; return; } bool get(int x){ return x==ch[fa[x]][1]; } void rot(int x){ int y=fa[x],z=fa[y],c=get(x); if(nr(y))ch[z][get(y)]=x; fa[x]=z; if(ch[x][!c])fa[ch[x][!c]]=y; ch[y][c]=ch[x][!c]; fa[y]=x; ch[x][!c]=y; pu(y),pu(x); return; } void upd(int x){ if(nr(x))upd(fa[x]); pd(x); return; } void splay(int x){ for(int f=fa[x];nr(x);rot(x),f=fa[x]) if(nr(f))rot(get(f)==get(x)?f:x); pu(x); return; } void acs(int x){ for(int p=0;x;p=x,x=fa[p]){ splay(x); d[x][1]+=dp[ch[x][1]][1]-dp[p][1]; d[x][0]+=dp[ch[x][1]][0]-dp[p][0]; ch[x][1]=p,pu(x); // cout<<d[x][1]<<" "<<d[x][0]<<" "<<x<<endl; } return; } void mkr(int x){ acs(x),splay(x),tr(x); } int fr(int x){ acs(x),splay(x),pd(x); while(ch[x][0])x=ch[x][0],pd(x); return splay(x),x; } void link(int x,int y){ mkr(x); if(fr(y)!=x)fa[x]=y; return; } signed main(){ cin>>n>>m; for(int i=1;i<n;i++){ cin>>u>>v>>vl[n+i]; link(u,n+i),link(n+i,v); } while(m--){ cin>>op>>k; if(op==1)splay(k+n),vl[n+k]=(vl[n+k]==1?0:1),pu(n+k); else { mkr(k); cout<<dp[k][0]+dp[k][1]<<endl; } } return 0; }


测评信息: