Submission #503451

# Submission time Handle Problem Language Result Execution time Memory
503451 2022-01-08T03:51:47 Z sumit_kk10 Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 48408 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];
}
 
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;
	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();
	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;
			int cur = -1, pre = -1;
			for(int i = l; i <= r; ++i){
				int x = seq[i];
				if(tin[x] >= tin[v] and tout[x] <= tout[v]){
					if(cur == -1){ 
						cur = x;
						pre = i;
					}else cur = LCA(cur, x);
				}
				else{
					cur = -1;
					pre = -1;
				}
				if(cur == v){
					cout << pre + 1 << ' ' << i + 1 << '\n';
					ok = 1;
					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 19 ms 28492 KB n=5
2 Correct 19 ms 28476 KB n=100
3 Correct 19 ms 28516 KB n=100
4 Correct 20 ms 28408 KB n=100
5 Correct 20 ms 28424 KB n=100
6 Correct 23 ms 28468 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 20 ms 28496 KB n=100
9 Correct 23 ms 28496 KB n=100
10 Correct 24 ms 28492 KB n=100
11 Correct 19 ms 28412 KB n=100
12 Correct 19 ms 28460 KB n=100
13 Correct 20 ms 28516 KB n=100
14 Correct 21 ms 28400 KB n=100
15 Correct 21 ms 28432 KB n=100
16 Correct 19 ms 28492 KB n=100
17 Correct 20 ms 28492 KB n=100
18 Correct 21 ms 28404 KB n=100
19 Correct 19 ms 28516 KB n=100
20 Correct 18 ms 28424 KB n=100
21 Correct 19 ms 28500 KB n=100
22 Correct 22 ms 28484 KB n=100
23 Correct 20 ms 28504 KB n=100
24 Correct 22 ms 28492 KB n=100
25 Correct 21 ms 28504 KB n=100
26 Correct 19 ms 28404 KB n=12
27 Correct 20 ms 28492 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28492 KB n=5
2 Correct 19 ms 28476 KB n=100
3 Correct 19 ms 28516 KB n=100
4 Correct 20 ms 28408 KB n=100
5 Correct 20 ms 28424 KB n=100
6 Correct 23 ms 28468 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 20 ms 28496 KB n=100
9 Correct 23 ms 28496 KB n=100
10 Correct 24 ms 28492 KB n=100
11 Correct 19 ms 28412 KB n=100
12 Correct 19 ms 28460 KB n=100
13 Correct 20 ms 28516 KB n=100
14 Correct 21 ms 28400 KB n=100
15 Correct 21 ms 28432 KB n=100
16 Correct 19 ms 28492 KB n=100
17 Correct 20 ms 28492 KB n=100
18 Correct 21 ms 28404 KB n=100
19 Correct 19 ms 28516 KB n=100
20 Correct 18 ms 28424 KB n=100
21 Correct 19 ms 28500 KB n=100
22 Correct 22 ms 28484 KB n=100
23 Correct 20 ms 28504 KB n=100
24 Correct 22 ms 28492 KB n=100
25 Correct 21 ms 28504 KB n=100
26 Correct 19 ms 28404 KB n=12
27 Correct 20 ms 28492 KB n=100
28 Correct 19 ms 28436 KB n=500
29 Correct 21 ms 28492 KB n=500
30 Correct 25 ms 28544 KB n=500
31 Correct 24 ms 28524 KB n=500
32 Correct 28 ms 28492 KB n=500
33 Correct 22 ms 28492 KB n=500
34 Correct 19 ms 28492 KB n=500
35 Correct 21 ms 28524 KB n=500
36 Correct 20 ms 28492 KB n=500
37 Correct 19 ms 28512 KB n=500
38 Correct 25 ms 28560 KB n=500
39 Correct 26 ms 28532 KB n=500
40 Correct 28 ms 28484 KB n=500
41 Correct 22 ms 28492 KB n=500
42 Correct 21 ms 28524 KB n=500
43 Correct 22 ms 28440 KB n=500
44 Correct 21 ms 28432 KB n=500
45 Correct 19 ms 28496 KB n=500
46 Correct 24 ms 28476 KB n=500
47 Correct 22 ms 28584 KB n=500
48 Correct 20 ms 28528 KB n=500
49 Correct 23 ms 28536 KB n=500
50 Correct 21 ms 28528 KB n=500
51 Correct 21 ms 28576 KB n=500
52 Correct 24 ms 28492 KB n=500
53 Correct 22 ms 28496 KB n=500
54 Correct 35 ms 28524 KB n=500
55 Correct 20 ms 28492 KB n=278
56 Correct 43 ms 28576 KB n=500
57 Correct 42 ms 28532 KB n=500
58 Correct 20 ms 28484 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28492 KB n=5
2 Correct 19 ms 28476 KB n=100
3 Correct 19 ms 28516 KB n=100
4 Correct 20 ms 28408 KB n=100
5 Correct 20 ms 28424 KB n=100
6 Correct 23 ms 28468 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 20 ms 28496 KB n=100
9 Correct 23 ms 28496 KB n=100
10 Correct 24 ms 28492 KB n=100
11 Correct 19 ms 28412 KB n=100
12 Correct 19 ms 28460 KB n=100
13 Correct 20 ms 28516 KB n=100
14 Correct 21 ms 28400 KB n=100
15 Correct 21 ms 28432 KB n=100
16 Correct 19 ms 28492 KB n=100
17 Correct 20 ms 28492 KB n=100
18 Correct 21 ms 28404 KB n=100
19 Correct 19 ms 28516 KB n=100
20 Correct 18 ms 28424 KB n=100
21 Correct 19 ms 28500 KB n=100
22 Correct 22 ms 28484 KB n=100
23 Correct 20 ms 28504 KB n=100
24 Correct 22 ms 28492 KB n=100
25 Correct 21 ms 28504 KB n=100
26 Correct 19 ms 28404 KB n=12
27 Correct 20 ms 28492 KB n=100
28 Correct 19 ms 28436 KB n=500
29 Correct 21 ms 28492 KB n=500
30 Correct 25 ms 28544 KB n=500
31 Correct 24 ms 28524 KB n=500
32 Correct 28 ms 28492 KB n=500
33 Correct 22 ms 28492 KB n=500
34 Correct 19 ms 28492 KB n=500
35 Correct 21 ms 28524 KB n=500
36 Correct 20 ms 28492 KB n=500
37 Correct 19 ms 28512 KB n=500
38 Correct 25 ms 28560 KB n=500
39 Correct 26 ms 28532 KB n=500
40 Correct 28 ms 28484 KB n=500
41 Correct 22 ms 28492 KB n=500
42 Correct 21 ms 28524 KB n=500
43 Correct 22 ms 28440 KB n=500
44 Correct 21 ms 28432 KB n=500
45 Correct 19 ms 28496 KB n=500
46 Correct 24 ms 28476 KB n=500
47 Correct 22 ms 28584 KB n=500
48 Correct 20 ms 28528 KB n=500
49 Correct 23 ms 28536 KB n=500
50 Correct 21 ms 28528 KB n=500
51 Correct 21 ms 28576 KB n=500
52 Correct 24 ms 28492 KB n=500
53 Correct 22 ms 28496 KB n=500
54 Correct 35 ms 28524 KB n=500
55 Correct 20 ms 28492 KB n=278
56 Correct 43 ms 28576 KB n=500
57 Correct 42 ms 28532 KB n=500
58 Correct 20 ms 28484 KB n=500
59 Correct 24 ms 28620 KB n=2000
60 Correct 74 ms 28788 KB n=2000
61 Correct 64 ms 28660 KB n=2000
62 Correct 47 ms 28612 KB n=2000
63 Correct 21 ms 28692 KB n=2000
64 Correct 58 ms 28620 KB n=2000
65 Correct 23 ms 28616 KB n=2000
66 Correct 72 ms 28880 KB n=2000
67 Correct 20 ms 28620 KB n=2000
68 Correct 56 ms 28720 KB n=2000
69 Correct 23 ms 28620 KB n=2000
70 Correct 24 ms 28612 KB n=2000
71 Correct 31 ms 28640 KB n=2000
72 Correct 22 ms 28640 KB n=2000
73 Correct 22 ms 28684 KB n=2000
74 Correct 21 ms 28656 KB n=1844
75 Correct 24 ms 28616 KB n=2000
76 Correct 33 ms 28644 KB n=2000
77 Correct 32 ms 28620 KB n=2000
78 Correct 32 ms 28560 KB n=2000
79 Correct 21 ms 28676 KB n=2000
80 Correct 61 ms 28764 KB n=2000
81 Correct 53 ms 28628 KB n=2000
82 Correct 22 ms 28672 KB n=2000
83 Correct 55 ms 28708 KB n=2000
84 Correct 23 ms 28692 KB n=2000
85 Correct 48 ms 28704 KB n=2000
86 Correct 38 ms 28708 KB n=2000
87 Correct 23 ms 28620 KB n=2000
88 Correct 521 ms 28908 KB n=2000
89 Correct 535 ms 28784 KB n=2000
90 Correct 277 ms 28780 KB n=2000
91 Correct 25 ms 28616 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28492 KB n=5
2 Correct 19 ms 28476 KB n=100
3 Correct 19 ms 28516 KB n=100
4 Correct 20 ms 28408 KB n=100
5 Correct 20 ms 28424 KB n=100
6 Correct 23 ms 28468 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 20 ms 28496 KB n=100
9 Correct 23 ms 28496 KB n=100
10 Correct 24 ms 28492 KB n=100
11 Correct 19 ms 28412 KB n=100
12 Correct 19 ms 28460 KB n=100
13 Correct 20 ms 28516 KB n=100
14 Correct 21 ms 28400 KB n=100
15 Correct 21 ms 28432 KB n=100
16 Correct 19 ms 28492 KB n=100
17 Correct 20 ms 28492 KB n=100
18 Correct 21 ms 28404 KB n=100
19 Correct 19 ms 28516 KB n=100
20 Correct 18 ms 28424 KB n=100
21 Correct 19 ms 28500 KB n=100
22 Correct 22 ms 28484 KB n=100
23 Correct 20 ms 28504 KB n=100
24 Correct 22 ms 28492 KB n=100
25 Correct 21 ms 28504 KB n=100
26 Correct 19 ms 28404 KB n=12
27 Correct 20 ms 28492 KB n=100
28 Correct 19 ms 28436 KB n=500
29 Correct 21 ms 28492 KB n=500
30 Correct 25 ms 28544 KB n=500
31 Correct 24 ms 28524 KB n=500
32 Correct 28 ms 28492 KB n=500
33 Correct 22 ms 28492 KB n=500
34 Correct 19 ms 28492 KB n=500
35 Correct 21 ms 28524 KB n=500
36 Correct 20 ms 28492 KB n=500
37 Correct 19 ms 28512 KB n=500
38 Correct 25 ms 28560 KB n=500
39 Correct 26 ms 28532 KB n=500
40 Correct 28 ms 28484 KB n=500
41 Correct 22 ms 28492 KB n=500
42 Correct 21 ms 28524 KB n=500
43 Correct 22 ms 28440 KB n=500
44 Correct 21 ms 28432 KB n=500
45 Correct 19 ms 28496 KB n=500
46 Correct 24 ms 28476 KB n=500
47 Correct 22 ms 28584 KB n=500
48 Correct 20 ms 28528 KB n=500
49 Correct 23 ms 28536 KB n=500
50 Correct 21 ms 28528 KB n=500
51 Correct 21 ms 28576 KB n=500
52 Correct 24 ms 28492 KB n=500
53 Correct 22 ms 28496 KB n=500
54 Correct 35 ms 28524 KB n=500
55 Correct 20 ms 28492 KB n=278
56 Correct 43 ms 28576 KB n=500
57 Correct 42 ms 28532 KB n=500
58 Correct 20 ms 28484 KB n=500
59 Correct 24 ms 28620 KB n=2000
60 Correct 74 ms 28788 KB n=2000
61 Correct 64 ms 28660 KB n=2000
62 Correct 47 ms 28612 KB n=2000
63 Correct 21 ms 28692 KB n=2000
64 Correct 58 ms 28620 KB n=2000
65 Correct 23 ms 28616 KB n=2000
66 Correct 72 ms 28880 KB n=2000
67 Correct 20 ms 28620 KB n=2000
68 Correct 56 ms 28720 KB n=2000
69 Correct 23 ms 28620 KB n=2000
70 Correct 24 ms 28612 KB n=2000
71 Correct 31 ms 28640 KB n=2000
72 Correct 22 ms 28640 KB n=2000
73 Correct 22 ms 28684 KB n=2000
74 Correct 21 ms 28656 KB n=1844
75 Correct 24 ms 28616 KB n=2000
76 Correct 33 ms 28644 KB n=2000
77 Correct 32 ms 28620 KB n=2000
78 Correct 32 ms 28560 KB n=2000
79 Correct 21 ms 28676 KB n=2000
80 Correct 61 ms 28764 KB n=2000
81 Correct 53 ms 28628 KB n=2000
82 Correct 22 ms 28672 KB n=2000
83 Correct 55 ms 28708 KB n=2000
84 Correct 23 ms 28692 KB n=2000
85 Correct 48 ms 28704 KB n=2000
86 Correct 38 ms 28708 KB n=2000
87 Correct 23 ms 28620 KB n=2000
88 Correct 521 ms 28908 KB n=2000
89 Correct 535 ms 28784 KB n=2000
90 Correct 277 ms 28780 KB n=2000
91 Correct 25 ms 28616 KB n=2000
92 Correct 267 ms 46340 KB n=200000
93 Execution timed out 4091 ms 48408 KB Time limit exceeded
94 Halted 0 ms 0 KB -