QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#397162#4387. Static Query on TreeDiuAC ✓62ms26908kbC++142.6kb2024-04-23 18:46:432024-04-23 18:46:43

Judging History

你现在查看的是最新测评结果

  • [2024-04-23 18:46:43]
  • 评测
  • 测评结果:AC
  • 用时:62ms
  • 内存:26908kb
  • [2024-04-23 18:46:43]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+10;
int n,m;
int vis[N],imp[N];
int dep[N],num[N],id;
struct Ready{
	struct tree{
		int v,nxt;
	}e[N];
	int hd[N],tot;
	void add(int u,int v){e[++tot]={v,hd[u]},hd[u]=tot;}
	int fa[N],top[N],son[N],sz[N];
	void clr(){for(int i=1;i<=n;i++)hd[i]=0;tot=id=0;}
	void dfs(int u){
		sz[u]=1,son[u]=0;
		for(int i=hd[u];i;i=e[i].nxt){
			int v=e[i].v;
			if(v==fa[u])continue;
			fa[v]=u,dep[v]=dep[u]+1;
			dfs(v);
			sz[u]+=sz[v];
			if(sz[son[u]]<sz[v])son[u]=v;
		}
	}
	void dfs2(int u,int h){
		num[u]=++id,top[u]=h;
		if(son[u])dfs2(son[u],h);
		for(int i=hd[u];i;i=e[i].nxt){
			int v=e[i].v;
			if(v==fa[u]||v==son[u])continue;
			dfs2(v,v);
		}
	}
	int lca(int u,int v){
		while(top[u]!=top[v]){
			if(num[u]<num[v])swap(u,v);
			u=fa[top[u]];
		}
		return num[u]<num[v]?u:v;
	}
}tr;
int x[N];
bool cmp(int a,int b){return num[a]<num[b];}
int st[N],tp;
int Fa[N],isa[N],isb[N],isc[N];
int stk[N];
int cnt;
int nd[N],Cnt;
void add(int u,int v,int k){
	if(vis[v]!=k)vis[v]=k,nd[++Cnt]=v;
	if(vis[u]!=k)vis[u]=k,nd[++Cnt]=u;
	if(isa[v]==k)isa[u]=k;
	if(isb[v]==k)isb[u]=k;
	Fa[v]=u;
	stk[++cnt]=v;
}
int T;
signed main(){
	scanf("%d",&T);
	for(;T--;){
		scanf("%d%d",&n,&m);
		tr.clr();
		for(int i=1;i<=n;i++)imp[i]=vis[i]=isa[i]=isb[i]=isc[i]=Fa[i]=stk[i]=st[i]=dep[i]=num[i]=0;
		for(int fa,i=2;i<=n;i++){
			scanf("%d",&fa);
			tr.add(fa,i);
		}
		dep[1]=1;
		tr.dfs(1),tr.dfs2(1,1);
		for(int l=1,k1,k2,k3;l<=m;l++){
			scanf("%d%d%d",&k1,&k2,&k3);
			int k=k1+k2+k3;
			for(int i=1,t;i<=k1;i++)scanf("%d",&t),x[i]=t,imp[t]=isa[t]=l;
			for(int i=k1+1,t;i<=k1+k2;i++)scanf("%d",&t),x[i]=t,imp[t]=isb[t]=l;
			for(int i=k1+k2+1,t;i<=k1+k2+k3;i++)scanf("%d",&t),x[i]=t,imp[t]=isc[t]=l;
			sort(x+1,x+k+1,cmp);
			k=unique(x+1,x+k+1)-x-1;
			st[tp=1]=1,cnt=0,Cnt=0;
			for(int i=1;i<=k;i++){
				if(x[i]==1)continue;
				int y=tr.lca(st[tp],x[i]);
				if(y^st[tp]){
					while(tp>1&&num[y]<num[st[tp-1]])add(st[tp-1],st[tp],l),--tp;
					add(y,st[tp],l);
					if(num[y]>num[st[tp-1]])st[tp]=y;
					else --tp;
				}
				st[++tp]=x[i];
			}
			for(int i=tp-1;i>=1;i--)add(st[i],st[i+1],l);
			Fa[1]=0;
			for(;cnt;--cnt){
				int v=stk[cnt],u=Fa[v];
				if(isc[u]==l)isc[v]=l;
			}
//			for(int i=1;i<=Cnt;i++)printf("Node:%d %d %d %d %d\n",x[i],isa[x[i]],isb[x[i]],isc[x[i]],Fa[x[i]]);
			for(int i=1;i<=Cnt;i++)if(isa[nd[i]]==l&&isb[nd[i]]==l&&isc[Fa[nd[i]]]==l)cnt+=dep[nd[i]]-dep[Fa[nd[i]]]-1;
			for(int i=1;i<=Cnt;i++)if(isa[nd[i]]==l&&isb[nd[i]]==l&&isc[nd[i]]==l)cnt++;
			printf("%d\n",cnt);
		}
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 62ms
memory: 26908kb

input:

1
200000 18309
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 ...

output:

102147
62590
87270
88880
7654
61542
62953
85022
55135
54125
70500
64356
25824
88300
42278
15336
18132
28734
90282
42889
28099
31311
96842
19959
34366
60205
78358
91142
56048
74688
86091
51979
94750
11989
89544
86860
56720
29534
52343
90031
79002
90293
94554
48340
65015
9181
15016
19884
49445
14181
6...

result:

ok 18309 numbers