Submission #93930

# Submission time Handle Problem Language Result Execution time Memory
93930 2019-01-13T09:43:24 Z mrtsima22 Birthday gift (IZhO18_treearray) C++17
0 / 100
12 ms 12152 KB
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int n,k,q,t_out[100003],p[100003][33];
vector<int>a[100003];
int ba[100003],t_in[100004],T,x;
set<int>s1[100003],s2[100003];
set<int>::iterator it1,it2;
bool is_it(int a,int b){
	if (t_in[a]<=t_in[b]&&t_out[b]<=t_out[a]){
		return true;
	}else{
		return false;
	}
}
void go(int s,int parent){
	t_in[s]=++T;
	p[s][0]=parent;
	int j=s;	
	for (int i=1;i<=30;i++){
		p[j][i]=p[p[j][i-1]][i-1];
	}
	for(int i=0;i<(int)a[s].size();i++){
		if(a[s][i]==parent)continue;
		go(a[s][i],s);
	}
	t_out[s]=++T;
}
int lca(int a,int b){
	if(t_in[a]>t_in[b]){
		swap(a,b);
	}
	if(is_it(a,b)){
		return a;
	}
	if(is_it(b,a)){
		return b;
	}
	for(int i=30;i>=0;i--){
		if(p[a][i]==0)continue;
		if(is_it(p[a][i],b)==0){
			a=p[a][i];
		}
	}
	return p[a][0];
}
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);
for(int j=1;j<=n;j++){

}
for(int i=1;i<=k;i++){
	s2[ba[i]].insert(i);
}
for(int i=1;i<k;i++){
	s1[lca(ba[i],ba[i+1])].insert(i);
}

while(q--){
	cin>>tp;
	if(tp==1){
		cin>>u>>v;
		if(u>1){
			s1[lca(ba[u-1],ba[u])].erase(u-1);
		}
		if(x<k){
			s1[lca(ba[u],ba[u+1])].erase(u);
		}
		s2[ba[u]].erase(u);
		ba[u]=v;
		if(u>1){
			s1[lca(ba[u-1],ba[u])].insert(u-1);
		}
		if(u<k){
			s1[lca(ba[u],ba[u+1])].insert(u);
		}
		s2[ba[u]].insert(u);
	}else{
		cin>>u>>v>>x;
		it1=s1[x].lower_bound(u);
		it2=s2[x].lower_bound(u);
		
		if(it2!=s2[x].end()&&(*it2)<=v){
			cout<<(*it2)<<" "<<(*it2)<<endl;
		}else{
			if(it1!=s1[u].end()){
				if((*it1)<v){
					cout<<(*it1)<<" "<<(*it1)+1<<endl;
				}
				else{
					cout<<-1<<" "<<-1<<endl;
				}
			}else
				cout<<-1<<" "<<-1<<endl;
		}
	}

}
return 0;
}
/*

5 4 4
 1 2
  3 1 
  3 4 
  5  3 
  4 5 
  2 3
    2 1
    3 1 
  1 3 5 
  2 3 4 5
   2 1 3 1


*/
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB n=5
2 Incorrect 12 ms 12152 KB Wrong output format.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB n=5
2 Incorrect 12 ms 12152 KB Wrong output format.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB n=5
2 Incorrect 12 ms 12152 KB Wrong output format.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB n=5
2 Incorrect 12 ms 12152 KB Wrong output format.
3 Halted 0 ms 0 KB -