답안 #1109150

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1109150 2024-11-06T06:11:26 Z Muhammet Birthday gift (IZhO18_treearray) C++17
0 / 100
1 ms 336 KB
#include <bits/stdc++.h>

using namespace std;

#define sz(s) (int)s.size()

vector <vector <int>> v, sp;

vector <int> h, p;

void dfs(int x, int y){
	h[x] = h[y]+1;
	p[x] = y;
	for(auto i : v[x]){
		if(i != y){
			dfs(i,x);
		}
	}
}

int lca(int x, int y){
	if(h[x] < h[y]) swap(x,y);
	for(int i = 20; i >= 0; i--){
		if(h[sp[x][i]] >= h[y]){
			x = sp[x][i];
		}
	}
	for(int i = 20; i >= 0; i--){
		if(sp[x][i] != sp[y][i]){
			x = sp[x][i];
			y = sp[y][i];
		}
	}
	if(x == y) return x;
	return p[x];
}

int main(){
	int n, m, q;
	cin >> n >> m >> q;
	v.resize(n+1);
	h.resize(n+1,0);
	for(int i = 1; i < n; i++){
		int u1, u2;
		cin >> u1 >> u2;
		v[u1].push_back(u2);
		v[u2].push_back(u1);
	}
	p.resize(n+1);
	sp.resize(n+1, vector <int> (25,0));
	dfs(1,0);
	for(int i = 1; i <= n; i++){
		sp[i][0] = p[i];
	}
	for(int j = 1; j <= 20; j++){
		for(int i = 1; i <= n; i++){
			sp[i][j] = sp[sp[i][j-1]][j-1];
		}
	}
	set <int> s[n+1], s1[n+1];
	vector <int> a(m+1);
	for(int i = 1; i <= m; i++){
		cin >> a[i];
		s1[a[i]].insert(i);
		if(i != 1){
			s[lca(a[i],a[i-1])].insert(i-1);
		}
	}
	for(int i = 1; i <= q; i++){
		int t;
		cin >> t;
		if(t == 1){
			int in, vl;
			cin >> in >> vl;
			s1[a[in]].erase(s1[a[in]].find(in));
			if(in != 1){
				int x = lca(a[in],a[in-1]);
				s[x].erase(s[x].find(in-1));
			}
			if(in != m){
				int x = lca(a[in],a[in+1]);
				s[x].erase(s[x].find(in));
			}
			a[in] = vl;
			if(in != 1){
				int x = lca(a[in],a[in-1]);
				s[x].insert(in-1);
			}
			if(in != m){
				int x = lca(a[in],a[in+1]);
				s[x].insert(in);
			}
			s1[vl].insert(in);
		}
		else {
			int l, r, vl;
			cin >> l >> r >> vl;
			int x = m, y = m;
			if(sz(s1[vl]) > 0 and *(--s1[vl].end()) >= l) x = (*s1[vl].lower_bound(l));
			if(sz(s[vl]) > 0 and *(--s[vl].end()) >= l) y = (*s[vl].lower_bound(l));
			if(x <= r){
				cout << x << ' ' << x << '\n';
			}
			else if(y < r){
				cout << y << ' ' << y+1 << '\n';
			}
			else {
				cout << "-1 -1\n";
			}
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Incorrect 1 ms 336 KB Wrong range.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Incorrect 1 ms 336 KB Wrong range.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Incorrect 1 ms 336 KB Wrong range.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Incorrect 1 ms 336 KB Wrong range.
3 Halted 0 ms 0 KB -