#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);
}
vector<int> res;
init();
for(int i = 0; i < m - 1; ++i)
res.push_back(LCA(seq[i], seq[i + 1]));
while(q--){
int query;
cin >> query;
if(query == 1){
int x, val;
cin >> x >> val;
--x;
seq[x] = val;
if(x != 0)
res[x - 1] = LCA(seq[x - 1], val);
if(x != m - 1)
res[x] = LCA(seq[x + 1], 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){
if(seq[i] == v){
cout << i + 1 << ' ' << i + 1 << '\n';
ok = 1;
break;
}
if(i != r and res[i] == v){
cout << i + 1 << ' ' << i + 2 << '\n';
ok = 1;
break;
}
}
if(!ok) cout << "-1 -1\n";
}
}
}
int main(){
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
return 0;
}
Compilation message
treearray.cpp: In function 'void solve()':
treearray.cpp:85:8: warning: unused variable 'cur' [-Wunused-variable]
85 | int cur = -1, pre = -1;
| ^~~
treearray.cpp:85:18: warning: unused variable 'pre' [-Wunused-variable]
85 | int cur = -1, pre = -1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28512 KB |
n=5 |
2 |
Correct |
21 ms |
28492 KB |
n=100 |
3 |
Correct |
19 ms |
28492 KB |
n=100 |
4 |
Correct |
23 ms |
28512 KB |
n=100 |
5 |
Correct |
21 ms |
28480 KB |
n=100 |
6 |
Correct |
24 ms |
28400 KB |
n=100 |
7 |
Correct |
20 ms |
28484 KB |
n=100 |
8 |
Correct |
19 ms |
28444 KB |
n=100 |
9 |
Correct |
19 ms |
28496 KB |
n=100 |
10 |
Correct |
20 ms |
28508 KB |
n=100 |
11 |
Correct |
25 ms |
28392 KB |
n=100 |
12 |
Correct |
20 ms |
28492 KB |
n=100 |
13 |
Correct |
23 ms |
28492 KB |
n=100 |
14 |
Correct |
20 ms |
28492 KB |
n=100 |
15 |
Correct |
19 ms |
28440 KB |
n=100 |
16 |
Correct |
19 ms |
28460 KB |
n=100 |
17 |
Correct |
23 ms |
28424 KB |
n=100 |
18 |
Correct |
19 ms |
28456 KB |
n=100 |
19 |
Correct |
22 ms |
28492 KB |
n=100 |
20 |
Correct |
20 ms |
28492 KB |
n=100 |
21 |
Correct |
23 ms |
28412 KB |
n=100 |
22 |
Correct |
20 ms |
28488 KB |
n=100 |
23 |
Correct |
20 ms |
28504 KB |
n=100 |
24 |
Correct |
20 ms |
28492 KB |
n=100 |
25 |
Correct |
19 ms |
28464 KB |
n=100 |
26 |
Correct |
18 ms |
28460 KB |
n=12 |
27 |
Correct |
19 ms |
28480 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28512 KB |
n=5 |
2 |
Correct |
21 ms |
28492 KB |
n=100 |
3 |
Correct |
19 ms |
28492 KB |
n=100 |
4 |
Correct |
23 ms |
28512 KB |
n=100 |
5 |
Correct |
21 ms |
28480 KB |
n=100 |
6 |
Correct |
24 ms |
28400 KB |
n=100 |
7 |
Correct |
20 ms |
28484 KB |
n=100 |
8 |
Correct |
19 ms |
28444 KB |
n=100 |
9 |
Correct |
19 ms |
28496 KB |
n=100 |
10 |
Correct |
20 ms |
28508 KB |
n=100 |
11 |
Correct |
25 ms |
28392 KB |
n=100 |
12 |
Correct |
20 ms |
28492 KB |
n=100 |
13 |
Correct |
23 ms |
28492 KB |
n=100 |
14 |
Correct |
20 ms |
28492 KB |
n=100 |
15 |
Correct |
19 ms |
28440 KB |
n=100 |
16 |
Correct |
19 ms |
28460 KB |
n=100 |
17 |
Correct |
23 ms |
28424 KB |
n=100 |
18 |
Correct |
19 ms |
28456 KB |
n=100 |
19 |
Correct |
22 ms |
28492 KB |
n=100 |
20 |
Correct |
20 ms |
28492 KB |
n=100 |
21 |
Correct |
23 ms |
28412 KB |
n=100 |
22 |
Correct |
20 ms |
28488 KB |
n=100 |
23 |
Correct |
20 ms |
28504 KB |
n=100 |
24 |
Correct |
20 ms |
28492 KB |
n=100 |
25 |
Correct |
19 ms |
28464 KB |
n=100 |
26 |
Correct |
18 ms |
28460 KB |
n=12 |
27 |
Correct |
19 ms |
28480 KB |
n=100 |
28 |
Correct |
20 ms |
28504 KB |
n=500 |
29 |
Correct |
20 ms |
28492 KB |
n=500 |
30 |
Correct |
20 ms |
28456 KB |
n=500 |
31 |
Correct |
20 ms |
28504 KB |
n=500 |
32 |
Correct |
20 ms |
28492 KB |
n=500 |
33 |
Correct |
22 ms |
28492 KB |
n=500 |
34 |
Correct |
20 ms |
28472 KB |
n=500 |
35 |
Correct |
19 ms |
28492 KB |
n=500 |
36 |
Correct |
19 ms |
28456 KB |
n=500 |
37 |
Correct |
19 ms |
28492 KB |
n=500 |
38 |
Correct |
19 ms |
28476 KB |
n=500 |
39 |
Correct |
20 ms |
28548 KB |
n=500 |
40 |
Correct |
22 ms |
28492 KB |
n=500 |
41 |
Correct |
19 ms |
28464 KB |
n=500 |
42 |
Correct |
19 ms |
28492 KB |
n=500 |
43 |
Correct |
19 ms |
28484 KB |
n=500 |
44 |
Correct |
19 ms |
28492 KB |
n=500 |
45 |
Correct |
20 ms |
28492 KB |
n=500 |
46 |
Correct |
20 ms |
28492 KB |
n=500 |
47 |
Correct |
20 ms |
28492 KB |
n=500 |
48 |
Correct |
20 ms |
28492 KB |
n=500 |
49 |
Correct |
27 ms |
28532 KB |
n=500 |
50 |
Correct |
21 ms |
28524 KB |
n=500 |
51 |
Correct |
19 ms |
28492 KB |
n=500 |
52 |
Correct |
20 ms |
28572 KB |
n=500 |
53 |
Correct |
20 ms |
28544 KB |
n=500 |
54 |
Correct |
19 ms |
28556 KB |
n=500 |
55 |
Correct |
19 ms |
28432 KB |
n=278 |
56 |
Correct |
19 ms |
28504 KB |
n=500 |
57 |
Correct |
22 ms |
28484 KB |
n=500 |
58 |
Correct |
27 ms |
28452 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28512 KB |
n=5 |
2 |
Correct |
21 ms |
28492 KB |
n=100 |
3 |
Correct |
19 ms |
28492 KB |
n=100 |
4 |
Correct |
23 ms |
28512 KB |
n=100 |
5 |
Correct |
21 ms |
28480 KB |
n=100 |
6 |
Correct |
24 ms |
28400 KB |
n=100 |
7 |
Correct |
20 ms |
28484 KB |
n=100 |
8 |
Correct |
19 ms |
28444 KB |
n=100 |
9 |
Correct |
19 ms |
28496 KB |
n=100 |
10 |
Correct |
20 ms |
28508 KB |
n=100 |
11 |
Correct |
25 ms |
28392 KB |
n=100 |
12 |
Correct |
20 ms |
28492 KB |
n=100 |
13 |
Correct |
23 ms |
28492 KB |
n=100 |
14 |
Correct |
20 ms |
28492 KB |
n=100 |
15 |
Correct |
19 ms |
28440 KB |
n=100 |
16 |
Correct |
19 ms |
28460 KB |
n=100 |
17 |
Correct |
23 ms |
28424 KB |
n=100 |
18 |
Correct |
19 ms |
28456 KB |
n=100 |
19 |
Correct |
22 ms |
28492 KB |
n=100 |
20 |
Correct |
20 ms |
28492 KB |
n=100 |
21 |
Correct |
23 ms |
28412 KB |
n=100 |
22 |
Correct |
20 ms |
28488 KB |
n=100 |
23 |
Correct |
20 ms |
28504 KB |
n=100 |
24 |
Correct |
20 ms |
28492 KB |
n=100 |
25 |
Correct |
19 ms |
28464 KB |
n=100 |
26 |
Correct |
18 ms |
28460 KB |
n=12 |
27 |
Correct |
19 ms |
28480 KB |
n=100 |
28 |
Correct |
20 ms |
28504 KB |
n=500 |
29 |
Correct |
20 ms |
28492 KB |
n=500 |
30 |
Correct |
20 ms |
28456 KB |
n=500 |
31 |
Correct |
20 ms |
28504 KB |
n=500 |
32 |
Correct |
20 ms |
28492 KB |
n=500 |
33 |
Correct |
22 ms |
28492 KB |
n=500 |
34 |
Correct |
20 ms |
28472 KB |
n=500 |
35 |
Correct |
19 ms |
28492 KB |
n=500 |
36 |
Correct |
19 ms |
28456 KB |
n=500 |
37 |
Correct |
19 ms |
28492 KB |
n=500 |
38 |
Correct |
19 ms |
28476 KB |
n=500 |
39 |
Correct |
20 ms |
28548 KB |
n=500 |
40 |
Correct |
22 ms |
28492 KB |
n=500 |
41 |
Correct |
19 ms |
28464 KB |
n=500 |
42 |
Correct |
19 ms |
28492 KB |
n=500 |
43 |
Correct |
19 ms |
28484 KB |
n=500 |
44 |
Correct |
19 ms |
28492 KB |
n=500 |
45 |
Correct |
20 ms |
28492 KB |
n=500 |
46 |
Correct |
20 ms |
28492 KB |
n=500 |
47 |
Correct |
20 ms |
28492 KB |
n=500 |
48 |
Correct |
20 ms |
28492 KB |
n=500 |
49 |
Correct |
27 ms |
28532 KB |
n=500 |
50 |
Correct |
21 ms |
28524 KB |
n=500 |
51 |
Correct |
19 ms |
28492 KB |
n=500 |
52 |
Correct |
20 ms |
28572 KB |
n=500 |
53 |
Correct |
20 ms |
28544 KB |
n=500 |
54 |
Correct |
19 ms |
28556 KB |
n=500 |
55 |
Correct |
19 ms |
28432 KB |
n=278 |
56 |
Correct |
19 ms |
28504 KB |
n=500 |
57 |
Correct |
22 ms |
28484 KB |
n=500 |
58 |
Correct |
27 ms |
28452 KB |
n=500 |
59 |
Correct |
23 ms |
28552 KB |
n=2000 |
60 |
Correct |
20 ms |
28740 KB |
n=2000 |
61 |
Correct |
22 ms |
28584 KB |
n=2000 |
62 |
Correct |
21 ms |
28588 KB |
n=2000 |
63 |
Correct |
20 ms |
28616 KB |
n=2000 |
64 |
Correct |
21 ms |
28548 KB |
n=2000 |
65 |
Correct |
30 ms |
28556 KB |
n=2000 |
66 |
Correct |
25 ms |
28620 KB |
n=2000 |
67 |
Correct |
20 ms |
28556 KB |
n=2000 |
68 |
Correct |
21 ms |
28648 KB |
n=2000 |
69 |
Correct |
22 ms |
28560 KB |
n=2000 |
70 |
Correct |
22 ms |
28632 KB |
n=2000 |
71 |
Correct |
21 ms |
28620 KB |
n=2000 |
72 |
Correct |
21 ms |
28572 KB |
n=2000 |
73 |
Correct |
21 ms |
28548 KB |
n=2000 |
74 |
Correct |
25 ms |
28620 KB |
n=1844 |
75 |
Correct |
22 ms |
28620 KB |
n=2000 |
76 |
Correct |
21 ms |
28596 KB |
n=2000 |
77 |
Correct |
21 ms |
28592 KB |
n=2000 |
78 |
Correct |
21 ms |
28620 KB |
n=2000 |
79 |
Correct |
21 ms |
28584 KB |
n=2000 |
80 |
Correct |
21 ms |
28708 KB |
n=2000 |
81 |
Correct |
21 ms |
28620 KB |
n=2000 |
82 |
Correct |
20 ms |
28616 KB |
n=2000 |
83 |
Correct |
22 ms |
28680 KB |
n=2000 |
84 |
Correct |
20 ms |
28572 KB |
n=2000 |
85 |
Correct |
21 ms |
28664 KB |
n=2000 |
86 |
Correct |
25 ms |
28640 KB |
n=2000 |
87 |
Correct |
28 ms |
28620 KB |
n=2000 |
88 |
Correct |
24 ms |
28724 KB |
n=2000 |
89 |
Correct |
22 ms |
28748 KB |
n=2000 |
90 |
Correct |
21 ms |
28724 KB |
n=2000 |
91 |
Correct |
21 ms |
28620 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28512 KB |
n=5 |
2 |
Correct |
21 ms |
28492 KB |
n=100 |
3 |
Correct |
19 ms |
28492 KB |
n=100 |
4 |
Correct |
23 ms |
28512 KB |
n=100 |
5 |
Correct |
21 ms |
28480 KB |
n=100 |
6 |
Correct |
24 ms |
28400 KB |
n=100 |
7 |
Correct |
20 ms |
28484 KB |
n=100 |
8 |
Correct |
19 ms |
28444 KB |
n=100 |
9 |
Correct |
19 ms |
28496 KB |
n=100 |
10 |
Correct |
20 ms |
28508 KB |
n=100 |
11 |
Correct |
25 ms |
28392 KB |
n=100 |
12 |
Correct |
20 ms |
28492 KB |
n=100 |
13 |
Correct |
23 ms |
28492 KB |
n=100 |
14 |
Correct |
20 ms |
28492 KB |
n=100 |
15 |
Correct |
19 ms |
28440 KB |
n=100 |
16 |
Correct |
19 ms |
28460 KB |
n=100 |
17 |
Correct |
23 ms |
28424 KB |
n=100 |
18 |
Correct |
19 ms |
28456 KB |
n=100 |
19 |
Correct |
22 ms |
28492 KB |
n=100 |
20 |
Correct |
20 ms |
28492 KB |
n=100 |
21 |
Correct |
23 ms |
28412 KB |
n=100 |
22 |
Correct |
20 ms |
28488 KB |
n=100 |
23 |
Correct |
20 ms |
28504 KB |
n=100 |
24 |
Correct |
20 ms |
28492 KB |
n=100 |
25 |
Correct |
19 ms |
28464 KB |
n=100 |
26 |
Correct |
18 ms |
28460 KB |
n=12 |
27 |
Correct |
19 ms |
28480 KB |
n=100 |
28 |
Correct |
20 ms |
28504 KB |
n=500 |
29 |
Correct |
20 ms |
28492 KB |
n=500 |
30 |
Correct |
20 ms |
28456 KB |
n=500 |
31 |
Correct |
20 ms |
28504 KB |
n=500 |
32 |
Correct |
20 ms |
28492 KB |
n=500 |
33 |
Correct |
22 ms |
28492 KB |
n=500 |
34 |
Correct |
20 ms |
28472 KB |
n=500 |
35 |
Correct |
19 ms |
28492 KB |
n=500 |
36 |
Correct |
19 ms |
28456 KB |
n=500 |
37 |
Correct |
19 ms |
28492 KB |
n=500 |
38 |
Correct |
19 ms |
28476 KB |
n=500 |
39 |
Correct |
20 ms |
28548 KB |
n=500 |
40 |
Correct |
22 ms |
28492 KB |
n=500 |
41 |
Correct |
19 ms |
28464 KB |
n=500 |
42 |
Correct |
19 ms |
28492 KB |
n=500 |
43 |
Correct |
19 ms |
28484 KB |
n=500 |
44 |
Correct |
19 ms |
28492 KB |
n=500 |
45 |
Correct |
20 ms |
28492 KB |
n=500 |
46 |
Correct |
20 ms |
28492 KB |
n=500 |
47 |
Correct |
20 ms |
28492 KB |
n=500 |
48 |
Correct |
20 ms |
28492 KB |
n=500 |
49 |
Correct |
27 ms |
28532 KB |
n=500 |
50 |
Correct |
21 ms |
28524 KB |
n=500 |
51 |
Correct |
19 ms |
28492 KB |
n=500 |
52 |
Correct |
20 ms |
28572 KB |
n=500 |
53 |
Correct |
20 ms |
28544 KB |
n=500 |
54 |
Correct |
19 ms |
28556 KB |
n=500 |
55 |
Correct |
19 ms |
28432 KB |
n=278 |
56 |
Correct |
19 ms |
28504 KB |
n=500 |
57 |
Correct |
22 ms |
28484 KB |
n=500 |
58 |
Correct |
27 ms |
28452 KB |
n=500 |
59 |
Correct |
23 ms |
28552 KB |
n=2000 |
60 |
Correct |
20 ms |
28740 KB |
n=2000 |
61 |
Correct |
22 ms |
28584 KB |
n=2000 |
62 |
Correct |
21 ms |
28588 KB |
n=2000 |
63 |
Correct |
20 ms |
28616 KB |
n=2000 |
64 |
Correct |
21 ms |
28548 KB |
n=2000 |
65 |
Correct |
30 ms |
28556 KB |
n=2000 |
66 |
Correct |
25 ms |
28620 KB |
n=2000 |
67 |
Correct |
20 ms |
28556 KB |
n=2000 |
68 |
Correct |
21 ms |
28648 KB |
n=2000 |
69 |
Correct |
22 ms |
28560 KB |
n=2000 |
70 |
Correct |
22 ms |
28632 KB |
n=2000 |
71 |
Correct |
21 ms |
28620 KB |
n=2000 |
72 |
Correct |
21 ms |
28572 KB |
n=2000 |
73 |
Correct |
21 ms |
28548 KB |
n=2000 |
74 |
Correct |
25 ms |
28620 KB |
n=1844 |
75 |
Correct |
22 ms |
28620 KB |
n=2000 |
76 |
Correct |
21 ms |
28596 KB |
n=2000 |
77 |
Correct |
21 ms |
28592 KB |
n=2000 |
78 |
Correct |
21 ms |
28620 KB |
n=2000 |
79 |
Correct |
21 ms |
28584 KB |
n=2000 |
80 |
Correct |
21 ms |
28708 KB |
n=2000 |
81 |
Correct |
21 ms |
28620 KB |
n=2000 |
82 |
Correct |
20 ms |
28616 KB |
n=2000 |
83 |
Correct |
22 ms |
28680 KB |
n=2000 |
84 |
Correct |
20 ms |
28572 KB |
n=2000 |
85 |
Correct |
21 ms |
28664 KB |
n=2000 |
86 |
Correct |
25 ms |
28640 KB |
n=2000 |
87 |
Correct |
28 ms |
28620 KB |
n=2000 |
88 |
Correct |
24 ms |
28724 KB |
n=2000 |
89 |
Correct |
22 ms |
28748 KB |
n=2000 |
90 |
Correct |
21 ms |
28724 KB |
n=2000 |
91 |
Correct |
21 ms |
28620 KB |
n=2000 |
92 |
Correct |
378 ms |
40748 KB |
n=200000 |
93 |
Correct |
2987 ms |
49588 KB |
n=200000 |
94 |
Correct |
3802 ms |
58188 KB |
n=200000 |
95 |
Correct |
331 ms |
46936 KB |
n=200000 |
96 |
Correct |
295 ms |
47048 KB |
n=200000 |
97 |
Correct |
2742 ms |
52516 KB |
n=200000 |
98 |
Correct |
339 ms |
46964 KB |
n=200000 |
99 |
Correct |
439 ms |
47152 KB |
n=200000 |
100 |
Correct |
295 ms |
47112 KB |
n=200000 |
101 |
Correct |
3543 ms |
59884 KB |
n=200000 |
102 |
Execution timed out |
4058 ms |
44596 KB |
Time limit exceeded |
103 |
Halted |
0 ms |
0 KB |
- |