#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iterator>
using namespace std;
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define no_answer {cout << "NO"; exit(0);}
#define FOR(i, start, finish, k) for(llong i = start; i <= finish; i += k)
const long long mxn = 2e5 + 110;
const long long mnn = 1e3 + 2;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
const long long OO = 1e9;
typedef long long llong;
typedef unsigned long long ullong;
int n, m, q, d[mxn][20], lvl[mxn], a[mxn], ans[mxn];
vector<int> g[mxn];
set<int> st[mxn], stpos[mxn];
void dfs(int x, int p){
lvl[x] = lvl[p] + 1;
d[x][0] = p;
for(int i = 1; i <= 18; i++){
d[x][i] = d[d[x][i - 1]][i - 1];
}
for(auto to : g[x]){
if(to != p)
dfs(to, x);
}
}
int lca(int u, int v){
if(lvl[u] < lvl[v]){
swap(u, v);
}
int dif = lvl[u] - lvl[v];
for(int j = 0; j <= 18; j++){
if((1 << j) & dif){
u = d[u][j];
}
}
if(u == v) return u;
for(int i = 18; i >= 0; i--){
if(d[u][i] != d[v][i]){
u = d[u][i];
v = d[v][i];
}
}
return d[u][0];
}
void replace_pair(int pos, int type, int x){
int anc = lca(a[pos], a[pos + type]);
st[anc].erase(min(pos + type, pos));
anc = lca(a[pos + type], x);
st[anc].insert(min(pos + type, pos));
}
int main() {
cin >> n >> m >> q;
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
for(int i = 1; i <= m; i++){
cin >> a[i];
}
for(int i = 1; i <= m; i++){
stpos[a[i]].insert(i);
if(i != m){
int u = i, v = i + 1;
int anc = lca(a[u], a[v]);
st[anc].insert(u);
}
}
for(int i = 1; i <= q; i++){
int t;
cin >> t;
if(t == 1){
int pos, b;
cin >> pos >> b;
if(pos != 1){
replace_pair(pos, -1, b);
}
if(pos != m){
replace_pair(pos, 1, b);
}
stpos[a[pos]].erase(pos);
stpos[b].insert(pos);
a[pos] = b;
}else{
int l, r, b;
cin >> l >> r >> b;
set<int> :: iterator it = stpos[b].lower_bound(l);
if(it != stpos[b].end() && l <= *it && *it <= r){
cout << *it << ' ' << *it << nl;
continue;
}
it = st[b].lower_bound(l);
if(it == st[b].end()) {
cout << -1 << ' ' << -1 << nl;
continue;
}
if(*it < l){
it++;
}
if(it == st[b].end()){
cout << -1 << ' ' << -1 << nl;
continue;
}
if(l <= *it && *it + 1 <= r){
cout << *it << ' ' << *it + 1 << nl;
}else{
cout << -1 << ' ' << -1 << nl;
}
}
}
}
/*
5 4 4
1 2
3 1
4 3
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23788 KB |
n=5 |
2 |
Correct |
15 ms |
23916 KB |
n=100 |
3 |
Correct |
15 ms |
23916 KB |
n=100 |
4 |
Correct |
15 ms |
23936 KB |
n=100 |
5 |
Correct |
14 ms |
23916 KB |
n=100 |
6 |
Correct |
15 ms |
23916 KB |
n=100 |
7 |
Correct |
15 ms |
23916 KB |
n=100 |
8 |
Correct |
15 ms |
23916 KB |
n=100 |
9 |
Correct |
15 ms |
23916 KB |
n=100 |
10 |
Correct |
14 ms |
23916 KB |
n=100 |
11 |
Correct |
14 ms |
23916 KB |
n=100 |
12 |
Correct |
15 ms |
23916 KB |
n=100 |
13 |
Correct |
14 ms |
23916 KB |
n=100 |
14 |
Correct |
15 ms |
23916 KB |
n=100 |
15 |
Correct |
17 ms |
23916 KB |
n=100 |
16 |
Correct |
15 ms |
23916 KB |
n=100 |
17 |
Correct |
14 ms |
24044 KB |
n=100 |
18 |
Correct |
15 ms |
23916 KB |
n=100 |
19 |
Correct |
15 ms |
23916 KB |
n=100 |
20 |
Correct |
16 ms |
23916 KB |
n=100 |
21 |
Correct |
14 ms |
23916 KB |
n=100 |
22 |
Correct |
14 ms |
23916 KB |
n=100 |
23 |
Correct |
15 ms |
23916 KB |
n=100 |
24 |
Correct |
14 ms |
23916 KB |
n=100 |
25 |
Correct |
14 ms |
23916 KB |
n=100 |
26 |
Correct |
14 ms |
23788 KB |
n=12 |
27 |
Correct |
15 ms |
23916 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23788 KB |
n=5 |
2 |
Correct |
15 ms |
23916 KB |
n=100 |
3 |
Correct |
15 ms |
23916 KB |
n=100 |
4 |
Correct |
15 ms |
23936 KB |
n=100 |
5 |
Correct |
14 ms |
23916 KB |
n=100 |
6 |
Correct |
15 ms |
23916 KB |
n=100 |
7 |
Correct |
15 ms |
23916 KB |
n=100 |
8 |
Correct |
15 ms |
23916 KB |
n=100 |
9 |
Correct |
15 ms |
23916 KB |
n=100 |
10 |
Correct |
14 ms |
23916 KB |
n=100 |
11 |
Correct |
14 ms |
23916 KB |
n=100 |
12 |
Correct |
15 ms |
23916 KB |
n=100 |
13 |
Correct |
14 ms |
23916 KB |
n=100 |
14 |
Correct |
15 ms |
23916 KB |
n=100 |
15 |
Correct |
17 ms |
23916 KB |
n=100 |
16 |
Correct |
15 ms |
23916 KB |
n=100 |
17 |
Correct |
14 ms |
24044 KB |
n=100 |
18 |
Correct |
15 ms |
23916 KB |
n=100 |
19 |
Correct |
15 ms |
23916 KB |
n=100 |
20 |
Correct |
16 ms |
23916 KB |
n=100 |
21 |
Correct |
14 ms |
23916 KB |
n=100 |
22 |
Correct |
14 ms |
23916 KB |
n=100 |
23 |
Correct |
15 ms |
23916 KB |
n=100 |
24 |
Correct |
14 ms |
23916 KB |
n=100 |
25 |
Correct |
14 ms |
23916 KB |
n=100 |
26 |
Correct |
14 ms |
23788 KB |
n=12 |
27 |
Correct |
15 ms |
23916 KB |
n=100 |
28 |
Correct |
16 ms |
23916 KB |
n=500 |
29 |
Correct |
16 ms |
23916 KB |
n=500 |
30 |
Correct |
17 ms |
23916 KB |
n=500 |
31 |
Correct |
16 ms |
23916 KB |
n=500 |
32 |
Correct |
16 ms |
23916 KB |
n=500 |
33 |
Correct |
19 ms |
23916 KB |
n=500 |
34 |
Correct |
18 ms |
24048 KB |
n=500 |
35 |
Correct |
16 ms |
23916 KB |
n=500 |
36 |
Correct |
17 ms |
23916 KB |
n=500 |
37 |
Correct |
16 ms |
23916 KB |
n=500 |
38 |
Correct |
17 ms |
23916 KB |
n=500 |
39 |
Correct |
16 ms |
23916 KB |
n=500 |
40 |
Correct |
16 ms |
23916 KB |
n=500 |
41 |
Correct |
16 ms |
23936 KB |
n=500 |
42 |
Correct |
16 ms |
23928 KB |
n=500 |
43 |
Correct |
17 ms |
23916 KB |
n=500 |
44 |
Correct |
16 ms |
23916 KB |
n=500 |
45 |
Correct |
16 ms |
23916 KB |
n=500 |
46 |
Correct |
17 ms |
23916 KB |
n=500 |
47 |
Correct |
16 ms |
23916 KB |
n=500 |
48 |
Correct |
16 ms |
23916 KB |
n=500 |
49 |
Correct |
16 ms |
23916 KB |
n=500 |
50 |
Correct |
16 ms |
23916 KB |
n=500 |
51 |
Correct |
16 ms |
23916 KB |
n=500 |
52 |
Correct |
17 ms |
23916 KB |
n=500 |
53 |
Correct |
16 ms |
23916 KB |
n=500 |
54 |
Correct |
16 ms |
23916 KB |
n=500 |
55 |
Correct |
15 ms |
23916 KB |
n=278 |
56 |
Correct |
16 ms |
23916 KB |
n=500 |
57 |
Correct |
17 ms |
23916 KB |
n=500 |
58 |
Correct |
17 ms |
23936 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23788 KB |
n=5 |
2 |
Correct |
15 ms |
23916 KB |
n=100 |
3 |
Correct |
15 ms |
23916 KB |
n=100 |
4 |
Correct |
15 ms |
23936 KB |
n=100 |
5 |
Correct |
14 ms |
23916 KB |
n=100 |
6 |
Correct |
15 ms |
23916 KB |
n=100 |
7 |
Correct |
15 ms |
23916 KB |
n=100 |
8 |
Correct |
15 ms |
23916 KB |
n=100 |
9 |
Correct |
15 ms |
23916 KB |
n=100 |
10 |
Correct |
14 ms |
23916 KB |
n=100 |
11 |
Correct |
14 ms |
23916 KB |
n=100 |
12 |
Correct |
15 ms |
23916 KB |
n=100 |
13 |
Correct |
14 ms |
23916 KB |
n=100 |
14 |
Correct |
15 ms |
23916 KB |
n=100 |
15 |
Correct |
17 ms |
23916 KB |
n=100 |
16 |
Correct |
15 ms |
23916 KB |
n=100 |
17 |
Correct |
14 ms |
24044 KB |
n=100 |
18 |
Correct |
15 ms |
23916 KB |
n=100 |
19 |
Correct |
15 ms |
23916 KB |
n=100 |
20 |
Correct |
16 ms |
23916 KB |
n=100 |
21 |
Correct |
14 ms |
23916 KB |
n=100 |
22 |
Correct |
14 ms |
23916 KB |
n=100 |
23 |
Correct |
15 ms |
23916 KB |
n=100 |
24 |
Correct |
14 ms |
23916 KB |
n=100 |
25 |
Correct |
14 ms |
23916 KB |
n=100 |
26 |
Correct |
14 ms |
23788 KB |
n=12 |
27 |
Correct |
15 ms |
23916 KB |
n=100 |
28 |
Correct |
16 ms |
23916 KB |
n=500 |
29 |
Correct |
16 ms |
23916 KB |
n=500 |
30 |
Correct |
17 ms |
23916 KB |
n=500 |
31 |
Correct |
16 ms |
23916 KB |
n=500 |
32 |
Correct |
16 ms |
23916 KB |
n=500 |
33 |
Correct |
19 ms |
23916 KB |
n=500 |
34 |
Correct |
18 ms |
24048 KB |
n=500 |
35 |
Correct |
16 ms |
23916 KB |
n=500 |
36 |
Correct |
17 ms |
23916 KB |
n=500 |
37 |
Correct |
16 ms |
23916 KB |
n=500 |
38 |
Correct |
17 ms |
23916 KB |
n=500 |
39 |
Correct |
16 ms |
23916 KB |
n=500 |
40 |
Correct |
16 ms |
23916 KB |
n=500 |
41 |
Correct |
16 ms |
23936 KB |
n=500 |
42 |
Correct |
16 ms |
23928 KB |
n=500 |
43 |
Correct |
17 ms |
23916 KB |
n=500 |
44 |
Correct |
16 ms |
23916 KB |
n=500 |
45 |
Correct |
16 ms |
23916 KB |
n=500 |
46 |
Correct |
17 ms |
23916 KB |
n=500 |
47 |
Correct |
16 ms |
23916 KB |
n=500 |
48 |
Correct |
16 ms |
23916 KB |
n=500 |
49 |
Correct |
16 ms |
23916 KB |
n=500 |
50 |
Correct |
16 ms |
23916 KB |
n=500 |
51 |
Correct |
16 ms |
23916 KB |
n=500 |
52 |
Correct |
17 ms |
23916 KB |
n=500 |
53 |
Correct |
16 ms |
23916 KB |
n=500 |
54 |
Correct |
16 ms |
23916 KB |
n=500 |
55 |
Correct |
15 ms |
23916 KB |
n=278 |
56 |
Correct |
16 ms |
23916 KB |
n=500 |
57 |
Correct |
17 ms |
23916 KB |
n=500 |
58 |
Correct |
17 ms |
23936 KB |
n=500 |
59 |
Correct |
22 ms |
24300 KB |
n=2000 |
60 |
Correct |
22 ms |
24300 KB |
n=2000 |
61 |
Correct |
22 ms |
24300 KB |
n=2000 |
62 |
Correct |
23 ms |
24300 KB |
n=2000 |
63 |
Correct |
22 ms |
24300 KB |
n=2000 |
64 |
Correct |
24 ms |
24300 KB |
n=2000 |
65 |
Correct |
22 ms |
24300 KB |
n=2000 |
66 |
Correct |
22 ms |
24300 KB |
n=2000 |
67 |
Correct |
23 ms |
24300 KB |
n=2000 |
68 |
Correct |
22 ms |
24320 KB |
n=2000 |
69 |
Correct |
23 ms |
24300 KB |
n=2000 |
70 |
Correct |
23 ms |
24300 KB |
n=2000 |
71 |
Correct |
24 ms |
24300 KB |
n=2000 |
72 |
Correct |
24 ms |
24300 KB |
n=2000 |
73 |
Correct |
23 ms |
24300 KB |
n=2000 |
74 |
Correct |
21 ms |
24300 KB |
n=1844 |
75 |
Correct |
23 ms |
24300 KB |
n=2000 |
76 |
Correct |
23 ms |
24300 KB |
n=2000 |
77 |
Correct |
22 ms |
24300 KB |
n=2000 |
78 |
Correct |
23 ms |
24300 KB |
n=2000 |
79 |
Correct |
22 ms |
24300 KB |
n=2000 |
80 |
Correct |
22 ms |
24300 KB |
n=2000 |
81 |
Correct |
22 ms |
24300 KB |
n=2000 |
82 |
Correct |
24 ms |
24428 KB |
n=2000 |
83 |
Correct |
22 ms |
24300 KB |
n=2000 |
84 |
Correct |
22 ms |
24300 KB |
n=2000 |
85 |
Correct |
22 ms |
24300 KB |
n=2000 |
86 |
Correct |
23 ms |
24300 KB |
n=2000 |
87 |
Correct |
22 ms |
24428 KB |
n=2000 |
88 |
Correct |
22 ms |
24300 KB |
n=2000 |
89 |
Correct |
22 ms |
24428 KB |
n=2000 |
90 |
Correct |
22 ms |
24448 KB |
n=2000 |
91 |
Correct |
24 ms |
24300 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23788 KB |
n=5 |
2 |
Correct |
15 ms |
23916 KB |
n=100 |
3 |
Correct |
15 ms |
23916 KB |
n=100 |
4 |
Correct |
15 ms |
23936 KB |
n=100 |
5 |
Correct |
14 ms |
23916 KB |
n=100 |
6 |
Correct |
15 ms |
23916 KB |
n=100 |
7 |
Correct |
15 ms |
23916 KB |
n=100 |
8 |
Correct |
15 ms |
23916 KB |
n=100 |
9 |
Correct |
15 ms |
23916 KB |
n=100 |
10 |
Correct |
14 ms |
23916 KB |
n=100 |
11 |
Correct |
14 ms |
23916 KB |
n=100 |
12 |
Correct |
15 ms |
23916 KB |
n=100 |
13 |
Correct |
14 ms |
23916 KB |
n=100 |
14 |
Correct |
15 ms |
23916 KB |
n=100 |
15 |
Correct |
17 ms |
23916 KB |
n=100 |
16 |
Correct |
15 ms |
23916 KB |
n=100 |
17 |
Correct |
14 ms |
24044 KB |
n=100 |
18 |
Correct |
15 ms |
23916 KB |
n=100 |
19 |
Correct |
15 ms |
23916 KB |
n=100 |
20 |
Correct |
16 ms |
23916 KB |
n=100 |
21 |
Correct |
14 ms |
23916 KB |
n=100 |
22 |
Correct |
14 ms |
23916 KB |
n=100 |
23 |
Correct |
15 ms |
23916 KB |
n=100 |
24 |
Correct |
14 ms |
23916 KB |
n=100 |
25 |
Correct |
14 ms |
23916 KB |
n=100 |
26 |
Correct |
14 ms |
23788 KB |
n=12 |
27 |
Correct |
15 ms |
23916 KB |
n=100 |
28 |
Correct |
16 ms |
23916 KB |
n=500 |
29 |
Correct |
16 ms |
23916 KB |
n=500 |
30 |
Correct |
17 ms |
23916 KB |
n=500 |
31 |
Correct |
16 ms |
23916 KB |
n=500 |
32 |
Correct |
16 ms |
23916 KB |
n=500 |
33 |
Correct |
19 ms |
23916 KB |
n=500 |
34 |
Correct |
18 ms |
24048 KB |
n=500 |
35 |
Correct |
16 ms |
23916 KB |
n=500 |
36 |
Correct |
17 ms |
23916 KB |
n=500 |
37 |
Correct |
16 ms |
23916 KB |
n=500 |
38 |
Correct |
17 ms |
23916 KB |
n=500 |
39 |
Correct |
16 ms |
23916 KB |
n=500 |
40 |
Correct |
16 ms |
23916 KB |
n=500 |
41 |
Correct |
16 ms |
23936 KB |
n=500 |
42 |
Correct |
16 ms |
23928 KB |
n=500 |
43 |
Correct |
17 ms |
23916 KB |
n=500 |
44 |
Correct |
16 ms |
23916 KB |
n=500 |
45 |
Correct |
16 ms |
23916 KB |
n=500 |
46 |
Correct |
17 ms |
23916 KB |
n=500 |
47 |
Correct |
16 ms |
23916 KB |
n=500 |
48 |
Correct |
16 ms |
23916 KB |
n=500 |
49 |
Correct |
16 ms |
23916 KB |
n=500 |
50 |
Correct |
16 ms |
23916 KB |
n=500 |
51 |
Correct |
16 ms |
23916 KB |
n=500 |
52 |
Correct |
17 ms |
23916 KB |
n=500 |
53 |
Correct |
16 ms |
23916 KB |
n=500 |
54 |
Correct |
16 ms |
23916 KB |
n=500 |
55 |
Correct |
15 ms |
23916 KB |
n=278 |
56 |
Correct |
16 ms |
23916 KB |
n=500 |
57 |
Correct |
17 ms |
23916 KB |
n=500 |
58 |
Correct |
17 ms |
23936 KB |
n=500 |
59 |
Correct |
22 ms |
24300 KB |
n=2000 |
60 |
Correct |
22 ms |
24300 KB |
n=2000 |
61 |
Correct |
22 ms |
24300 KB |
n=2000 |
62 |
Correct |
23 ms |
24300 KB |
n=2000 |
63 |
Correct |
22 ms |
24300 KB |
n=2000 |
64 |
Correct |
24 ms |
24300 KB |
n=2000 |
65 |
Correct |
22 ms |
24300 KB |
n=2000 |
66 |
Correct |
22 ms |
24300 KB |
n=2000 |
67 |
Correct |
23 ms |
24300 KB |
n=2000 |
68 |
Correct |
22 ms |
24320 KB |
n=2000 |
69 |
Correct |
23 ms |
24300 KB |
n=2000 |
70 |
Correct |
23 ms |
24300 KB |
n=2000 |
71 |
Correct |
24 ms |
24300 KB |
n=2000 |
72 |
Correct |
24 ms |
24300 KB |
n=2000 |
73 |
Correct |
23 ms |
24300 KB |
n=2000 |
74 |
Correct |
21 ms |
24300 KB |
n=1844 |
75 |
Correct |
23 ms |
24300 KB |
n=2000 |
76 |
Correct |
23 ms |
24300 KB |
n=2000 |
77 |
Correct |
22 ms |
24300 KB |
n=2000 |
78 |
Correct |
23 ms |
24300 KB |
n=2000 |
79 |
Correct |
22 ms |
24300 KB |
n=2000 |
80 |
Correct |
22 ms |
24300 KB |
n=2000 |
81 |
Correct |
22 ms |
24300 KB |
n=2000 |
82 |
Correct |
24 ms |
24428 KB |
n=2000 |
83 |
Correct |
22 ms |
24300 KB |
n=2000 |
84 |
Correct |
22 ms |
24300 KB |
n=2000 |
85 |
Correct |
22 ms |
24300 KB |
n=2000 |
86 |
Correct |
23 ms |
24300 KB |
n=2000 |
87 |
Correct |
22 ms |
24428 KB |
n=2000 |
88 |
Correct |
22 ms |
24300 KB |
n=2000 |
89 |
Correct |
22 ms |
24428 KB |
n=2000 |
90 |
Correct |
22 ms |
24448 KB |
n=2000 |
91 |
Correct |
24 ms |
24300 KB |
n=2000 |
92 |
Correct |
1345 ms |
71876 KB |
n=200000 |
93 |
Correct |
1622 ms |
75372 KB |
n=200000 |
94 |
Correct |
1584 ms |
78828 KB |
n=200000 |
95 |
Correct |
1301 ms |
71904 KB |
n=200000 |
96 |
Correct |
1252 ms |
71516 KB |
n=200000 |
97 |
Correct |
1646 ms |
74604 KB |
n=200000 |
98 |
Correct |
1298 ms |
71520 KB |
n=200000 |
99 |
Correct |
1542 ms |
71008 KB |
n=200000 |
100 |
Correct |
1299 ms |
71652 KB |
n=200000 |
101 |
Correct |
1554 ms |
79840 KB |
n=200000 |
102 |
Correct |
1193 ms |
72684 KB |
n=200000 |
103 |
Correct |
1187 ms |
72556 KB |
n=200000 |
104 |
Correct |
1188 ms |
72556 KB |
n=200000 |
105 |
Correct |
1216 ms |
72140 KB |
n=200000 |
106 |
Correct |
1228 ms |
72312 KB |
n=200000 |
107 |
Correct |
1219 ms |
72196 KB |
n=200000 |
108 |
Correct |
1415 ms |
71276 KB |
n=200000 |
109 |
Correct |
1404 ms |
71320 KB |
n=200000 |
110 |
Correct |
1432 ms |
71376 KB |
n=200000 |
111 |
Correct |
1305 ms |
71776 KB |
n=200000 |
112 |
Correct |
1574 ms |
79140 KB |
n=200000 |
113 |
Correct |
1619 ms |
74476 KB |
n=200000 |
114 |
Correct |
1302 ms |
71724 KB |
n=200000 |
115 |
Correct |
1730 ms |
72684 KB |
n=200000 |
116 |
Correct |
1264 ms |
70752 KB |
n=200000 |
117 |
Correct |
1542 ms |
79596 KB |
n=200000 |
118 |
Correct |
1658 ms |
73360 KB |
n=200000 |
119 |
Correct |
1249 ms |
71520 KB |
n=200000 |
120 |
Correct |
1473 ms |
79724 KB |
n=200000 |
121 |
Correct |
1483 ms |
79980 KB |
n=200000 |
122 |
Correct |
1480 ms |
80216 KB |
n=200000 |
123 |
Correct |
1241 ms |
71788 KB |
n=200000 |
124 |
Correct |
414 ms |
38892 KB |
n=25264 |