#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m, q;
vector <int> adj[300005];
multiset <int> ms[300005];
int S[300005], E[300005], cnt = 1, par[20][200005], dep[200005];
void dfs(int x, int p, int d){
S[x] = cnt++;
par[0][x] = p;
dep[x] = d;
for(auto i : adj[x])if(i != p)dfs(i, x, d + 1);
E[x] = cnt - 1;
}
int lca(int u, int v){
if(dep[u] > dep[v])swap(u, v);
int df = dep[v] - dep[u];
for(int i=0;i<19;i++)if(df >> i & 1)v = par[i][v];
if(u == v)return u;
for(int i=19;i>=0;i--){
if(par[i][u] != par[i][v])u = par[i][u], v = par[i][v];
}
return par[0][u];
}
int back[300005];
struct node{
int s, e, m, mn, mx, mx2;
node *l, *r;
node(int _s, int _e){
s = _s, e = _e, m = (s + e) >> 1;
if(s != e)l = new node(s, m), r = new node(m+1, e);
mn = 1e9, mx = mx2 = 0;
}
void upd(int p, int v, int v2){
if(s == e)mn = mx2 = v, mx = v2;
else{
if(p <= m)l->upd(p, v, v2);
else r->upd(p, v, v2);
mn = min(l->mn, r->mn);
mx = max(l->mx, r->mx);
mx2 = max(l->mx2, r->mx2);
}
}
int q1(int a, int b){
if(a == s && b == e)return mn;
if(b <= m)return l->q1(a, b);
if(a > m)return r->q1(a, b);
return min(l->q1(a, m), r->q1(m+1, b));
}
int q2(int a, int b){
if(a == s && b == e)return mx;
if(b <= m)return l->q2(a, b);
if(a > m)return r->q2(a, b);
return max(l->q2(a, m), r->q2(m+1, b));
}
int q3(int a, int b){
if(a == s && b == e)return mx2;
if(b <= m)return l->q3(a, b);
if(a > m)return r->q3(a, b);
return max(l->q3(a, m), r->q3(m+1, b));
}
}*root;
int A[300005], f, v;
void dnc(int l, int r){
if(f || l > r)return;
int mid = (l + r) >> 1;
if(f)return;
int lo = mid + 1, hi = r, tmp = hi + 1;
while(lo <= hi){
int mm = (lo + hi) >> 1;
if(root->q1(mid + 1, mm) < S[v] || root->q2(mid + 1, mm) > E[v])tmp = mm, hi = mm - 1;
else lo = mm + 1;
}
tmp--;
lo = l, hi = mid;
int tmp2 = lo - 1;
while(lo <= hi){
int mm = (lo + hi) >> 1;
if(root->q1(mm, mid) < S[v] || root->q2(mm, mid) > E[v])tmp2 = mm, lo = mm + 1;
else hi = mm - 1;
}
tmp2++;
if(tmp2 <= tmp){
hi = root->q1(tmp2, tmp); int hi2 = root->q3(tmp2, tmp);
//cerr << l << ' ' << r << ' ' << hi << ' ' << hi2 << '\n';
if(lca(back[hi], back[hi2]) == v){
cout << tmp2 << ' ' << tmp << '\n';
f = 1;
return;
}
}
dnc(l, tmp2 - 2); dnc(tmp + 2, r);
}
void solve(){
cin >> n >> m >> q;
for(int i=1;i<n;i++){
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1, 1);
root = new node(1, m);
for(int i=1;i<=m;i++)cin >> A[i], root->upd(i, S[A[i]], E[A[i]]);
/*
root = new node(1, n);
for(int i=1;i<=n;i++){
if(!ms[i].empty()){
auto it = ms[i].begin(), it2 = --ms[i].end();
root->upd(S[i], *it, *it2);
}
}
*/
for(int i=1;i<=n;i++)back[S[i]] = i;
for(int i=1;i<=19;i++)for(int j=1;j<=n;j++)par[i][j] = par[i-1][par[i-1][j]];
cerr << "ok\n";
while(q--){
int t; cin >> t;
if(t == 1){
int x, y; cin >> x >> y;
root->upd(x, S[y], E[y]);
}
else{
int l, r; cin >> l >> r >> v;
f = 0;
dnc(l, r);
if(!f)cout << -1 << ' ' << -1 << '\n';
}
}
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
treearray.cpp:151:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
151 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
61784 KB |
n=5 |
2 |
Correct |
15 ms |
61532 KB |
n=100 |
3 |
Correct |
14 ms |
61532 KB |
n=100 |
4 |
Correct |
14 ms |
61528 KB |
n=100 |
5 |
Correct |
13 ms |
61532 KB |
n=100 |
6 |
Correct |
13 ms |
61532 KB |
n=100 |
7 |
Correct |
13 ms |
61532 KB |
n=100 |
8 |
Correct |
16 ms |
61528 KB |
n=100 |
9 |
Correct |
15 ms |
61532 KB |
n=100 |
10 |
Correct |
16 ms |
61532 KB |
n=100 |
11 |
Correct |
13 ms |
61528 KB |
n=100 |
12 |
Correct |
17 ms |
61528 KB |
n=100 |
13 |
Correct |
13 ms |
61528 KB |
n=100 |
14 |
Correct |
14 ms |
61532 KB |
n=100 |
15 |
Correct |
17 ms |
61532 KB |
n=100 |
16 |
Correct |
15 ms |
61532 KB |
n=100 |
17 |
Correct |
14 ms |
61532 KB |
n=100 |
18 |
Correct |
15 ms |
61528 KB |
n=100 |
19 |
Correct |
14 ms |
61528 KB |
n=100 |
20 |
Correct |
15 ms |
61532 KB |
n=100 |
21 |
Correct |
15 ms |
61644 KB |
n=100 |
22 |
Correct |
13 ms |
61532 KB |
n=100 |
23 |
Correct |
14 ms |
61532 KB |
n=100 |
24 |
Correct |
13 ms |
61528 KB |
n=100 |
25 |
Correct |
14 ms |
61648 KB |
n=100 |
26 |
Correct |
13 ms |
61532 KB |
n=12 |
27 |
Correct |
14 ms |
61652 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
61784 KB |
n=5 |
2 |
Correct |
15 ms |
61532 KB |
n=100 |
3 |
Correct |
14 ms |
61532 KB |
n=100 |
4 |
Correct |
14 ms |
61528 KB |
n=100 |
5 |
Correct |
13 ms |
61532 KB |
n=100 |
6 |
Correct |
13 ms |
61532 KB |
n=100 |
7 |
Correct |
13 ms |
61532 KB |
n=100 |
8 |
Correct |
16 ms |
61528 KB |
n=100 |
9 |
Correct |
15 ms |
61532 KB |
n=100 |
10 |
Correct |
16 ms |
61532 KB |
n=100 |
11 |
Correct |
13 ms |
61528 KB |
n=100 |
12 |
Correct |
17 ms |
61528 KB |
n=100 |
13 |
Correct |
13 ms |
61528 KB |
n=100 |
14 |
Correct |
14 ms |
61532 KB |
n=100 |
15 |
Correct |
17 ms |
61532 KB |
n=100 |
16 |
Correct |
15 ms |
61532 KB |
n=100 |
17 |
Correct |
14 ms |
61532 KB |
n=100 |
18 |
Correct |
15 ms |
61528 KB |
n=100 |
19 |
Correct |
14 ms |
61528 KB |
n=100 |
20 |
Correct |
15 ms |
61532 KB |
n=100 |
21 |
Correct |
15 ms |
61644 KB |
n=100 |
22 |
Correct |
13 ms |
61532 KB |
n=100 |
23 |
Correct |
14 ms |
61532 KB |
n=100 |
24 |
Correct |
13 ms |
61528 KB |
n=100 |
25 |
Correct |
14 ms |
61648 KB |
n=100 |
26 |
Correct |
13 ms |
61532 KB |
n=12 |
27 |
Correct |
14 ms |
61652 KB |
n=100 |
28 |
Correct |
15 ms |
61528 KB |
n=500 |
29 |
Correct |
17 ms |
61784 KB |
n=500 |
30 |
Correct |
16 ms |
61528 KB |
n=500 |
31 |
Correct |
16 ms |
61528 KB |
n=500 |
32 |
Correct |
17 ms |
61732 KB |
n=500 |
33 |
Correct |
19 ms |
61532 KB |
n=500 |
34 |
Correct |
15 ms |
61528 KB |
n=500 |
35 |
Correct |
17 ms |
61784 KB |
n=500 |
36 |
Correct |
15 ms |
61624 KB |
n=500 |
37 |
Correct |
14 ms |
61532 KB |
n=500 |
38 |
Correct |
14 ms |
61784 KB |
n=500 |
39 |
Correct |
25 ms |
61772 KB |
n=500 |
40 |
Correct |
30 ms |
61532 KB |
n=500 |
41 |
Correct |
33 ms |
61620 KB |
n=500 |
42 |
Correct |
14 ms |
61748 KB |
n=500 |
43 |
Correct |
14 ms |
61532 KB |
n=500 |
44 |
Correct |
14 ms |
61528 KB |
n=500 |
45 |
Correct |
14 ms |
61528 KB |
n=500 |
46 |
Correct |
16 ms |
61752 KB |
n=500 |
47 |
Correct |
16 ms |
61784 KB |
n=500 |
48 |
Correct |
14 ms |
61748 KB |
n=500 |
49 |
Correct |
17 ms |
61532 KB |
n=500 |
50 |
Correct |
20 ms |
61532 KB |
n=500 |
51 |
Correct |
18 ms |
61532 KB |
n=500 |
52 |
Correct |
19 ms |
61532 KB |
n=500 |
53 |
Correct |
19 ms |
61532 KB |
n=500 |
54 |
Correct |
14 ms |
61532 KB |
n=500 |
55 |
Correct |
14 ms |
61532 KB |
n=278 |
56 |
Correct |
14 ms |
61552 KB |
n=500 |
57 |
Correct |
13 ms |
61532 KB |
n=500 |
58 |
Correct |
22 ms |
61528 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
61784 KB |
n=5 |
2 |
Correct |
15 ms |
61532 KB |
n=100 |
3 |
Correct |
14 ms |
61532 KB |
n=100 |
4 |
Correct |
14 ms |
61528 KB |
n=100 |
5 |
Correct |
13 ms |
61532 KB |
n=100 |
6 |
Correct |
13 ms |
61532 KB |
n=100 |
7 |
Correct |
13 ms |
61532 KB |
n=100 |
8 |
Correct |
16 ms |
61528 KB |
n=100 |
9 |
Correct |
15 ms |
61532 KB |
n=100 |
10 |
Correct |
16 ms |
61532 KB |
n=100 |
11 |
Correct |
13 ms |
61528 KB |
n=100 |
12 |
Correct |
17 ms |
61528 KB |
n=100 |
13 |
Correct |
13 ms |
61528 KB |
n=100 |
14 |
Correct |
14 ms |
61532 KB |
n=100 |
15 |
Correct |
17 ms |
61532 KB |
n=100 |
16 |
Correct |
15 ms |
61532 KB |
n=100 |
17 |
Correct |
14 ms |
61532 KB |
n=100 |
18 |
Correct |
15 ms |
61528 KB |
n=100 |
19 |
Correct |
14 ms |
61528 KB |
n=100 |
20 |
Correct |
15 ms |
61532 KB |
n=100 |
21 |
Correct |
15 ms |
61644 KB |
n=100 |
22 |
Correct |
13 ms |
61532 KB |
n=100 |
23 |
Correct |
14 ms |
61532 KB |
n=100 |
24 |
Correct |
13 ms |
61528 KB |
n=100 |
25 |
Correct |
14 ms |
61648 KB |
n=100 |
26 |
Correct |
13 ms |
61532 KB |
n=12 |
27 |
Correct |
14 ms |
61652 KB |
n=100 |
28 |
Correct |
15 ms |
61528 KB |
n=500 |
29 |
Correct |
17 ms |
61784 KB |
n=500 |
30 |
Correct |
16 ms |
61528 KB |
n=500 |
31 |
Correct |
16 ms |
61528 KB |
n=500 |
32 |
Correct |
17 ms |
61732 KB |
n=500 |
33 |
Correct |
19 ms |
61532 KB |
n=500 |
34 |
Correct |
15 ms |
61528 KB |
n=500 |
35 |
Correct |
17 ms |
61784 KB |
n=500 |
36 |
Correct |
15 ms |
61624 KB |
n=500 |
37 |
Correct |
14 ms |
61532 KB |
n=500 |
38 |
Correct |
14 ms |
61784 KB |
n=500 |
39 |
Correct |
25 ms |
61772 KB |
n=500 |
40 |
Correct |
30 ms |
61532 KB |
n=500 |
41 |
Correct |
33 ms |
61620 KB |
n=500 |
42 |
Correct |
14 ms |
61748 KB |
n=500 |
43 |
Correct |
14 ms |
61532 KB |
n=500 |
44 |
Correct |
14 ms |
61528 KB |
n=500 |
45 |
Correct |
14 ms |
61528 KB |
n=500 |
46 |
Correct |
16 ms |
61752 KB |
n=500 |
47 |
Correct |
16 ms |
61784 KB |
n=500 |
48 |
Correct |
14 ms |
61748 KB |
n=500 |
49 |
Correct |
17 ms |
61532 KB |
n=500 |
50 |
Correct |
20 ms |
61532 KB |
n=500 |
51 |
Correct |
18 ms |
61532 KB |
n=500 |
52 |
Correct |
19 ms |
61532 KB |
n=500 |
53 |
Correct |
19 ms |
61532 KB |
n=500 |
54 |
Correct |
14 ms |
61532 KB |
n=500 |
55 |
Correct |
14 ms |
61532 KB |
n=278 |
56 |
Correct |
14 ms |
61552 KB |
n=500 |
57 |
Correct |
13 ms |
61532 KB |
n=500 |
58 |
Correct |
22 ms |
61528 KB |
n=500 |
59 |
Correct |
19 ms |
62060 KB |
n=2000 |
60 |
Correct |
59 ms |
62128 KB |
n=2000 |
61 |
Correct |
56 ms |
62100 KB |
n=2000 |
62 |
Correct |
42 ms |
62044 KB |
n=2000 |
63 |
Correct |
19 ms |
62044 KB |
n=2000 |
64 |
Correct |
54 ms |
62088 KB |
n=2000 |
65 |
Correct |
21 ms |
62300 KB |
n=2000 |
66 |
Correct |
62 ms |
62108 KB |
n=2000 |
67 |
Correct |
21 ms |
62040 KB |
n=2000 |
68 |
Correct |
54 ms |
62296 KB |
n=2000 |
69 |
Correct |
16 ms |
62044 KB |
n=2000 |
70 |
Correct |
15 ms |
62044 KB |
n=2000 |
71 |
Correct |
16 ms |
62040 KB |
n=2000 |
72 |
Correct |
214 ms |
62044 KB |
n=2000 |
73 |
Correct |
216 ms |
62060 KB |
n=2000 |
74 |
Correct |
18 ms |
62040 KB |
n=1844 |
75 |
Correct |
211 ms |
62044 KB |
n=2000 |
76 |
Correct |
16 ms |
62044 KB |
n=2000 |
77 |
Correct |
15 ms |
62040 KB |
n=2000 |
78 |
Correct |
15 ms |
62044 KB |
n=2000 |
79 |
Correct |
18 ms |
62040 KB |
n=2000 |
80 |
Correct |
55 ms |
62040 KB |
n=2000 |
81 |
Correct |
49 ms |
62044 KB |
n=2000 |
82 |
Correct |
18 ms |
62040 KB |
n=2000 |
83 |
Correct |
55 ms |
62044 KB |
n=2000 |
84 |
Correct |
112 ms |
62064 KB |
n=2000 |
85 |
Correct |
91 ms |
62100 KB |
n=2000 |
86 |
Correct |
100 ms |
62044 KB |
n=2000 |
87 |
Correct |
113 ms |
62064 KB |
n=2000 |
88 |
Correct |
19 ms |
62368 KB |
n=2000 |
89 |
Correct |
19 ms |
62040 KB |
n=2000 |
90 |
Correct |
17 ms |
62040 KB |
n=2000 |
91 |
Correct |
257 ms |
62068 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
61784 KB |
n=5 |
2 |
Correct |
15 ms |
61532 KB |
n=100 |
3 |
Correct |
14 ms |
61532 KB |
n=100 |
4 |
Correct |
14 ms |
61528 KB |
n=100 |
5 |
Correct |
13 ms |
61532 KB |
n=100 |
6 |
Correct |
13 ms |
61532 KB |
n=100 |
7 |
Correct |
13 ms |
61532 KB |
n=100 |
8 |
Correct |
16 ms |
61528 KB |
n=100 |
9 |
Correct |
15 ms |
61532 KB |
n=100 |
10 |
Correct |
16 ms |
61532 KB |
n=100 |
11 |
Correct |
13 ms |
61528 KB |
n=100 |
12 |
Correct |
17 ms |
61528 KB |
n=100 |
13 |
Correct |
13 ms |
61528 KB |
n=100 |
14 |
Correct |
14 ms |
61532 KB |
n=100 |
15 |
Correct |
17 ms |
61532 KB |
n=100 |
16 |
Correct |
15 ms |
61532 KB |
n=100 |
17 |
Correct |
14 ms |
61532 KB |
n=100 |
18 |
Correct |
15 ms |
61528 KB |
n=100 |
19 |
Correct |
14 ms |
61528 KB |
n=100 |
20 |
Correct |
15 ms |
61532 KB |
n=100 |
21 |
Correct |
15 ms |
61644 KB |
n=100 |
22 |
Correct |
13 ms |
61532 KB |
n=100 |
23 |
Correct |
14 ms |
61532 KB |
n=100 |
24 |
Correct |
13 ms |
61528 KB |
n=100 |
25 |
Correct |
14 ms |
61648 KB |
n=100 |
26 |
Correct |
13 ms |
61532 KB |
n=12 |
27 |
Correct |
14 ms |
61652 KB |
n=100 |
28 |
Correct |
15 ms |
61528 KB |
n=500 |
29 |
Correct |
17 ms |
61784 KB |
n=500 |
30 |
Correct |
16 ms |
61528 KB |
n=500 |
31 |
Correct |
16 ms |
61528 KB |
n=500 |
32 |
Correct |
17 ms |
61732 KB |
n=500 |
33 |
Correct |
19 ms |
61532 KB |
n=500 |
34 |
Correct |
15 ms |
61528 KB |
n=500 |
35 |
Correct |
17 ms |
61784 KB |
n=500 |
36 |
Correct |
15 ms |
61624 KB |
n=500 |
37 |
Correct |
14 ms |
61532 KB |
n=500 |
38 |
Correct |
14 ms |
61784 KB |
n=500 |
39 |
Correct |
25 ms |
61772 KB |
n=500 |
40 |
Correct |
30 ms |
61532 KB |
n=500 |
41 |
Correct |
33 ms |
61620 KB |
n=500 |
42 |
Correct |
14 ms |
61748 KB |
n=500 |
43 |
Correct |
14 ms |
61532 KB |
n=500 |
44 |
Correct |
14 ms |
61528 KB |
n=500 |
45 |
Correct |
14 ms |
61528 KB |
n=500 |
46 |
Correct |
16 ms |
61752 KB |
n=500 |
47 |
Correct |
16 ms |
61784 KB |
n=500 |
48 |
Correct |
14 ms |
61748 KB |
n=500 |
49 |
Correct |
17 ms |
61532 KB |
n=500 |
50 |
Correct |
20 ms |
61532 KB |
n=500 |
51 |
Correct |
18 ms |
61532 KB |
n=500 |
52 |
Correct |
19 ms |
61532 KB |
n=500 |
53 |
Correct |
19 ms |
61532 KB |
n=500 |
54 |
Correct |
14 ms |
61532 KB |
n=500 |
55 |
Correct |
14 ms |
61532 KB |
n=278 |
56 |
Correct |
14 ms |
61552 KB |
n=500 |
57 |
Correct |
13 ms |
61532 KB |
n=500 |
58 |
Correct |
22 ms |
61528 KB |
n=500 |
59 |
Correct |
19 ms |
62060 KB |
n=2000 |
60 |
Correct |
59 ms |
62128 KB |
n=2000 |
61 |
Correct |
56 ms |
62100 KB |
n=2000 |
62 |
Correct |
42 ms |
62044 KB |
n=2000 |
63 |
Correct |
19 ms |
62044 KB |
n=2000 |
64 |
Correct |
54 ms |
62088 KB |
n=2000 |
65 |
Correct |
21 ms |
62300 KB |
n=2000 |
66 |
Correct |
62 ms |
62108 KB |
n=2000 |
67 |
Correct |
21 ms |
62040 KB |
n=2000 |
68 |
Correct |
54 ms |
62296 KB |
n=2000 |
69 |
Correct |
16 ms |
62044 KB |
n=2000 |
70 |
Correct |
15 ms |
62044 KB |
n=2000 |
71 |
Correct |
16 ms |
62040 KB |
n=2000 |
72 |
Correct |
214 ms |
62044 KB |
n=2000 |
73 |
Correct |
216 ms |
62060 KB |
n=2000 |
74 |
Correct |
18 ms |
62040 KB |
n=1844 |
75 |
Correct |
211 ms |
62044 KB |
n=2000 |
76 |
Correct |
16 ms |
62044 KB |
n=2000 |
77 |
Correct |
15 ms |
62040 KB |
n=2000 |
78 |
Correct |
15 ms |
62044 KB |
n=2000 |
79 |
Correct |
18 ms |
62040 KB |
n=2000 |
80 |
Correct |
55 ms |
62040 KB |
n=2000 |
81 |
Correct |
49 ms |
62044 KB |
n=2000 |
82 |
Correct |
18 ms |
62040 KB |
n=2000 |
83 |
Correct |
55 ms |
62044 KB |
n=2000 |
84 |
Correct |
112 ms |
62064 KB |
n=2000 |
85 |
Correct |
91 ms |
62100 KB |
n=2000 |
86 |
Correct |
100 ms |
62044 KB |
n=2000 |
87 |
Correct |
113 ms |
62064 KB |
n=2000 |
88 |
Correct |
19 ms |
62368 KB |
n=2000 |
89 |
Correct |
19 ms |
62040 KB |
n=2000 |
90 |
Correct |
17 ms |
62040 KB |
n=2000 |
91 |
Correct |
257 ms |
62068 KB |
n=2000 |
92 |
Correct |
2466 ms |
104168 KB |
n=200000 |
93 |
Execution timed out |
4042 ms |
107092 KB |
Time limit exceeded |
94 |
Halted |
0 ms |
0 KB |
- |