QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#327048#3307. Query on a Tree 17JohnAlfnovWA 4ms19404kbC++142.5kb2024-02-14 18:25:472024-02-14 18:25:47

Judging History

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

  • [2024-02-14 18:25:47]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:19404kb
  • [2024-02-14 18:25:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
vector<int>g[100005];
int sz[100005],dep[100005];
int fa[21][100005];
void dfs1(int x,int la){
	sz[x]=1;
	for(auto cu:g[x]){
		if(cu==la)continue;
		dep[cu]=x;fa[0][cu]=x;
		dfs1(cu,x);
		sz[x]+=sz[cu];
	}
}
int ffa[100005],bg[100005],en[100005],gb[100005],tot=0;
void dfs2(int x,int la){
	bg[x]=en[x]=++tot;gb[bg[x]]=x;
	if(sz[x]==1)return;
	int ans=0,w=0;
	for(auto cu:g[x]){
		if(cu==la)continue;
		if(ans<sz[cu])ans=sz[cu],w=cu;
	}
	ffa[w]=ffa[x];dfs2(w,x);en[x]=en[w];
	for(auto cu:g[x]){
		if(cu==la||cu==w)continue;
		ffa[cu]=cu;dfs2(cu,x);en[x]=en[cu];
	}
}
long long sm[400005];
int lz[400005];
void pushdown(int l,int r,int o){
	if(!lz[o])return;
	int mid=(l+r)>>1;
	sm[o<<1]+=1ll*(mid-l+1)*lz[o],sm[o<<1|1]+=1ll*(r-mid)*lz[o];
	lz[o<<1]+=lz[o],lz[o<<1|1]+=lz[o];
	lz[o]=0;
}
void add(int l,int r,int o,int ll,int rr){
	if(l>=ll&&r<=rr){
		sm[o]+=r-l+1,++lz[o];
		return;
	}
	pushdown(l,r,o);
	int mid=(l+r)>>1;
	if(mid>=ll)add(l,mid,o<<1,ll,rr);
	if(mid<rr)add(mid+1,r,o<<1|1,ll,rr);
	sm[o]=sm[o<<1]+sm[o<<1|1];
}
int fin(int l,int r,int o,long long k){
	if(l==r)return l;
	pushdown(l,r,o);
	int mid=(l+r)>>1;
	if(k<=sm[o<<1])return fin(l,mid,o<<1,k);
	return fin(mid+1,r,o<<1|1,k-sm[o<<1]);
}
long long query(int l,int r,int o,int ll,int rr){
	if(l>=ll&&r<=rr)return sm[o];
	pushdown(l,r,o);
	int mid=(l+r)>>1;
	long long ans=0;
	if(mid>=ll)ans+=query(l,mid,o<<1,ll,rr);
	if(mid<rr)ans+=query(mid+1,r,o<<1|1,ll,rr);
	return ans;
}
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<n;++i){
		int u,v;
		scanf("%d%d",&u,&v);
		g[u].emplace_back(v);
		g[v].emplace_back(u);
	}
	dfs1(1,0);ffa[1]=1;dfs2(1,0);
	for(int i=1;i<=20;++i)for(int j=1;j<=n;++j)
		fa[i][j]=fa[i-1][fa[i-1][j]];
	long long he=0;
	int q;
	scanf("%d",&q);
	while(q--){
		int op;
		scanf("%d",&op);
		if(op==1){
			int u;
			scanf("%d",&u);
			he+=sz[u];
			add(1,n,1,bg[u],en[u]);
		}else{
			int u,v;
			scanf("%d%d",&u,&v);
			while(ffa[u]!=ffa[v]){
				if(dep[ffa[u]]<dep[ffa[v]])swap(u,v);
				add(1,n,1,bg[ffa[u]],bg[u]);he+=bg[u]-bg[ffa[u]]+1;
				u=fa[0][ffa[u]];
			}
			if(dep[u]<dep[v])swap(u,v);
			add(1,n,1,bg[v],bg[u]);he+=bg[u]-bg[v]+1;
		}
		int x=gb[fin(1,n,1,(he+1)/2)];
		for(int i=19;i>=0;--i)if(fa[i][x]){
			int z=fa[i][x];
			if(query(1,n,1,bg[z],en[z])<=he/2)x=z;
		}
		if(query(1,n,1,bg[x],en[x])<=he/2)x=fa[0][x];
		printf("%d\n",x);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 19400kb

input:

7
1 6
1 7
7 3
3 2
7 5
5 4
4
1 2
1 4
1 6
2 6 7

output:

2
7
7
1

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 18868kb

input:

15
1 2
1 3
2 4
3 7
6 2
3 8
4 9
2 5
5 11
7 13
10 4
11 15
12 5
14 7
11
2 6 11
1 6
2 10 9
1 9
2 2 6
1 4
2 7 6
1 2
2 8 13
1 10
2 8 15

output:

2
2
2
2
2
2
2
2
2
2
2

result:

ok 11 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 19404kb

input:

2
1 2
1
1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 0ms
memory: 19132kb

input:

2
1 2
1
2 1 2

output:

1

result:

ok single line: '1'

Test #5:

score: -100
Wrong Answer
time: 4ms
memory: 19024kb

input:

100
1 2
58 2
2 87
63 87
65 63
65 19
6 87
58 21
87 8
87 74
91 6
95 65
2 61
87 59
3 61
37 87
67 87
23 2
63 9
87 46
40 67
70 2
12 58
46 75
75 36
28 3
12 33
72 87
39 6
75 52
85 70
1 76
26 40
47 28
2 49
41 65
66 28
51 37
15 47
86 51
8 60
97 19
48 58
72 90
33 83
97 54
36 5
23 14
78 52
90 7
99 2
48 82
48 6...

output:

72
3
3
87
87
2
2
2
2
87
87
87
87
87
2
87
87
87
87
87
87
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
87
2
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
...

result:

wrong answer 84th lines differ - expected: '87', found: '2'