답안 #90582

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
90582 2018-12-22T15:04:53 Z mrtsima22 Birthday gift (IZhO18_treearray) C++17
0 / 100
5 ms 2948 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 b[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>>b[i];
}
go(1,0,0);
mshobelize(1,1);
t_out[0]=999999999;
while(q--)
{
	cin>>tp;
	if(tp==1)
	{
		cin>>u>>v;
		b[u]=v;
	}
	else if(tp==2){
	cin>>u>>v>>x;
	fin=0;
		for(int L=u;!fin&&L<=v;L++)
		{
			int c=b[L];
			for(int R=L;!fin&&R<=v;R++)
			{
//				while(!is_it(c,b[R]))c=p[c][1];
				c=lca(c,b[R]);
				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 2652 KB n=5
2 Correct 4 ms 2812 KB n=100
3 Correct 4 ms 2812 KB n=100
4 Correct 4 ms 2812 KB n=100
5 Correct 4 ms 2888 KB n=100
6 Correct 4 ms 2888 KB n=100
7 Correct 5 ms 2948 KB n=100
8 Correct 4 ms 2948 KB n=100
9 Correct 4 ms 2948 KB n=100
10 Correct 4 ms 2948 KB n=100
11 Correct 4 ms 2948 KB n=100
12 Correct 3 ms 2948 KB n=100
13 Correct 4 ms 2948 KB n=100
14 Correct 3 ms 2948 KB n=100
15 Incorrect 4 ms 2948 KB Jury has the answer but participant has not
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2652 KB n=5
2 Correct 4 ms 2812 KB n=100
3 Correct 4 ms 2812 KB n=100
4 Correct 4 ms 2812 KB n=100
5 Correct 4 ms 2888 KB n=100
6 Correct 4 ms 2888 KB n=100
7 Correct 5 ms 2948 KB n=100
8 Correct 4 ms 2948 KB n=100
9 Correct 4 ms 2948 KB n=100
10 Correct 4 ms 2948 KB n=100
11 Correct 4 ms 2948 KB n=100
12 Correct 3 ms 2948 KB n=100
13 Correct 4 ms 2948 KB n=100
14 Correct 3 ms 2948 KB n=100
15 Incorrect 4 ms 2948 KB Jury has the answer but participant has not
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2652 KB n=5
2 Correct 4 ms 2812 KB n=100
3 Correct 4 ms 2812 KB n=100
4 Correct 4 ms 2812 KB n=100
5 Correct 4 ms 2888 KB n=100
6 Correct 4 ms 2888 KB n=100
7 Correct 5 ms 2948 KB n=100
8 Correct 4 ms 2948 KB n=100
9 Correct 4 ms 2948 KB n=100
10 Correct 4 ms 2948 KB n=100
11 Correct 4 ms 2948 KB n=100
12 Correct 3 ms 2948 KB n=100
13 Correct 4 ms 2948 KB n=100
14 Correct 3 ms 2948 KB n=100
15 Incorrect 4 ms 2948 KB Jury has the answer but participant has not
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2652 KB n=5
2 Correct 4 ms 2812 KB n=100
3 Correct 4 ms 2812 KB n=100
4 Correct 4 ms 2812 KB n=100
5 Correct 4 ms 2888 KB n=100
6 Correct 4 ms 2888 KB n=100
7 Correct 5 ms 2948 KB n=100
8 Correct 4 ms 2948 KB n=100
9 Correct 4 ms 2948 KB n=100
10 Correct 4 ms 2948 KB n=100
11 Correct 4 ms 2948 KB n=100
12 Correct 3 ms 2948 KB n=100
13 Correct 4 ms 2948 KB n=100
14 Correct 3 ms 2948 KB n=100
15 Incorrect 4 ms 2948 KB Jury has the answer but participant has not
16 Halted 0 ms 0 KB -