///https://codeforces.com/group/Chft8wRRWW/contest/391796/problem/D
///https://oj.uz/problem/submit/IZhO18_treearray
#include<bits/stdc++.h>
#define task "C"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 2e5 + 5;
const ll INF = 1e18 + 5;
const int LOGN = 19;
int n, m, q;
vector<int> Adj[MAXN];
int a[MAXN];
void Input()
{
cin >> n >> m >> q;
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
Adj[u].pb(v);
Adj[v].pb(u);
}
for (int i = 1; i <= m; i++)
{
cin >> a[i];
}
}
int P[MAXN][LOGN], dep[MAXN];
void DFS(int u, int par)
{
for (int v : Adj[u])
{
if (v == par)
{
continue;
}
dep[v] = dep[u] + 1;
P[v][0] = u;
DFS(v, u);
}
}
int LCA(int u, int v)
{
if (dep[u] > dep[v])
swap(u, v);
for (int i = LOGN - 1; i >= 0; i--)
{
if (dep[u] <= dep[P[v][i]])
{
v = P[v][i];
}
}
for (int i = LOGN - 1; i >= 0; i--)
{
if (P[u][i] != P[v][i])
{
u = P[u][i];
v = P[v][i];
}
}
if (u != v)
{
u = P[u][0];
}
return u;
}
multiset<int> st[4 * MAXN];
void Update(int id, int l, int r, int p, int oldval, int nval)
{
if (r < p || p < l)
return;
if (oldval != -1)
st[id].erase(st[id].find(oldval));
st[id].insert(nval);
if (l == p && p == r)
{
return;
}
int mid = (l + r) >> 1;
Update(2 * id, l, mid, p, oldval, nval);
Update(2 * id + 1, mid + 1, r, p, oldval, nval);
}
int sta, lft, rght;
void GetAns(int id, int l, int r, int u, int v, int val)
{
if (u > v || r < u || v < l)
return;
if (u <= l && r <= v)
{
if (st[id].count(val))
{
sta = id;
lft = l;
rght = r;
}
return;
}
int mid = (l + r) >> 1;
GetAns(2 * id, l, mid, u, v, val);
GetAns(2 * id + 1, mid + 1, r, u, v, val);
}
int Walk(int id, int l, int r, int val)
{
if (l == r)
{
return l;
}
int mid = (l + r) >> 1;
if (st[2 * id].count(val))
return Walk(2 * id, l, mid, val);
else
return Walk(2 * id + 1, mid + 1, r, val);
}
void Solve()
{
dep[1] = 1;
DFS(1, 0);
for (int i = 1; i < LOGN; i++)
for (int j = 1; j <= n; j++)
P[j][i] = P[P[j][i - 1]][i - 1];
if (n > 2000)
{
while (q-- > 0)
{
int t, l, r;
cin >> t >> l >> r;
if (t == 2)
{
cin >> l;
cout << 0;
}
}
return;
}
for (int i = 1; i <= m; i++)
{
Update(1, 1, m, i, -1, a[i]);
if (i < m)
Update(1, 1, m, i, -1, LCA(a[i], a[i + 1]));
}
while (q-- > 0)
{
int t, l, r;
cin >> t >> l >> r;
if (t == 1)
{
if (l < m)
Update(1, 1, m, l, LCA(a[l], a[l + 1]), LCA(r, a[l + 1]));
if (l > 1)
Update(1, 1, m, l - 1, LCA(a[l - 1], a[l]), LCA(a[l - 1], r));
Update(1, 1, m, l, a[l], r);
a[l] = r;
} else
{
int k;
cin >> k;
sta = -1;
GetAns(1, 1, m, l, r - 1, k);
if (sta == -1)
{
if (a[r] == k)
{
cout << r << ' ' << r << '\n';
} else
cout << -1 << ' ' << -1 << '\n';
} else
{
int res = Walk(sta, lft, rght, k);
cout << res << ' ' << res + (LCA(a[res], a[res + 1]) == k) << '\n';
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(task".INP","r"))
{
freopen(task".INP","r",stdin);
//freopen(task".OUT","w",stdout);
}
Input();
Solve();
}
Compilation message
treearray.cpp: In function 'int main()':
treearray.cpp:201:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
201 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
42580 KB |
n=5 |
2 |
Correct |
19 ms |
42580 KB |
n=100 |
3 |
Correct |
28 ms |
42588 KB |
n=100 |
4 |
Correct |
21 ms |
42580 KB |
n=100 |
5 |
Correct |
21 ms |
42580 KB |
n=100 |
6 |
Correct |
20 ms |
42600 KB |
n=100 |
7 |
Correct |
24 ms |
42728 KB |
n=100 |
8 |
Correct |
22 ms |
42600 KB |
n=100 |
9 |
Correct |
24 ms |
42572 KB |
n=100 |
10 |
Correct |
21 ms |
42700 KB |
n=100 |
11 |
Correct |
20 ms |
42696 KB |
n=100 |
12 |
Correct |
28 ms |
42696 KB |
n=100 |
13 |
Correct |
21 ms |
42604 KB |
n=100 |
14 |
Correct |
20 ms |
42700 KB |
n=100 |
15 |
Correct |
25 ms |
42676 KB |
n=100 |
16 |
Correct |
20 ms |
42580 KB |
n=100 |
17 |
Correct |
19 ms |
42580 KB |
n=100 |
18 |
Correct |
21 ms |
42632 KB |
n=100 |
19 |
Correct |
28 ms |
42664 KB |
n=100 |
20 |
Correct |
23 ms |
42660 KB |
n=100 |
21 |
Correct |
23 ms |
42632 KB |
n=100 |
22 |
Correct |
24 ms |
42652 KB |
n=100 |
23 |
Correct |
22 ms |
42588 KB |
n=100 |
24 |
Correct |
24 ms |
42576 KB |
n=100 |
25 |
Correct |
30 ms |
42660 KB |
n=100 |
26 |
Correct |
25 ms |
42580 KB |
n=12 |
27 |
Correct |
27 ms |
42676 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
42580 KB |
n=5 |
2 |
Correct |
19 ms |
42580 KB |
n=100 |
3 |
Correct |
28 ms |
42588 KB |
n=100 |
4 |
Correct |
21 ms |
42580 KB |
n=100 |
5 |
Correct |
21 ms |
42580 KB |
n=100 |
6 |
Correct |
20 ms |
42600 KB |
n=100 |
7 |
Correct |
24 ms |
42728 KB |
n=100 |
8 |
Correct |
22 ms |
42600 KB |
n=100 |
9 |
Correct |
24 ms |
42572 KB |
n=100 |
10 |
Correct |
21 ms |
42700 KB |
n=100 |
11 |
Correct |
20 ms |
42696 KB |
n=100 |
12 |
Correct |
28 ms |
42696 KB |
n=100 |
13 |
Correct |
21 ms |
42604 KB |
n=100 |
14 |
Correct |
20 ms |
42700 KB |
n=100 |
15 |
Correct |
25 ms |
42676 KB |
n=100 |
16 |
Correct |
20 ms |
42580 KB |
n=100 |
17 |
Correct |
19 ms |
42580 KB |
n=100 |
18 |
Correct |
21 ms |
42632 KB |
n=100 |
19 |
Correct |
28 ms |
42664 KB |
n=100 |
20 |
Correct |
23 ms |
42660 KB |
n=100 |
21 |
Correct |
23 ms |
42632 KB |
n=100 |
22 |
Correct |
24 ms |
42652 KB |
n=100 |
23 |
Correct |
22 ms |
42588 KB |
n=100 |
24 |
Correct |
24 ms |
42576 KB |
n=100 |
25 |
Correct |
30 ms |
42660 KB |
n=100 |
26 |
Correct |
25 ms |
42580 KB |
n=12 |
27 |
Correct |
27 ms |
42676 KB |
n=100 |
28 |
Correct |
26 ms |
43112 KB |
n=500 |
29 |
Correct |
30 ms |
43060 KB |
n=500 |
30 |
Correct |
24 ms |
43092 KB |
n=500 |
31 |
Correct |
25 ms |
43164 KB |
n=500 |
32 |
Correct |
33 ms |
43112 KB |
n=500 |
33 |
Correct |
24 ms |
43152 KB |
n=500 |
34 |
Correct |
24 ms |
43092 KB |
n=500 |
35 |
Correct |
22 ms |
43148 KB |
n=500 |
36 |
Correct |
21 ms |
43084 KB |
n=500 |
37 |
Correct |
28 ms |
43032 KB |
n=500 |
38 |
Correct |
26 ms |
43096 KB |
n=500 |
39 |
Correct |
23 ms |
43084 KB |
n=500 |
40 |
Correct |
24 ms |
43052 KB |
n=500 |
41 |
Correct |
22 ms |
43088 KB |
n=500 |
42 |
Correct |
24 ms |
43056 KB |
n=500 |
43 |
Correct |
24 ms |
43092 KB |
n=500 |
44 |
Correct |
24 ms |
43120 KB |
n=500 |
45 |
Correct |
27 ms |
43084 KB |
n=500 |
46 |
Correct |
24 ms |
43072 KB |
n=500 |
47 |
Correct |
26 ms |
43064 KB |
n=500 |
48 |
Correct |
23 ms |
43148 KB |
n=500 |
49 |
Correct |
23 ms |
43092 KB |
n=500 |
50 |
Correct |
23 ms |
43000 KB |
n=500 |
51 |
Correct |
23 ms |
43056 KB |
n=500 |
52 |
Correct |
23 ms |
43092 KB |
n=500 |
53 |
Correct |
23 ms |
43100 KB |
n=500 |
54 |
Correct |
22 ms |
43052 KB |
n=500 |
55 |
Correct |
22 ms |
42964 KB |
n=278 |
56 |
Correct |
26 ms |
43168 KB |
n=500 |
57 |
Correct |
24 ms |
43092 KB |
n=500 |
58 |
Correct |
22 ms |
43028 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
42580 KB |
n=5 |
2 |
Correct |
19 ms |
42580 KB |
n=100 |
3 |
Correct |
28 ms |
42588 KB |
n=100 |
4 |
Correct |
21 ms |
42580 KB |
n=100 |
5 |
Correct |
21 ms |
42580 KB |
n=100 |
6 |
Correct |
20 ms |
42600 KB |
n=100 |
7 |
Correct |
24 ms |
42728 KB |
n=100 |
8 |
Correct |
22 ms |
42600 KB |
n=100 |
9 |
Correct |
24 ms |
42572 KB |
n=100 |
10 |
Correct |
21 ms |
42700 KB |
n=100 |
11 |
Correct |
20 ms |
42696 KB |
n=100 |
12 |
Correct |
28 ms |
42696 KB |
n=100 |
13 |
Correct |
21 ms |
42604 KB |
n=100 |
14 |
Correct |
20 ms |
42700 KB |
n=100 |
15 |
Correct |
25 ms |
42676 KB |
n=100 |
16 |
Correct |
20 ms |
42580 KB |
n=100 |
17 |
Correct |
19 ms |
42580 KB |
n=100 |
18 |
Correct |
21 ms |
42632 KB |
n=100 |
19 |
Correct |
28 ms |
42664 KB |
n=100 |
20 |
Correct |
23 ms |
42660 KB |
n=100 |
21 |
Correct |
23 ms |
42632 KB |
n=100 |
22 |
Correct |
24 ms |
42652 KB |
n=100 |
23 |
Correct |
22 ms |
42588 KB |
n=100 |
24 |
Correct |
24 ms |
42576 KB |
n=100 |
25 |
Correct |
30 ms |
42660 KB |
n=100 |
26 |
Correct |
25 ms |
42580 KB |
n=12 |
27 |
Correct |
27 ms |
42676 KB |
n=100 |
28 |
Correct |
26 ms |
43112 KB |
n=500 |
29 |
Correct |
30 ms |
43060 KB |
n=500 |
30 |
Correct |
24 ms |
43092 KB |
n=500 |
31 |
Correct |
25 ms |
43164 KB |
n=500 |
32 |
Correct |
33 ms |
43112 KB |
n=500 |
33 |
Correct |
24 ms |
43152 KB |
n=500 |
34 |
Correct |
24 ms |
43092 KB |
n=500 |
35 |
Correct |
22 ms |
43148 KB |
n=500 |
36 |
Correct |
21 ms |
43084 KB |
n=500 |
37 |
Correct |
28 ms |
43032 KB |
n=500 |
38 |
Correct |
26 ms |
43096 KB |
n=500 |
39 |
Correct |
23 ms |
43084 KB |
n=500 |
40 |
Correct |
24 ms |
43052 KB |
n=500 |
41 |
Correct |
22 ms |
43088 KB |
n=500 |
42 |
Correct |
24 ms |
43056 KB |
n=500 |
43 |
Correct |
24 ms |
43092 KB |
n=500 |
44 |
Correct |
24 ms |
43120 KB |
n=500 |
45 |
Correct |
27 ms |
43084 KB |
n=500 |
46 |
Correct |
24 ms |
43072 KB |
n=500 |
47 |
Correct |
26 ms |
43064 KB |
n=500 |
48 |
Correct |
23 ms |
43148 KB |
n=500 |
49 |
Correct |
23 ms |
43092 KB |
n=500 |
50 |
Correct |
23 ms |
43000 KB |
n=500 |
51 |
Correct |
23 ms |
43056 KB |
n=500 |
52 |
Correct |
23 ms |
43092 KB |
n=500 |
53 |
Correct |
23 ms |
43100 KB |
n=500 |
54 |
Correct |
22 ms |
43052 KB |
n=500 |
55 |
Correct |
22 ms |
42964 KB |
n=278 |
56 |
Correct |
26 ms |
43168 KB |
n=500 |
57 |
Correct |
24 ms |
43092 KB |
n=500 |
58 |
Correct |
22 ms |
43028 KB |
n=500 |
59 |
Correct |
48 ms |
45092 KB |
n=2000 |
60 |
Correct |
49 ms |
45188 KB |
n=2000 |
61 |
Correct |
41 ms |
45116 KB |
n=2000 |
62 |
Correct |
40 ms |
45080 KB |
n=2000 |
63 |
Correct |
43 ms |
45060 KB |
n=2000 |
64 |
Correct |
38 ms |
45132 KB |
n=2000 |
65 |
Correct |
44 ms |
45096 KB |
n=2000 |
66 |
Correct |
39 ms |
45176 KB |
n=2000 |
67 |
Correct |
42 ms |
45084 KB |
n=2000 |
68 |
Correct |
48 ms |
45136 KB |
n=2000 |
69 |
Correct |
33 ms |
45176 KB |
n=2000 |
70 |
Correct |
31 ms |
45040 KB |
n=2000 |
71 |
Correct |
30 ms |
44992 KB |
n=2000 |
72 |
Correct |
33 ms |
45012 KB |
n=2000 |
73 |
Correct |
29 ms |
45028 KB |
n=2000 |
74 |
Correct |
41 ms |
44996 KB |
n=1844 |
75 |
Correct |
30 ms |
45104 KB |
n=2000 |
76 |
Correct |
41 ms |
45144 KB |
n=2000 |
77 |
Correct |
38 ms |
45104 KB |
n=2000 |
78 |
Correct |
38 ms |
45096 KB |
n=2000 |
79 |
Correct |
45 ms |
45100 KB |
n=2000 |
80 |
Correct |
39 ms |
45140 KB |
n=2000 |
81 |
Correct |
42 ms |
45120 KB |
n=2000 |
82 |
Correct |
53 ms |
45012 KB |
n=2000 |
83 |
Correct |
37 ms |
45120 KB |
n=2000 |
84 |
Correct |
37 ms |
44980 KB |
n=2000 |
85 |
Correct |
38 ms |
45032 KB |
n=2000 |
86 |
Correct |
38 ms |
45056 KB |
n=2000 |
87 |
Correct |
38 ms |
45012 KB |
n=2000 |
88 |
Correct |
36 ms |
45192 KB |
n=2000 |
89 |
Correct |
38 ms |
45192 KB |
n=2000 |
90 |
Correct |
38 ms |
45200 KB |
n=2000 |
91 |
Correct |
29 ms |
45012 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
42580 KB |
n=5 |
2 |
Correct |
19 ms |
42580 KB |
n=100 |
3 |
Correct |
28 ms |
42588 KB |
n=100 |
4 |
Correct |
21 ms |
42580 KB |
n=100 |
5 |
Correct |
21 ms |
42580 KB |
n=100 |
6 |
Correct |
20 ms |
42600 KB |
n=100 |
7 |
Correct |
24 ms |
42728 KB |
n=100 |
8 |
Correct |
22 ms |
42600 KB |
n=100 |
9 |
Correct |
24 ms |
42572 KB |
n=100 |
10 |
Correct |
21 ms |
42700 KB |
n=100 |
11 |
Correct |
20 ms |
42696 KB |
n=100 |
12 |
Correct |
28 ms |
42696 KB |
n=100 |
13 |
Correct |
21 ms |
42604 KB |
n=100 |
14 |
Correct |
20 ms |
42700 KB |
n=100 |
15 |
Correct |
25 ms |
42676 KB |
n=100 |
16 |
Correct |
20 ms |
42580 KB |
n=100 |
17 |
Correct |
19 ms |
42580 KB |
n=100 |
18 |
Correct |
21 ms |
42632 KB |
n=100 |
19 |
Correct |
28 ms |
42664 KB |
n=100 |
20 |
Correct |
23 ms |
42660 KB |
n=100 |
21 |
Correct |
23 ms |
42632 KB |
n=100 |
22 |
Correct |
24 ms |
42652 KB |
n=100 |
23 |
Correct |
22 ms |
42588 KB |
n=100 |
24 |
Correct |
24 ms |
42576 KB |
n=100 |
25 |
Correct |
30 ms |
42660 KB |
n=100 |
26 |
Correct |
25 ms |
42580 KB |
n=12 |
27 |
Correct |
27 ms |
42676 KB |
n=100 |
28 |
Correct |
26 ms |
43112 KB |
n=500 |
29 |
Correct |
30 ms |
43060 KB |
n=500 |
30 |
Correct |
24 ms |
43092 KB |
n=500 |
31 |
Correct |
25 ms |
43164 KB |
n=500 |
32 |
Correct |
33 ms |
43112 KB |
n=500 |
33 |
Correct |
24 ms |
43152 KB |
n=500 |
34 |
Correct |
24 ms |
43092 KB |
n=500 |
35 |
Correct |
22 ms |
43148 KB |
n=500 |
36 |
Correct |
21 ms |
43084 KB |
n=500 |
37 |
Correct |
28 ms |
43032 KB |
n=500 |
38 |
Correct |
26 ms |
43096 KB |
n=500 |
39 |
Correct |
23 ms |
43084 KB |
n=500 |
40 |
Correct |
24 ms |
43052 KB |
n=500 |
41 |
Correct |
22 ms |
43088 KB |
n=500 |
42 |
Correct |
24 ms |
43056 KB |
n=500 |
43 |
Correct |
24 ms |
43092 KB |
n=500 |
44 |
Correct |
24 ms |
43120 KB |
n=500 |
45 |
Correct |
27 ms |
43084 KB |
n=500 |
46 |
Correct |
24 ms |
43072 KB |
n=500 |
47 |
Correct |
26 ms |
43064 KB |
n=500 |
48 |
Correct |
23 ms |
43148 KB |
n=500 |
49 |
Correct |
23 ms |
43092 KB |
n=500 |
50 |
Correct |
23 ms |
43000 KB |
n=500 |
51 |
Correct |
23 ms |
43056 KB |
n=500 |
52 |
Correct |
23 ms |
43092 KB |
n=500 |
53 |
Correct |
23 ms |
43100 KB |
n=500 |
54 |
Correct |
22 ms |
43052 KB |
n=500 |
55 |
Correct |
22 ms |
42964 KB |
n=278 |
56 |
Correct |
26 ms |
43168 KB |
n=500 |
57 |
Correct |
24 ms |
43092 KB |
n=500 |
58 |
Correct |
22 ms |
43028 KB |
n=500 |
59 |
Correct |
48 ms |
45092 KB |
n=2000 |
60 |
Correct |
49 ms |
45188 KB |
n=2000 |
61 |
Correct |
41 ms |
45116 KB |
n=2000 |
62 |
Correct |
40 ms |
45080 KB |
n=2000 |
63 |
Correct |
43 ms |
45060 KB |
n=2000 |
64 |
Correct |
38 ms |
45132 KB |
n=2000 |
65 |
Correct |
44 ms |
45096 KB |
n=2000 |
66 |
Correct |
39 ms |
45176 KB |
n=2000 |
67 |
Correct |
42 ms |
45084 KB |
n=2000 |
68 |
Correct |
48 ms |
45136 KB |
n=2000 |
69 |
Correct |
33 ms |
45176 KB |
n=2000 |
70 |
Correct |
31 ms |
45040 KB |
n=2000 |
71 |
Correct |
30 ms |
44992 KB |
n=2000 |
72 |
Correct |
33 ms |
45012 KB |
n=2000 |
73 |
Correct |
29 ms |
45028 KB |
n=2000 |
74 |
Correct |
41 ms |
44996 KB |
n=1844 |
75 |
Correct |
30 ms |
45104 KB |
n=2000 |
76 |
Correct |
41 ms |
45144 KB |
n=2000 |
77 |
Correct |
38 ms |
45104 KB |
n=2000 |
78 |
Correct |
38 ms |
45096 KB |
n=2000 |
79 |
Correct |
45 ms |
45100 KB |
n=2000 |
80 |
Correct |
39 ms |
45140 KB |
n=2000 |
81 |
Correct |
42 ms |
45120 KB |
n=2000 |
82 |
Correct |
53 ms |
45012 KB |
n=2000 |
83 |
Correct |
37 ms |
45120 KB |
n=2000 |
84 |
Correct |
37 ms |
44980 KB |
n=2000 |
85 |
Correct |
38 ms |
45032 KB |
n=2000 |
86 |
Correct |
38 ms |
45056 KB |
n=2000 |
87 |
Correct |
38 ms |
45012 KB |
n=2000 |
88 |
Correct |
36 ms |
45192 KB |
n=2000 |
89 |
Correct |
38 ms |
45192 KB |
n=2000 |
90 |
Correct |
38 ms |
45200 KB |
n=2000 |
91 |
Correct |
29 ms |
45012 KB |
n=2000 |
92 |
Incorrect |
185 ms |
72800 KB |
Expected integer, but "000000000000000000000000000000...0000000000000000000000000000000" found |
93 |
Halted |
0 ms |
0 KB |
- |