답안 #90587

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
90587 2018-12-22T15:37:24 Z mrtsima22 Birthday gift (IZhO18_treearray) C++17
30 / 100
4000 ms 3504 KB
#include <iostream>
#include <vector>
using namespace std;
int n,k,q,t_out[100003],fin,p[100003][22],deep[100003];
vector<int>a[100003];
int ba[100003],t_in[100004],T,x;
bool is_it(int a,int b){
	return (t_in[a]<=t_in[b])&&(t_out[b]<=t_out[a]);
}
void go(int s,int parent,int DEEP){
	t_in[s]=++T;
	p[s][1]=parent;
	deep[s]=DEEP;
	for(int i=0;i<(int)a[s].size();i++){
		if(a[s][i]==parent)continue;
		go(a[s][i],s,DEEP+1);
	}
	t_out[s]=++T;
}
void mshobelize(int Par,int sad){
	for(int j=2;j<=19;j++){
		p[sad][j]=p[p[sad][j-1]][j-1];
	}
	for(int i=0;i<(int)a[sad].size();i++){
		if(a[sad][i]!=Par){
			mshobelize(sad,a[sad][i]);
		}
	}
}
int lca(int a,int b){
	int A=deep[a];
	int B=deep[b];
	if(A>B){
		swap(a,b);
		swap(A,B);
	}
	for(int i=19;i>=0;i--){
		if(deep[p[b][i]]>=deep[a]){
			b=p[b][i];
		}
	}
	if(a==b){
		return a;
	}
    for (int i = 19; i >= 0; i--) {
        if (p[b][i] != p[a][i]) {
            b = p[b][i];       
            a = p[a][i];
        }
    }
    return p[a][1];
}
int u,v,tp;
int main() {ios_base::sync_with_stdio(false);
cin>>n>>k>>q;
for(int i=0;i<n-1;i++){
	cin>>v>>u;
	a[v].push_back(u);
	a[u].push_back(v);
}
for(int i=1;i<=k;i++){
	cin>>ba[i];
}

go(1,0,0);

for(int i=0;i<=19;i++){
	p[1][i]=1;
}

mshobelize(1,1);

/*
for(int i=1;i<=n;i++)
{
	cout<<i<<"----===>";
	for(int j=1;j<=19;j++)
	{
		cout<<p[i][j]<<" ";
	}
	cout<<endl;
}
*/
t_out[0]=999999999;
while(q--){
	cin>>tp;
	if(tp==1)
	{
		cin>>u>>v;
		ba[u]=v;
	}
	else if(tp==2){
	cin>>u>>v>>x;
	fin=0;
		for(int L=u;!fin&&L<=v;L++){
			int c=ba[L];
//			int c1=c;
			for(int R=L;!fin&&R<=v;R++){
//				while(!is_it(c,ba[R]))c=p[c][1];
				c=lca(c,ba[R]);
//				if(c1!=c)
//					cout<<6/0<<endl;
				if(!is_it(x,c))break;
				if(x==c){
					fin=1;
					cout<<L<<" "<<R<<endl;
				}
			}
		}
		if(!fin){
			cout<<"-1 -1"<<endl;
		}
	}
	
}
 
 
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB n=5
2 Correct 4 ms 2812 KB n=100
3 Correct 4 ms 3016 KB n=100
4 Correct 4 ms 3060 KB n=100
5 Correct 4 ms 3060 KB n=100
6 Correct 6 ms 3060 KB n=100
7 Correct 5 ms 3060 KB n=100
8 Correct 4 ms 3060 KB n=100
9 Correct 4 ms 3060 KB n=100
10 Correct 4 ms 3060 KB n=100
11 Correct 4 ms 3060 KB n=100
12 Correct 4 ms 3060 KB n=100
13 Correct 4 ms 3060 KB n=100
14 Correct 4 ms 3060 KB n=100
15 Correct 4 ms 3060 KB n=100
16 Correct 4 ms 3060 KB n=100
17 Correct 4 ms 3060 KB n=100
18 Correct 4 ms 3060 KB n=100
19 Correct 4 ms 3060 KB n=100
20 Correct 4 ms 3060 KB n=100
21 Correct 4 ms 3060 KB n=100
22 Correct 4 ms 3060 KB n=100
23 Correct 12 ms 3060 KB n=100
24 Correct 14 ms 3060 KB n=100
25 Correct 6 ms 3060 KB n=100
26 Correct 3 ms 3060 KB n=12
27 Correct 4 ms 3060 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB n=5
2 Correct 4 ms 2812 KB n=100
3 Correct 4 ms 3016 KB n=100
4 Correct 4 ms 3060 KB n=100
5 Correct 4 ms 3060 KB n=100
6 Correct 6 ms 3060 KB n=100
7 Correct 5 ms 3060 KB n=100
8 Correct 4 ms 3060 KB n=100
9 Correct 4 ms 3060 KB n=100
10 Correct 4 ms 3060 KB n=100
11 Correct 4 ms 3060 KB n=100
12 Correct 4 ms 3060 KB n=100
13 Correct 4 ms 3060 KB n=100
14 Correct 4 ms 3060 KB n=100
15 Correct 4 ms 3060 KB n=100
16 Correct 4 ms 3060 KB n=100
17 Correct 4 ms 3060 KB n=100
18 Correct 4 ms 3060 KB n=100
19 Correct 4 ms 3060 KB n=100
20 Correct 4 ms 3060 KB n=100
21 Correct 4 ms 3060 KB n=100
22 Correct 4 ms 3060 KB n=100
23 Correct 12 ms 3060 KB n=100
24 Correct 14 ms 3060 KB n=100
25 Correct 6 ms 3060 KB n=100
26 Correct 3 ms 3060 KB n=12
27 Correct 4 ms 3060 KB n=100
28 Correct 4 ms 3060 KB n=500
29 Correct 17 ms 3060 KB n=500
30 Correct 18 ms 3060 KB n=500
31 Correct 19 ms 3060 KB n=500
32 Correct 4 ms 3060 KB n=500
33 Correct 18 ms 3060 KB n=500
34 Correct 4 ms 3060 KB n=500
35 Correct 14 ms 3060 KB n=500
36 Correct 28 ms 3132 KB n=500
37 Correct 27 ms 3132 KB n=500
38 Correct 26 ms 3132 KB n=500
39 Correct 16 ms 3132 KB n=500
40 Correct 16 ms 3132 KB n=500
41 Correct 16 ms 3132 KB n=500
42 Correct 17 ms 3132 KB n=500
43 Correct 17 ms 3132 KB n=500
44 Correct 16 ms 3132 KB n=500
45 Correct 4 ms 3132 KB n=500
46 Correct 20 ms 3132 KB n=500
47 Correct 14 ms 3132 KB n=500
48 Correct 4 ms 3132 KB n=500
49 Correct 13 ms 3132 KB n=500
50 Correct 8 ms 3132 KB n=500
51 Correct 18 ms 3132 KB n=500
52 Correct 22 ms 3132 KB n=500
53 Correct 15 ms 3132 KB n=500
54 Correct 250 ms 3132 KB n=500
55 Correct 7 ms 3132 KB n=278
56 Correct 2315 ms 3132 KB n=500
57 Correct 2391 ms 3132 KB n=500
58 Correct 14 ms 3132 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB n=5
2 Correct 4 ms 2812 KB n=100
3 Correct 4 ms 3016 KB n=100
4 Correct 4 ms 3060 KB n=100
5 Correct 4 ms 3060 KB n=100
6 Correct 6 ms 3060 KB n=100
7 Correct 5 ms 3060 KB n=100
8 Correct 4 ms 3060 KB n=100
9 Correct 4 ms 3060 KB n=100
10 Correct 4 ms 3060 KB n=100
11 Correct 4 ms 3060 KB n=100
12 Correct 4 ms 3060 KB n=100
13 Correct 4 ms 3060 KB n=100
14 Correct 4 ms 3060 KB n=100
15 Correct 4 ms 3060 KB n=100
16 Correct 4 ms 3060 KB n=100
17 Correct 4 ms 3060 KB n=100
18 Correct 4 ms 3060 KB n=100
19 Correct 4 ms 3060 KB n=100
20 Correct 4 ms 3060 KB n=100
21 Correct 4 ms 3060 KB n=100
22 Correct 4 ms 3060 KB n=100
23 Correct 12 ms 3060 KB n=100
24 Correct 14 ms 3060 KB n=100
25 Correct 6 ms 3060 KB n=100
26 Correct 3 ms 3060 KB n=12
27 Correct 4 ms 3060 KB n=100
28 Correct 4 ms 3060 KB n=500
29 Correct 17 ms 3060 KB n=500
30 Correct 18 ms 3060 KB n=500
31 Correct 19 ms 3060 KB n=500
32 Correct 4 ms 3060 KB n=500
33 Correct 18 ms 3060 KB n=500
34 Correct 4 ms 3060 KB n=500
35 Correct 14 ms 3060 KB n=500
36 Correct 28 ms 3132 KB n=500
37 Correct 27 ms 3132 KB n=500
38 Correct 26 ms 3132 KB n=500
39 Correct 16 ms 3132 KB n=500
40 Correct 16 ms 3132 KB n=500
41 Correct 16 ms 3132 KB n=500
42 Correct 17 ms 3132 KB n=500
43 Correct 17 ms 3132 KB n=500
44 Correct 16 ms 3132 KB n=500
45 Correct 4 ms 3132 KB n=500
46 Correct 20 ms 3132 KB n=500
47 Correct 14 ms 3132 KB n=500
48 Correct 4 ms 3132 KB n=500
49 Correct 13 ms 3132 KB n=500
50 Correct 8 ms 3132 KB n=500
51 Correct 18 ms 3132 KB n=500
52 Correct 22 ms 3132 KB n=500
53 Correct 15 ms 3132 KB n=500
54 Correct 250 ms 3132 KB n=500
55 Correct 7 ms 3132 KB n=278
56 Correct 2315 ms 3132 KB n=500
57 Correct 2391 ms 3132 KB n=500
58 Correct 14 ms 3132 KB n=500
59 Correct 7 ms 3284 KB n=2000
60 Correct 301 ms 3396 KB n=2000
61 Correct 259 ms 3396 KB n=2000
62 Correct 300 ms 3396 KB n=2000
63 Correct 8 ms 3396 KB n=2000
64 Correct 304 ms 3472 KB n=2000
65 Correct 7 ms 3472 KB n=2000
66 Correct 351 ms 3472 KB n=2000
67 Correct 9 ms 3472 KB n=2000
68 Correct 287 ms 3472 KB n=2000
69 Correct 375 ms 3472 KB n=2000
70 Correct 371 ms 3504 KB n=2000
71 Correct 374 ms 3504 KB n=2000
72 Correct 186 ms 3504 KB n=2000
73 Correct 191 ms 3504 KB n=2000
74 Correct 6 ms 3504 KB n=1844
75 Correct 197 ms 3504 KB n=2000
76 Correct 221 ms 3504 KB n=2000
77 Correct 214 ms 3504 KB n=2000
78 Correct 231 ms 3504 KB n=2000
79 Correct 7 ms 3504 KB n=2000
80 Correct 207 ms 3504 KB n=2000
81 Correct 242 ms 3504 KB n=2000
82 Correct 7 ms 3504 KB n=2000
83 Correct 217 ms 3504 KB n=2000
84 Correct 69 ms 3504 KB n=2000
85 Correct 490 ms 3504 KB n=2000
86 Correct 199 ms 3504 KB n=2000
87 Correct 68 ms 3504 KB n=2000
88 Execution timed out 4097 ms 3504 KB Time limit exceeded
89 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB n=5
2 Correct 4 ms 2812 KB n=100
3 Correct 4 ms 3016 KB n=100
4 Correct 4 ms 3060 KB n=100
5 Correct 4 ms 3060 KB n=100
6 Correct 6 ms 3060 KB n=100
7 Correct 5 ms 3060 KB n=100
8 Correct 4 ms 3060 KB n=100
9 Correct 4 ms 3060 KB n=100
10 Correct 4 ms 3060 KB n=100
11 Correct 4 ms 3060 KB n=100
12 Correct 4 ms 3060 KB n=100
13 Correct 4 ms 3060 KB n=100
14 Correct 4 ms 3060 KB n=100
15 Correct 4 ms 3060 KB n=100
16 Correct 4 ms 3060 KB n=100
17 Correct 4 ms 3060 KB n=100
18 Correct 4 ms 3060 KB n=100
19 Correct 4 ms 3060 KB n=100
20 Correct 4 ms 3060 KB n=100
21 Correct 4 ms 3060 KB n=100
22 Correct 4 ms 3060 KB n=100
23 Correct 12 ms 3060 KB n=100
24 Correct 14 ms 3060 KB n=100
25 Correct 6 ms 3060 KB n=100
26 Correct 3 ms 3060 KB n=12
27 Correct 4 ms 3060 KB n=100
28 Correct 4 ms 3060 KB n=500
29 Correct 17 ms 3060 KB n=500
30 Correct 18 ms 3060 KB n=500
31 Correct 19 ms 3060 KB n=500
32 Correct 4 ms 3060 KB n=500
33 Correct 18 ms 3060 KB n=500
34 Correct 4 ms 3060 KB n=500
35 Correct 14 ms 3060 KB n=500
36 Correct 28 ms 3132 KB n=500
37 Correct 27 ms 3132 KB n=500
38 Correct 26 ms 3132 KB n=500
39 Correct 16 ms 3132 KB n=500
40 Correct 16 ms 3132 KB n=500
41 Correct 16 ms 3132 KB n=500
42 Correct 17 ms 3132 KB n=500
43 Correct 17 ms 3132 KB n=500
44 Correct 16 ms 3132 KB n=500
45 Correct 4 ms 3132 KB n=500
46 Correct 20 ms 3132 KB n=500
47 Correct 14 ms 3132 KB n=500
48 Correct 4 ms 3132 KB n=500
49 Correct 13 ms 3132 KB n=500
50 Correct 8 ms 3132 KB n=500
51 Correct 18 ms 3132 KB n=500
52 Correct 22 ms 3132 KB n=500
53 Correct 15 ms 3132 KB n=500
54 Correct 250 ms 3132 KB n=500
55 Correct 7 ms 3132 KB n=278
56 Correct 2315 ms 3132 KB n=500
57 Correct 2391 ms 3132 KB n=500
58 Correct 14 ms 3132 KB n=500
59 Correct 7 ms 3284 KB n=2000
60 Correct 301 ms 3396 KB n=2000
61 Correct 259 ms 3396 KB n=2000
62 Correct 300 ms 3396 KB n=2000
63 Correct 8 ms 3396 KB n=2000
64 Correct 304 ms 3472 KB n=2000
65 Correct 7 ms 3472 KB n=2000
66 Correct 351 ms 3472 KB n=2000
67 Correct 9 ms 3472 KB n=2000
68 Correct 287 ms 3472 KB n=2000
69 Correct 375 ms 3472 KB n=2000
70 Correct 371 ms 3504 KB n=2000
71 Correct 374 ms 3504 KB n=2000
72 Correct 186 ms 3504 KB n=2000
73 Correct 191 ms 3504 KB n=2000
74 Correct 6 ms 3504 KB n=1844
75 Correct 197 ms 3504 KB n=2000
76 Correct 221 ms 3504 KB n=2000
77 Correct 214 ms 3504 KB n=2000
78 Correct 231 ms 3504 KB n=2000
79 Correct 7 ms 3504 KB n=2000
80 Correct 207 ms 3504 KB n=2000
81 Correct 242 ms 3504 KB n=2000
82 Correct 7 ms 3504 KB n=2000
83 Correct 217 ms 3504 KB n=2000
84 Correct 69 ms 3504 KB n=2000
85 Correct 490 ms 3504 KB n=2000
86 Correct 199 ms 3504 KB n=2000
87 Correct 68 ms 3504 KB n=2000
88 Execution timed out 4097 ms 3504 KB Time limit exceeded
89 Halted 0 ms 0 KB -