답안 #503458

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
503458 2022-01-08T04:29:45 Z sumit_kk10 Birthday gift (IZhO18_treearray) C++17
0 / 100
20 ms 28512 KB
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ll long long int
#define ld long double
using namespace std;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
int n, m, q, lvl[N], tin[N], tout[N], timer;
vector<int> graph[N];
vector<vector<int> > dp(N, vector<int>(20, -1));
 
void dfs(int source, int par, int level){
	dp[source][0] = par;
	lvl[source] = level;
	tin[source] = ++timer;
	for(auto k : graph[source])
		if(k != par)
			dfs(k, source, level + 1);
	tout[source] = ++timer;
}
 
void init(){
	dfs(1, -1, 0);
	for(int i = 1; i <= (log2(n)); ++i)
		for(int j = 1; j <= n; ++j)
			if(dp[j][i - 1] != -1)
				dp[j][i] = dp[dp[j][i - 1]][i - 1];
}
 
int LCA(int u, int v){
	if(lvl[u] > lvl[v]) swap(u, v);
	ll d = lvl[v] - lvl[u];
	while(d){
		int jump = log2(d);
		v = dp[v][jump];
		d -= pow(2, jump);
	}
	if(v == u) 
		return v;
	for(int i = log2(n); i >= 0; --i){
		if(dp[v][i] != -1 && dp[v][i] != dp[u][i]){
			u = dp[u][i];
			v = dp[v][i];
		}
	}
	return dp[v][0];
}
 
void solve(){
	cin >> n >> m >> q;
	vector<int> seq;
	for(int i = 0; i < n - 1; ++i){
		int u, v;
		cin >> u >> v;
		graph[u].push_back(v);
		graph[v].push_back(u);
	}
	for(int i = 0; i < m; ++i){
		int x;
		cin >> x;
		seq.push_back(x);
	}
	init();
	set<int> res[n + 1], val[n + 1];
	for(int i = 0; i < m - 1; ++i)
		res[LCA(seq[i], seq[i + 1])].insert(i);
	for(int i = 0; i < m; ++i)
		val[seq[i]].insert(i);
	while(q--){
		int query;
		cin >> query;
		if(query == 1){
			int x, v;
			cin >> x >> v;
			--x;
			auto k = val[seq[x]].find(x);
			if(k != val[seq[x]].end())
				val[seq[x]].erase(k);
			val[v].insert(x);
			if(x != 0){
				k = res[LCA(seq[x - 1], seq[x])].find(x - 1);
				if(k != res[LCA(seq[x - 1], seq[x])].end())
					res[LCA(seq[x - 1], seq[x])].erase(k);
				res[LCA(seq[x - 1], v)].insert(x - 1);
			}
			if(x != m - 1){
				k = res[LCA(seq[x], seq[x + 1])].find(x);
				if(k != res[LCA(seq[x], seq[x + 1])].end())
					res[LCA(seq[x], seq[x + 1])].erase(k);
				res[LCA(v, seq[x + 1])].insert(x);
			}
			seq[x] = v;
		}
		else{
			int l, r, v;
			cin >> l >> r >> v;
			--l, --r;
			auto x = val[v].lower_bound(l);
			auto k = res[v].lower_bound(l);
			if(*x <= r and x != val[v].end())
				cout << *x + 1 << ' ' << *x + 1 << '\n';
			else if(k != res[v].end() and *k <= r)
				cout << *k + 1 << ' ' << *k + 2 << '\n';
			else
				cout << "-1 -1\n";
		}
	}
}
 
int main(){
	fast;
	int t = 1;
	// cin >> t;
	while(t--)
		solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 28492 KB n=5
2 Correct 19 ms 28460 KB n=100
3 Correct 19 ms 28476 KB n=100
4 Correct 19 ms 28476 KB n=100
5 Correct 19 ms 28492 KB n=100
6 Correct 19 ms 28432 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 18 ms 28492 KB n=100
9 Correct 18 ms 28472 KB n=100
10 Correct 19 ms 28436 KB n=100
11 Correct 19 ms 28504 KB n=100
12 Incorrect 18 ms 28512 KB Wrong output format.
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 28492 KB n=5
2 Correct 19 ms 28460 KB n=100
3 Correct 19 ms 28476 KB n=100
4 Correct 19 ms 28476 KB n=100
5 Correct 19 ms 28492 KB n=100
6 Correct 19 ms 28432 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 18 ms 28492 KB n=100
9 Correct 18 ms 28472 KB n=100
10 Correct 19 ms 28436 KB n=100
11 Correct 19 ms 28504 KB n=100
12 Incorrect 18 ms 28512 KB Wrong output format.
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 28492 KB n=5
2 Correct 19 ms 28460 KB n=100
3 Correct 19 ms 28476 KB n=100
4 Correct 19 ms 28476 KB n=100
5 Correct 19 ms 28492 KB n=100
6 Correct 19 ms 28432 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 18 ms 28492 KB n=100
9 Correct 18 ms 28472 KB n=100
10 Correct 19 ms 28436 KB n=100
11 Correct 19 ms 28504 KB n=100
12 Incorrect 18 ms 28512 KB Wrong output format.
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 28492 KB n=5
2 Correct 19 ms 28460 KB n=100
3 Correct 19 ms 28476 KB n=100
4 Correct 19 ms 28476 KB n=100
5 Correct 19 ms 28492 KB n=100
6 Correct 19 ms 28432 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 18 ms 28492 KB n=100
9 Correct 18 ms 28472 KB n=100
10 Correct 19 ms 28436 KB n=100
11 Correct 19 ms 28504 KB n=100
12 Incorrect 18 ms 28512 KB Wrong output format.
13 Halted 0 ms 0 KB -