Submission #338818

# Submission time Handle Problem Language Result Execution time Memory
338818 2020-12-24T03:40:01 Z sumit_kk10 Birthday gift (IZhO18_treearray) C++14
12 / 100
4000 ms 24044 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 = 1e6 + 5;
const int MOD = 1e9 + 7;
int n, m, q;
vector<int> graph[N];
vector<int> seq;
ll dp[N][(ll) log2(100005)], lvl[N];

void dfs(int source, int par, int level){
	dp[source][0] = par;
	lvl[source] = level;
	for(auto k : graph[source])
		if(k != par)
			dfs(k, source, level + 1);
}

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];
}

ll 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;
	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();
	while(q--){
		int query;
		cin >> query;
		if(query == 1){
			int x, val;
			cin >> x >> val;
			--x;
			seq[x] = val;
		}
		else{
			int l, r, v;
			cin >> l >> r >> v;
			--l, --r;
			bool ok = 0;
			for(int i = l; i <= r; ++i){
				if(seq[i] == v){
					cout << i + 1 << ' ' << i + 1 << '\n';
					ok = 1;
					break;
				}
			} 
			if(!ok){
				for(int i = l; i <= r; ++i){
					int lca = seq[i];
					for(int j = i; j <= r; ++j){
						lca = LCA(lca, seq[j]);
						if(lca == v){
							cout << i + 1 << ' ' << j + 1 << '\n'; 
							ok = 1;
							break;
						}
					}
					if(ok)
						break;
				}
				if(!ok)
					cout << "-1 -1\n";
			}
		}
	}
}

int main(){
	fast;
	int t = 1;
	// cin >> t;
	while(t--)
		solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23788 KB n=5
2 Correct 17 ms 23788 KB n=100
3 Correct 17 ms 23788 KB n=100
4 Correct 16 ms 23916 KB n=100
5 Correct 15 ms 23936 KB n=100
6 Correct 42 ms 23788 KB n=100
7 Correct 42 ms 23788 KB n=100
8 Correct 19 ms 23936 KB n=100
9 Correct 18 ms 23788 KB n=100
10 Correct 31 ms 23916 KB n=100
11 Correct 30 ms 23928 KB n=100
12 Correct 15 ms 23788 KB n=100
13 Correct 15 ms 23788 KB n=100
14 Correct 16 ms 23788 KB n=100
15 Correct 15 ms 23788 KB n=100
16 Correct 16 ms 23788 KB n=100
17 Correct 17 ms 23788 KB n=100
18 Correct 17 ms 23788 KB n=100
19 Correct 15 ms 23788 KB n=100
20 Correct 18 ms 23788 KB n=100
21 Correct 19 ms 23788 KB n=100
22 Correct 17 ms 23788 KB n=100
23 Correct 37 ms 23788 KB n=100
24 Correct 38 ms 23936 KB n=100
25 Correct 20 ms 23788 KB n=100
26 Correct 15 ms 23788 KB n=12
27 Correct 28 ms 23788 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23788 KB n=5
2 Correct 17 ms 23788 KB n=100
3 Correct 17 ms 23788 KB n=100
4 Correct 16 ms 23916 KB n=100
5 Correct 15 ms 23936 KB n=100
6 Correct 42 ms 23788 KB n=100
7 Correct 42 ms 23788 KB n=100
8 Correct 19 ms 23936 KB n=100
9 Correct 18 ms 23788 KB n=100
10 Correct 31 ms 23916 KB n=100
11 Correct 30 ms 23928 KB n=100
12 Correct 15 ms 23788 KB n=100
13 Correct 15 ms 23788 KB n=100
14 Correct 16 ms 23788 KB n=100
15 Correct 15 ms 23788 KB n=100
16 Correct 16 ms 23788 KB n=100
17 Correct 17 ms 23788 KB n=100
18 Correct 17 ms 23788 KB n=100
19 Correct 15 ms 23788 KB n=100
20 Correct 18 ms 23788 KB n=100
21 Correct 19 ms 23788 KB n=100
22 Correct 17 ms 23788 KB n=100
23 Correct 37 ms 23788 KB n=100
24 Correct 38 ms 23936 KB n=100
25 Correct 20 ms 23788 KB n=100
26 Correct 15 ms 23788 KB n=12
27 Correct 28 ms 23788 KB n=100
28 Correct 15 ms 23916 KB n=500
29 Correct 272 ms 24008 KB n=500
30 Correct 287 ms 24044 KB n=500
31 Correct 452 ms 24016 KB n=500
32 Correct 19 ms 23916 KB n=500
33 Correct 309 ms 24044 KB n=500
34 Correct 15 ms 23916 KB n=500
35 Correct 364 ms 23916 KB n=500
36 Execution timed out 4077 ms 24000 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23788 KB n=5
2 Correct 17 ms 23788 KB n=100
3 Correct 17 ms 23788 KB n=100
4 Correct 16 ms 23916 KB n=100
5 Correct 15 ms 23936 KB n=100
6 Correct 42 ms 23788 KB n=100
7 Correct 42 ms 23788 KB n=100
8 Correct 19 ms 23936 KB n=100
9 Correct 18 ms 23788 KB n=100
10 Correct 31 ms 23916 KB n=100
11 Correct 30 ms 23928 KB n=100
12 Correct 15 ms 23788 KB n=100
13 Correct 15 ms 23788 KB n=100
14 Correct 16 ms 23788 KB n=100
15 Correct 15 ms 23788 KB n=100
16 Correct 16 ms 23788 KB n=100
17 Correct 17 ms 23788 KB n=100
18 Correct 17 ms 23788 KB n=100
19 Correct 15 ms 23788 KB n=100
20 Correct 18 ms 23788 KB n=100
21 Correct 19 ms 23788 KB n=100
22 Correct 17 ms 23788 KB n=100
23 Correct 37 ms 23788 KB n=100
24 Correct 38 ms 23936 KB n=100
25 Correct 20 ms 23788 KB n=100
26 Correct 15 ms 23788 KB n=12
27 Correct 28 ms 23788 KB n=100
28 Correct 15 ms 23916 KB n=500
29 Correct 272 ms 24008 KB n=500
30 Correct 287 ms 24044 KB n=500
31 Correct 452 ms 24016 KB n=500
32 Correct 19 ms 23916 KB n=500
33 Correct 309 ms 24044 KB n=500
34 Correct 15 ms 23916 KB n=500
35 Correct 364 ms 23916 KB n=500
36 Execution timed out 4077 ms 24000 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23788 KB n=5
2 Correct 17 ms 23788 KB n=100
3 Correct 17 ms 23788 KB n=100
4 Correct 16 ms 23916 KB n=100
5 Correct 15 ms 23936 KB n=100
6 Correct 42 ms 23788 KB n=100
7 Correct 42 ms 23788 KB n=100
8 Correct 19 ms 23936 KB n=100
9 Correct 18 ms 23788 KB n=100
10 Correct 31 ms 23916 KB n=100
11 Correct 30 ms 23928 KB n=100
12 Correct 15 ms 23788 KB n=100
13 Correct 15 ms 23788 KB n=100
14 Correct 16 ms 23788 KB n=100
15 Correct 15 ms 23788 KB n=100
16 Correct 16 ms 23788 KB n=100
17 Correct 17 ms 23788 KB n=100
18 Correct 17 ms 23788 KB n=100
19 Correct 15 ms 23788 KB n=100
20 Correct 18 ms 23788 KB n=100
21 Correct 19 ms 23788 KB n=100
22 Correct 17 ms 23788 KB n=100
23 Correct 37 ms 23788 KB n=100
24 Correct 38 ms 23936 KB n=100
25 Correct 20 ms 23788 KB n=100
26 Correct 15 ms 23788 KB n=12
27 Correct 28 ms 23788 KB n=100
28 Correct 15 ms 23916 KB n=500
29 Correct 272 ms 24008 KB n=500
30 Correct 287 ms 24044 KB n=500
31 Correct 452 ms 24016 KB n=500
32 Correct 19 ms 23916 KB n=500
33 Correct 309 ms 24044 KB n=500
34 Correct 15 ms 23916 KB n=500
35 Correct 364 ms 23916 KB n=500
36 Execution timed out 4077 ms 24000 KB Time limit exceeded
37 Halted 0 ms 0 KB -