Submission #503459

# Submission time Handle Problem Language Result Execution time Memory
503459 2022-01-08T04:30:33 Z sumit_kk10 Birthday gift (IZhO18_treearray) C++17
100 / 100
1541 ms 85408 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;
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28492 KB n=5
2 Correct 19 ms 28496 KB n=100
3 Correct 19 ms 28492 KB n=100
4 Correct 19 ms 28492 KB n=100
5 Correct 19 ms 28504 KB n=100
6 Correct 25 ms 28540 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 20 ms 28488 KB n=100
9 Correct 25 ms 28484 KB n=100
10 Correct 19 ms 28492 KB n=100
11 Correct 22 ms 28452 KB n=100
12 Correct 19 ms 28492 KB n=100
13 Correct 21 ms 28440 KB n=100
14 Correct 24 ms 28492 KB n=100
15 Correct 24 ms 28456 KB n=100
16 Correct 19 ms 28492 KB n=100
17 Correct 20 ms 28424 KB n=100
18 Correct 20 ms 28492 KB n=100
19 Correct 20 ms 28464 KB n=100
20 Correct 20 ms 28460 KB n=100
21 Correct 19 ms 28468 KB n=100
22 Correct 19 ms 28508 KB n=100
23 Correct 19 ms 28512 KB n=100
24 Correct 19 ms 28492 KB n=100
25 Correct 18 ms 28436 KB n=100
26 Correct 19 ms 28468 KB n=12
27 Correct 19 ms 28440 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28492 KB n=5
2 Correct 19 ms 28496 KB n=100
3 Correct 19 ms 28492 KB n=100
4 Correct 19 ms 28492 KB n=100
5 Correct 19 ms 28504 KB n=100
6 Correct 25 ms 28540 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 20 ms 28488 KB n=100
9 Correct 25 ms 28484 KB n=100
10 Correct 19 ms 28492 KB n=100
11 Correct 22 ms 28452 KB n=100
12 Correct 19 ms 28492 KB n=100
13 Correct 21 ms 28440 KB n=100
14 Correct 24 ms 28492 KB n=100
15 Correct 24 ms 28456 KB n=100
16 Correct 19 ms 28492 KB n=100
17 Correct 20 ms 28424 KB n=100
18 Correct 20 ms 28492 KB n=100
19 Correct 20 ms 28464 KB n=100
20 Correct 20 ms 28460 KB n=100
21 Correct 19 ms 28468 KB n=100
22 Correct 19 ms 28508 KB n=100
23 Correct 19 ms 28512 KB n=100
24 Correct 19 ms 28492 KB n=100
25 Correct 18 ms 28436 KB n=100
26 Correct 19 ms 28468 KB n=12
27 Correct 19 ms 28440 KB n=100
28 Correct 19 ms 28620 KB n=500
29 Correct 20 ms 28540 KB n=500
30 Correct 21 ms 28568 KB n=500
31 Correct 21 ms 28620 KB n=500
32 Correct 20 ms 28572 KB n=500
33 Correct 20 ms 28556 KB n=500
34 Correct 20 ms 28584 KB n=500
35 Correct 20 ms 28620 KB n=500
36 Correct 20 ms 28628 KB n=500
37 Correct 23 ms 28636 KB n=500
38 Correct 22 ms 28620 KB n=500
39 Correct 22 ms 28600 KB n=500
40 Correct 19 ms 28608 KB n=500
41 Correct 19 ms 28620 KB n=500
42 Correct 20 ms 28568 KB n=500
43 Correct 19 ms 28628 KB n=500
44 Correct 20 ms 28648 KB n=500
45 Correct 20 ms 28632 KB n=500
46 Correct 20 ms 28620 KB n=500
47 Correct 22 ms 28620 KB n=500
48 Correct 20 ms 28528 KB n=500
49 Correct 20 ms 28620 KB n=500
50 Correct 26 ms 28516 KB n=500
51 Correct 21 ms 28512 KB n=500
52 Correct 20 ms 28620 KB n=500
53 Correct 20 ms 28560 KB n=500
54 Correct 21 ms 28540 KB n=500
55 Correct 19 ms 28492 KB n=278
56 Correct 20 ms 28540 KB n=500
57 Correct 20 ms 28592 KB n=500
58 Correct 20 ms 28620 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28492 KB n=5
2 Correct 19 ms 28496 KB n=100
3 Correct 19 ms 28492 KB n=100
4 Correct 19 ms 28492 KB n=100
5 Correct 19 ms 28504 KB n=100
6 Correct 25 ms 28540 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 20 ms 28488 KB n=100
9 Correct 25 ms 28484 KB n=100
10 Correct 19 ms 28492 KB n=100
11 Correct 22 ms 28452 KB n=100
12 Correct 19 ms 28492 KB n=100
13 Correct 21 ms 28440 KB n=100
14 Correct 24 ms 28492 KB n=100
15 Correct 24 ms 28456 KB n=100
16 Correct 19 ms 28492 KB n=100
17 Correct 20 ms 28424 KB n=100
18 Correct 20 ms 28492 KB n=100
19 Correct 20 ms 28464 KB n=100
20 Correct 20 ms 28460 KB n=100
21 Correct 19 ms 28468 KB n=100
22 Correct 19 ms 28508 KB n=100
23 Correct 19 ms 28512 KB n=100
24 Correct 19 ms 28492 KB n=100
25 Correct 18 ms 28436 KB n=100
26 Correct 19 ms 28468 KB n=12
27 Correct 19 ms 28440 KB n=100
28 Correct 19 ms 28620 KB n=500
29 Correct 20 ms 28540 KB n=500
30 Correct 21 ms 28568 KB n=500
31 Correct 21 ms 28620 KB n=500
32 Correct 20 ms 28572 KB n=500
33 Correct 20 ms 28556 KB n=500
34 Correct 20 ms 28584 KB n=500
35 Correct 20 ms 28620 KB n=500
36 Correct 20 ms 28628 KB n=500
37 Correct 23 ms 28636 KB n=500
38 Correct 22 ms 28620 KB n=500
39 Correct 22 ms 28600 KB n=500
40 Correct 19 ms 28608 KB n=500
41 Correct 19 ms 28620 KB n=500
42 Correct 20 ms 28568 KB n=500
43 Correct 19 ms 28628 KB n=500
44 Correct 20 ms 28648 KB n=500
45 Correct 20 ms 28632 KB n=500
46 Correct 20 ms 28620 KB n=500
47 Correct 22 ms 28620 KB n=500
48 Correct 20 ms 28528 KB n=500
49 Correct 20 ms 28620 KB n=500
50 Correct 26 ms 28516 KB n=500
51 Correct 21 ms 28512 KB n=500
52 Correct 20 ms 28620 KB n=500
53 Correct 20 ms 28560 KB n=500
54 Correct 21 ms 28540 KB n=500
55 Correct 19 ms 28492 KB n=278
56 Correct 20 ms 28540 KB n=500
57 Correct 20 ms 28592 KB n=500
58 Correct 20 ms 28620 KB n=500
59 Correct 23 ms 29000 KB n=2000
60 Correct 24 ms 28916 KB n=2000
61 Correct 30 ms 28876 KB n=2000
62 Correct 24 ms 28876 KB n=2000
63 Correct 22 ms 28928 KB n=2000
64 Correct 23 ms 28980 KB n=2000
65 Correct 24 ms 28876 KB n=2000
66 Correct 23 ms 28876 KB n=2000
67 Correct 22 ms 28876 KB n=2000
68 Correct 25 ms 28984 KB n=2000
69 Correct 22 ms 28936 KB n=2000
70 Correct 29 ms 28956 KB n=2000
71 Correct 22 ms 28960 KB n=2000
72 Correct 21 ms 29004 KB n=2000
73 Correct 22 ms 29004 KB n=2000
74 Correct 22 ms 28888 KB n=1844
75 Correct 24 ms 28940 KB n=2000
76 Correct 30 ms 29008 KB n=2000
77 Correct 30 ms 28988 KB n=2000
78 Correct 23 ms 28932 KB n=2000
79 Correct 21 ms 28876 KB n=2000
80 Correct 24 ms 28876 KB n=2000
81 Correct 24 ms 28936 KB n=2000
82 Correct 22 ms 28944 KB n=2000
83 Correct 33 ms 28976 KB n=2000
84 Correct 23 ms 28868 KB n=2000
85 Correct 24 ms 28904 KB n=2000
86 Correct 24 ms 28876 KB n=2000
87 Correct 21 ms 28996 KB n=2000
88 Correct 24 ms 28888 KB n=2000
89 Correct 24 ms 28980 KB n=2000
90 Correct 24 ms 28928 KB n=2000
91 Correct 21 ms 28972 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28492 KB n=5
2 Correct 19 ms 28496 KB n=100
3 Correct 19 ms 28492 KB n=100
4 Correct 19 ms 28492 KB n=100
5 Correct 19 ms 28504 KB n=100
6 Correct 25 ms 28540 KB n=100
7 Correct 20 ms 28492 KB n=100
8 Correct 20 ms 28488 KB n=100
9 Correct 25 ms 28484 KB n=100
10 Correct 19 ms 28492 KB n=100
11 Correct 22 ms 28452 KB n=100
12 Correct 19 ms 28492 KB n=100
13 Correct 21 ms 28440 KB n=100
14 Correct 24 ms 28492 KB n=100
15 Correct 24 ms 28456 KB n=100
16 Correct 19 ms 28492 KB n=100
17 Correct 20 ms 28424 KB n=100
18 Correct 20 ms 28492 KB n=100
19 Correct 20 ms 28464 KB n=100
20 Correct 20 ms 28460 KB n=100
21 Correct 19 ms 28468 KB n=100
22 Correct 19 ms 28508 KB n=100
23 Correct 19 ms 28512 KB n=100
24 Correct 19 ms 28492 KB n=100
25 Correct 18 ms 28436 KB n=100
26 Correct 19 ms 28468 KB n=12
27 Correct 19 ms 28440 KB n=100
28 Correct 19 ms 28620 KB n=500
29 Correct 20 ms 28540 KB n=500
30 Correct 21 ms 28568 KB n=500
31 Correct 21 ms 28620 KB n=500
32 Correct 20 ms 28572 KB n=500
33 Correct 20 ms 28556 KB n=500
34 Correct 20 ms 28584 KB n=500
35 Correct 20 ms 28620 KB n=500
36 Correct 20 ms 28628 KB n=500
37 Correct 23 ms 28636 KB n=500
38 Correct 22 ms 28620 KB n=500
39 Correct 22 ms 28600 KB n=500
40 Correct 19 ms 28608 KB n=500
41 Correct 19 ms 28620 KB n=500
42 Correct 20 ms 28568 KB n=500
43 Correct 19 ms 28628 KB n=500
44 Correct 20 ms 28648 KB n=500
45 Correct 20 ms 28632 KB n=500
46 Correct 20 ms 28620 KB n=500
47 Correct 22 ms 28620 KB n=500
48 Correct 20 ms 28528 KB n=500
49 Correct 20 ms 28620 KB n=500
50 Correct 26 ms 28516 KB n=500
51 Correct 21 ms 28512 KB n=500
52 Correct 20 ms 28620 KB n=500
53 Correct 20 ms 28560 KB n=500
54 Correct 21 ms 28540 KB n=500
55 Correct 19 ms 28492 KB n=278
56 Correct 20 ms 28540 KB n=500
57 Correct 20 ms 28592 KB n=500
58 Correct 20 ms 28620 KB n=500
59 Correct 23 ms 29000 KB n=2000
60 Correct 24 ms 28916 KB n=2000
61 Correct 30 ms 28876 KB n=2000
62 Correct 24 ms 28876 KB n=2000
63 Correct 22 ms 28928 KB n=2000
64 Correct 23 ms 28980 KB n=2000
65 Correct 24 ms 28876 KB n=2000
66 Correct 23 ms 28876 KB n=2000
67 Correct 22 ms 28876 KB n=2000
68 Correct 25 ms 28984 KB n=2000
69 Correct 22 ms 28936 KB n=2000
70 Correct 29 ms 28956 KB n=2000
71 Correct 22 ms 28960 KB n=2000
72 Correct 21 ms 29004 KB n=2000
73 Correct 22 ms 29004 KB n=2000
74 Correct 22 ms 28888 KB n=1844
75 Correct 24 ms 28940 KB n=2000
76 Correct 30 ms 29008 KB n=2000
77 Correct 30 ms 28988 KB n=2000
78 Correct 23 ms 28932 KB n=2000
79 Correct 21 ms 28876 KB n=2000
80 Correct 24 ms 28876 KB n=2000
81 Correct 24 ms 28936 KB n=2000
82 Correct 22 ms 28944 KB n=2000
83 Correct 33 ms 28976 KB n=2000
84 Correct 23 ms 28868 KB n=2000
85 Correct 24 ms 28904 KB n=2000
86 Correct 24 ms 28876 KB n=2000
87 Correct 21 ms 28996 KB n=2000
88 Correct 24 ms 28888 KB n=2000
89 Correct 24 ms 28980 KB n=2000
90 Correct 24 ms 28928 KB n=2000
91 Correct 21 ms 28972 KB n=2000
92 Correct 686 ms 77340 KB n=200000
93 Correct 1537 ms 76596 KB n=200000
94 Correct 1484 ms 76408 KB n=200000
95 Correct 695 ms 77232 KB n=200000
96 Correct 680 ms 77352 KB n=200000
97 Correct 1536 ms 76444 KB n=200000
98 Correct 684 ms 77300 KB n=200000
99 Correct 1008 ms 76688 KB n=200000
100 Correct 711 ms 77316 KB n=200000
101 Correct 1439 ms 76432 KB n=200000
102 Correct 470 ms 80020 KB n=200000
103 Correct 427 ms 84888 KB n=200000
104 Correct 439 ms 84920 KB n=200000
105 Correct 429 ms 85340 KB n=200000
106 Correct 425 ms 85300 KB n=200000
107 Correct 429 ms 85408 KB n=200000
108 Correct 871 ms 83764 KB n=200000
109 Correct 884 ms 83748 KB n=200000
110 Correct 870 ms 83780 KB n=200000
111 Correct 695 ms 83208 KB n=200000
112 Correct 1477 ms 84264 KB n=200000
113 Correct 1541 ms 83936 KB n=200000
114 Correct 739 ms 83380 KB n=200000
115 Correct 1528 ms 84444 KB n=200000
116 Correct 658 ms 84128 KB n=200000
117 Correct 1402 ms 83868 KB n=200000
118 Correct 1511 ms 84096 KB n=200000
119 Correct 658 ms 83844 KB n=200000
120 Correct 1365 ms 83000 KB n=200000
121 Correct 1384 ms 83048 KB n=200000
122 Correct 1431 ms 83348 KB n=200000
123 Correct 475 ms 85088 KB n=200000
124 Correct 339 ms 42916 KB n=25264