#include "bits/stdc++.h"
using namespace std;
const int MAXN = 4e5+10; // 4e5 because euler can have 2 * n size which would requires n * 8 size for segment tree or otherwise it could cause error
int seglca[MAXN * 4], in[MAXN], out[MAXN], height[MAXN], seq[MAXN];
vector<int> euler;
vector< vector<int> > adj(MAXN);
set< pair<int, int> > pref[MAXN];
void dfs(int v, int p, int h)
{
in[v] = euler.size();
euler.push_back(v);
height[v] = h;
for(int e : adj[v])
{
if(e == p)
continue;
dfs(e, v, h + 1);
euler.push_back(v);
}
out[v] = euler.size() - 1;
}
void buildlca(int v, int tl, int tr)
{
if(tl == tr)
{
seglca[v] = euler[tl];
//cout << v << " " << tl << " " << tr << " " << seglca[v] << "\n";
return;
}
int tm = (tl + tr) / 2;
buildlca(2 * v, tl, tm);
buildlca(2 * v + 1, tm + 1, tr);
if(height[seglca[v * 2]] < height[seglca[v * 2 + 1]])
seglca[v] = seglca[v * 2];
else
seglca[v] = seglca[v * 2 + 1];
//cout << v << " " << tl << " " << tr << " " << seglca[v * 2] << " " << seglca[2 * v + 1] << " " << in[seglca[v * 2]] << " " << in[seglca[v * 2 + 1]] << " " << seglca[v] << "\n";
}
int getlca(int v, int tl, int tr, int l, int r)
{
if(l > r)
return -1;
if(tl == l && tr == r)
return seglca[v];
int tm = (tl + tr) / 2;
int left = getlca(2 * v, tl, tm, l, min(r, tm)), right = getlca(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
if(right == -1)
return left;
if(left == -1)
return right;
if(height[left] < height[right])
return left;
else
return right;
}
int lca(int a, int b)
{
if(in[a] > in[b])
swap(a, b);
return getlca(1, 0, euler.size() - 1, in[a], in[b]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
for(int i = 1; i <= n - 1; i++)
{
int f, s;
cin >> f >> s;
adj[f].push_back(s);
adj[s].push_back(f);
}
dfs(1, -1, 0);
buildlca(1, 0, euler.size() - 1);
for(int i = 0; i < m; i++) {
cin >> seq[i];
pref[seq[i]].insert({i, i});
if(i > 0)
{
pref[lca(seq[i - 1], seq[i])].insert({i - 1, i});
}
}
while(q--)
{
int type;
cin >> type;
if(type == 1)
{
int pos, val;
cin >> pos >> val;
pos--;
pref[seq[pos]].erase({pos, pos});
if(pos > 0)
pref[lca(seq[pos - 1], seq[pos])].erase({pos - 1, pos});
if(pos < m - 1)
pref[lca(seq[pos + 1], seq[pos])].erase({pos, pos + 1});
seq[pos] = val;
pref[seq[pos]].insert({pos, pos});
if(pos > 0)
pref[lca(seq[pos - 1], seq[pos])].insert({pos - 1, pos});
if(pos < m - 1)
pref[lca(seq[pos + 1], seq[pos])].insert({pos, pos + 1});
//updateans(1, 0, m - 1, pos, val);
} else
{
int l, r, v;
cin >> l >> r >> v;
l--;
r--;
auto it = pref[v].lower_bound({l, l});
if(it != pref[v].end() && (*it).first >= l && (*it).second <= r)
cout << (*it).first + 1 << " " << (*it).second + 1 << "\n";
else
cout << "-1 -1\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
28512 KB |
n=5 |
2 |
Correct |
13 ms |
28512 KB |
n=100 |
3 |
Correct |
13 ms |
28512 KB |
n=100 |
4 |
Correct |
16 ms |
28536 KB |
n=100 |
5 |
Correct |
13 ms |
28512 KB |
n=100 |
6 |
Correct |
13 ms |
28512 KB |
n=100 |
7 |
Correct |
13 ms |
28552 KB |
n=100 |
8 |
Correct |
13 ms |
28512 KB |
n=100 |
9 |
Correct |
13 ms |
28588 KB |
n=100 |
10 |
Correct |
13 ms |
28484 KB |
n=100 |
11 |
Correct |
13 ms |
28528 KB |
n=100 |
12 |
Correct |
13 ms |
28512 KB |
n=100 |
13 |
Correct |
13 ms |
28568 KB |
n=100 |
14 |
Correct |
14 ms |
28512 KB |
n=100 |
15 |
Correct |
13 ms |
28540 KB |
n=100 |
16 |
Correct |
13 ms |
28512 KB |
n=100 |
17 |
Correct |
13 ms |
28468 KB |
n=100 |
18 |
Correct |
13 ms |
28500 KB |
n=100 |
19 |
Correct |
13 ms |
28476 KB |
n=100 |
20 |
Correct |
13 ms |
28500 KB |
n=100 |
21 |
Correct |
14 ms |
28500 KB |
n=100 |
22 |
Correct |
13 ms |
28500 KB |
n=100 |
23 |
Correct |
13 ms |
28500 KB |
n=100 |
24 |
Correct |
13 ms |
28504 KB |
n=100 |
25 |
Correct |
13 ms |
28500 KB |
n=100 |
26 |
Correct |
14 ms |
28444 KB |
n=12 |
27 |
Correct |
16 ms |
28500 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
28512 KB |
n=5 |
2 |
Correct |
13 ms |
28512 KB |
n=100 |
3 |
Correct |
13 ms |
28512 KB |
n=100 |
4 |
Correct |
16 ms |
28536 KB |
n=100 |
5 |
Correct |
13 ms |
28512 KB |
n=100 |
6 |
Correct |
13 ms |
28512 KB |
n=100 |
7 |
Correct |
13 ms |
28552 KB |
n=100 |
8 |
Correct |
13 ms |
28512 KB |
n=100 |
9 |
Correct |
13 ms |
28588 KB |
n=100 |
10 |
Correct |
13 ms |
28484 KB |
n=100 |
11 |
Correct |
13 ms |
28528 KB |
n=100 |
12 |
Correct |
13 ms |
28512 KB |
n=100 |
13 |
Correct |
13 ms |
28568 KB |
n=100 |
14 |
Correct |
14 ms |
28512 KB |
n=100 |
15 |
Correct |
13 ms |
28540 KB |
n=100 |
16 |
Correct |
13 ms |
28512 KB |
n=100 |
17 |
Correct |
13 ms |
28468 KB |
n=100 |
18 |
Correct |
13 ms |
28500 KB |
n=100 |
19 |
Correct |
13 ms |
28476 KB |
n=100 |
20 |
Correct |
13 ms |
28500 KB |
n=100 |
21 |
Correct |
14 ms |
28500 KB |
n=100 |
22 |
Correct |
13 ms |
28500 KB |
n=100 |
23 |
Correct |
13 ms |
28500 KB |
n=100 |
24 |
Correct |
13 ms |
28504 KB |
n=100 |
25 |
Correct |
13 ms |
28500 KB |
n=100 |
26 |
Correct |
14 ms |
28444 KB |
n=12 |
27 |
Correct |
16 ms |
28500 KB |
n=100 |
28 |
Correct |
15 ms |
28628 KB |
n=500 |
29 |
Correct |
16 ms |
28564 KB |
n=500 |
30 |
Correct |
17 ms |
28632 KB |
n=500 |
31 |
Correct |
17 ms |
28640 KB |
n=500 |
32 |
Correct |
16 ms |
28628 KB |
n=500 |
33 |
Correct |
16 ms |
28628 KB |
n=500 |
34 |
Correct |
14 ms |
28552 KB |
n=500 |
35 |
Correct |
16 ms |
28600 KB |
n=500 |
36 |
Correct |
15 ms |
28640 KB |
n=500 |
37 |
Correct |
14 ms |
28640 KB |
n=500 |
38 |
Correct |
14 ms |
28640 KB |
n=500 |
39 |
Correct |
15 ms |
28640 KB |
n=500 |
40 |
Correct |
16 ms |
28640 KB |
n=500 |
41 |
Correct |
14 ms |
28556 KB |
n=500 |
42 |
Correct |
15 ms |
28640 KB |
n=500 |
43 |
Correct |
14 ms |
28640 KB |
n=500 |
44 |
Correct |
14 ms |
28640 KB |
n=500 |
45 |
Correct |
17 ms |
28640 KB |
n=500 |
46 |
Correct |
20 ms |
28640 KB |
n=500 |
47 |
Correct |
19 ms |
28640 KB |
n=500 |
48 |
Correct |
16 ms |
28640 KB |
n=500 |
49 |
Correct |
14 ms |
28640 KB |
n=500 |
50 |
Correct |
13 ms |
28576 KB |
n=500 |
51 |
Correct |
15 ms |
28588 KB |
n=500 |
52 |
Correct |
15 ms |
28636 KB |
n=500 |
53 |
Correct |
16 ms |
28640 KB |
n=500 |
54 |
Correct |
14 ms |
28668 KB |
n=500 |
55 |
Correct |
15 ms |
28636 KB |
n=278 |
56 |
Correct |
18 ms |
28640 KB |
n=500 |
57 |
Correct |
16 ms |
28548 KB |
n=500 |
58 |
Correct |
14 ms |
28640 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
28512 KB |
n=5 |
2 |
Correct |
13 ms |
28512 KB |
n=100 |
3 |
Correct |
13 ms |
28512 KB |
n=100 |
4 |
Correct |
16 ms |
28536 KB |
n=100 |
5 |
Correct |
13 ms |
28512 KB |
n=100 |
6 |
Correct |
13 ms |
28512 KB |
n=100 |
7 |
Correct |
13 ms |
28552 KB |
n=100 |
8 |
Correct |
13 ms |
28512 KB |
n=100 |
9 |
Correct |
13 ms |
28588 KB |
n=100 |
10 |
Correct |
13 ms |
28484 KB |
n=100 |
11 |
Correct |
13 ms |
28528 KB |
n=100 |
12 |
Correct |
13 ms |
28512 KB |
n=100 |
13 |
Correct |
13 ms |
28568 KB |
n=100 |
14 |
Correct |
14 ms |
28512 KB |
n=100 |
15 |
Correct |
13 ms |
28540 KB |
n=100 |
16 |
Correct |
13 ms |
28512 KB |
n=100 |
17 |
Correct |
13 ms |
28468 KB |
n=100 |
18 |
Correct |
13 ms |
28500 KB |
n=100 |
19 |
Correct |
13 ms |
28476 KB |
n=100 |
20 |
Correct |
13 ms |
28500 KB |
n=100 |
21 |
Correct |
14 ms |
28500 KB |
n=100 |
22 |
Correct |
13 ms |
28500 KB |
n=100 |
23 |
Correct |
13 ms |
28500 KB |
n=100 |
24 |
Correct |
13 ms |
28504 KB |
n=100 |
25 |
Correct |
13 ms |
28500 KB |
n=100 |
26 |
Correct |
14 ms |
28444 KB |
n=12 |
27 |
Correct |
16 ms |
28500 KB |
n=100 |
28 |
Correct |
15 ms |
28628 KB |
n=500 |
29 |
Correct |
16 ms |
28564 KB |
n=500 |
30 |
Correct |
17 ms |
28632 KB |
n=500 |
31 |
Correct |
17 ms |
28640 KB |
n=500 |
32 |
Correct |
16 ms |
28628 KB |
n=500 |
33 |
Correct |
16 ms |
28628 KB |
n=500 |
34 |
Correct |
14 ms |
28552 KB |
n=500 |
35 |
Correct |
16 ms |
28600 KB |
n=500 |
36 |
Correct |
15 ms |
28640 KB |
n=500 |
37 |
Correct |
14 ms |
28640 KB |
n=500 |
38 |
Correct |
14 ms |
28640 KB |
n=500 |
39 |
Correct |
15 ms |
28640 KB |
n=500 |
40 |
Correct |
16 ms |
28640 KB |
n=500 |
41 |
Correct |
14 ms |
28556 KB |
n=500 |
42 |
Correct |
15 ms |
28640 KB |
n=500 |
43 |
Correct |
14 ms |
28640 KB |
n=500 |
44 |
Correct |
14 ms |
28640 KB |
n=500 |
45 |
Correct |
17 ms |
28640 KB |
n=500 |
46 |
Correct |
20 ms |
28640 KB |
n=500 |
47 |
Correct |
19 ms |
28640 KB |
n=500 |
48 |
Correct |
16 ms |
28640 KB |
n=500 |
49 |
Correct |
14 ms |
28640 KB |
n=500 |
50 |
Correct |
13 ms |
28576 KB |
n=500 |
51 |
Correct |
15 ms |
28588 KB |
n=500 |
52 |
Correct |
15 ms |
28636 KB |
n=500 |
53 |
Correct |
16 ms |
28640 KB |
n=500 |
54 |
Correct |
14 ms |
28668 KB |
n=500 |
55 |
Correct |
15 ms |
28636 KB |
n=278 |
56 |
Correct |
18 ms |
28640 KB |
n=500 |
57 |
Correct |
16 ms |
28548 KB |
n=500 |
58 |
Correct |
14 ms |
28640 KB |
n=500 |
59 |
Correct |
20 ms |
28896 KB |
n=2000 |
60 |
Correct |
18 ms |
29016 KB |
n=2000 |
61 |
Correct |
20 ms |
28928 KB |
n=2000 |
62 |
Correct |
18 ms |
28788 KB |
n=2000 |
63 |
Correct |
20 ms |
28776 KB |
n=2000 |
64 |
Correct |
20 ms |
28816 KB |
n=2000 |
65 |
Correct |
20 ms |
28804 KB |
n=2000 |
66 |
Correct |
17 ms |
28884 KB |
n=2000 |
67 |
Correct |
18 ms |
28824 KB |
n=2000 |
68 |
Correct |
17 ms |
28856 KB |
n=2000 |
69 |
Correct |
16 ms |
28796 KB |
n=2000 |
70 |
Correct |
17 ms |
28764 KB |
n=2000 |
71 |
Correct |
18 ms |
28900 KB |
n=2000 |
72 |
Correct |
16 ms |
28884 KB |
n=2000 |
73 |
Correct |
17 ms |
28828 KB |
n=2000 |
74 |
Correct |
18 ms |
28756 KB |
n=1844 |
75 |
Correct |
16 ms |
28896 KB |
n=2000 |
76 |
Correct |
18 ms |
28884 KB |
n=2000 |
77 |
Correct |
18 ms |
28824 KB |
n=2000 |
78 |
Correct |
19 ms |
28884 KB |
n=2000 |
79 |
Correct |
21 ms |
28884 KB |
n=2000 |
80 |
Correct |
18 ms |
28884 KB |
n=2000 |
81 |
Correct |
18 ms |
28884 KB |
n=2000 |
82 |
Correct |
18 ms |
28840 KB |
n=2000 |
83 |
Correct |
18 ms |
28896 KB |
n=2000 |
84 |
Correct |
20 ms |
28884 KB |
n=2000 |
85 |
Correct |
18 ms |
28884 KB |
n=2000 |
86 |
Correct |
18 ms |
28788 KB |
n=2000 |
87 |
Correct |
18 ms |
28880 KB |
n=2000 |
88 |
Correct |
18 ms |
28920 KB |
n=2000 |
89 |
Correct |
17 ms |
28884 KB |
n=2000 |
90 |
Correct |
19 ms |
28884 KB |
n=2000 |
91 |
Correct |
23 ms |
28884 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
28512 KB |
n=5 |
2 |
Correct |
13 ms |
28512 KB |
n=100 |
3 |
Correct |
13 ms |
28512 KB |
n=100 |
4 |
Correct |
16 ms |
28536 KB |
n=100 |
5 |
Correct |
13 ms |
28512 KB |
n=100 |
6 |
Correct |
13 ms |
28512 KB |
n=100 |
7 |
Correct |
13 ms |
28552 KB |
n=100 |
8 |
Correct |
13 ms |
28512 KB |
n=100 |
9 |
Correct |
13 ms |
28588 KB |
n=100 |
10 |
Correct |
13 ms |
28484 KB |
n=100 |
11 |
Correct |
13 ms |
28528 KB |
n=100 |
12 |
Correct |
13 ms |
28512 KB |
n=100 |
13 |
Correct |
13 ms |
28568 KB |
n=100 |
14 |
Correct |
14 ms |
28512 KB |
n=100 |
15 |
Correct |
13 ms |
28540 KB |
n=100 |
16 |
Correct |
13 ms |
28512 KB |
n=100 |
17 |
Correct |
13 ms |
28468 KB |
n=100 |
18 |
Correct |
13 ms |
28500 KB |
n=100 |
19 |
Correct |
13 ms |
28476 KB |
n=100 |
20 |
Correct |
13 ms |
28500 KB |
n=100 |
21 |
Correct |
14 ms |
28500 KB |
n=100 |
22 |
Correct |
13 ms |
28500 KB |
n=100 |
23 |
Correct |
13 ms |
28500 KB |
n=100 |
24 |
Correct |
13 ms |
28504 KB |
n=100 |
25 |
Correct |
13 ms |
28500 KB |
n=100 |
26 |
Correct |
14 ms |
28444 KB |
n=12 |
27 |
Correct |
16 ms |
28500 KB |
n=100 |
28 |
Correct |
15 ms |
28628 KB |
n=500 |
29 |
Correct |
16 ms |
28564 KB |
n=500 |
30 |
Correct |
17 ms |
28632 KB |
n=500 |
31 |
Correct |
17 ms |
28640 KB |
n=500 |
32 |
Correct |
16 ms |
28628 KB |
n=500 |
33 |
Correct |
16 ms |
28628 KB |
n=500 |
34 |
Correct |
14 ms |
28552 KB |
n=500 |
35 |
Correct |
16 ms |
28600 KB |
n=500 |
36 |
Correct |
15 ms |
28640 KB |
n=500 |
37 |
Correct |
14 ms |
28640 KB |
n=500 |
38 |
Correct |
14 ms |
28640 KB |
n=500 |
39 |
Correct |
15 ms |
28640 KB |
n=500 |
40 |
Correct |
16 ms |
28640 KB |
n=500 |
41 |
Correct |
14 ms |
28556 KB |
n=500 |
42 |
Correct |
15 ms |
28640 KB |
n=500 |
43 |
Correct |
14 ms |
28640 KB |
n=500 |
44 |
Correct |
14 ms |
28640 KB |
n=500 |
45 |
Correct |
17 ms |
28640 KB |
n=500 |
46 |
Correct |
20 ms |
28640 KB |
n=500 |
47 |
Correct |
19 ms |
28640 KB |
n=500 |
48 |
Correct |
16 ms |
28640 KB |
n=500 |
49 |
Correct |
14 ms |
28640 KB |
n=500 |
50 |
Correct |
13 ms |
28576 KB |
n=500 |
51 |
Correct |
15 ms |
28588 KB |
n=500 |
52 |
Correct |
15 ms |
28636 KB |
n=500 |
53 |
Correct |
16 ms |
28640 KB |
n=500 |
54 |
Correct |
14 ms |
28668 KB |
n=500 |
55 |
Correct |
15 ms |
28636 KB |
n=278 |
56 |
Correct |
18 ms |
28640 KB |
n=500 |
57 |
Correct |
16 ms |
28548 KB |
n=500 |
58 |
Correct |
14 ms |
28640 KB |
n=500 |
59 |
Correct |
20 ms |
28896 KB |
n=2000 |
60 |
Correct |
18 ms |
29016 KB |
n=2000 |
61 |
Correct |
20 ms |
28928 KB |
n=2000 |
62 |
Correct |
18 ms |
28788 KB |
n=2000 |
63 |
Correct |
20 ms |
28776 KB |
n=2000 |
64 |
Correct |
20 ms |
28816 KB |
n=2000 |
65 |
Correct |
20 ms |
28804 KB |
n=2000 |
66 |
Correct |
17 ms |
28884 KB |
n=2000 |
67 |
Correct |
18 ms |
28824 KB |
n=2000 |
68 |
Correct |
17 ms |
28856 KB |
n=2000 |
69 |
Correct |
16 ms |
28796 KB |
n=2000 |
70 |
Correct |
17 ms |
28764 KB |
n=2000 |
71 |
Correct |
18 ms |
28900 KB |
n=2000 |
72 |
Correct |
16 ms |
28884 KB |
n=2000 |
73 |
Correct |
17 ms |
28828 KB |
n=2000 |
74 |
Correct |
18 ms |
28756 KB |
n=1844 |
75 |
Correct |
16 ms |
28896 KB |
n=2000 |
76 |
Correct |
18 ms |
28884 KB |
n=2000 |
77 |
Correct |
18 ms |
28824 KB |
n=2000 |
78 |
Correct |
19 ms |
28884 KB |
n=2000 |
79 |
Correct |
21 ms |
28884 KB |
n=2000 |
80 |
Correct |
18 ms |
28884 KB |
n=2000 |
81 |
Correct |
18 ms |
28884 KB |
n=2000 |
82 |
Correct |
18 ms |
28840 KB |
n=2000 |
83 |
Correct |
18 ms |
28896 KB |
n=2000 |
84 |
Correct |
20 ms |
28884 KB |
n=2000 |
85 |
Correct |
18 ms |
28884 KB |
n=2000 |
86 |
Correct |
18 ms |
28788 KB |
n=2000 |
87 |
Correct |
18 ms |
28880 KB |
n=2000 |
88 |
Correct |
18 ms |
28920 KB |
n=2000 |
89 |
Correct |
17 ms |
28884 KB |
n=2000 |
90 |
Correct |
19 ms |
28884 KB |
n=2000 |
91 |
Correct |
23 ms |
28884 KB |
n=2000 |
92 |
Correct |
886 ms |
68644 KB |
n=200000 |
93 |
Correct |
833 ms |
77244 KB |
n=200000 |
94 |
Correct |
771 ms |
81772 KB |
n=200000 |
95 |
Correct |
908 ms |
70696 KB |
n=200000 |
96 |
Correct |
852 ms |
70564 KB |
n=200000 |
97 |
Correct |
883 ms |
76048 KB |
n=200000 |
98 |
Correct |
867 ms |
70536 KB |
n=200000 |
99 |
Correct |
1068 ms |
70968 KB |
n=200000 |
100 |
Correct |
1207 ms |
70660 KB |
n=200000 |
101 |
Correct |
858 ms |
83416 KB |
n=200000 |
102 |
Correct |
534 ms |
71812 KB |
n=200000 |
103 |
Correct |
561 ms |
71772 KB |
n=200000 |
104 |
Correct |
503 ms |
71800 KB |
n=200000 |
105 |
Correct |
506 ms |
72252 KB |
n=200000 |
106 |
Correct |
527 ms |
72284 KB |
n=200000 |
107 |
Correct |
508 ms |
72208 KB |
n=200000 |
108 |
Correct |
938 ms |
70804 KB |
n=200000 |
109 |
Correct |
1066 ms |
70756 KB |
n=200000 |
110 |
Correct |
1062 ms |
70672 KB |
n=200000 |
111 |
Correct |
964 ms |
70096 KB |
n=200000 |
112 |
Correct |
935 ms |
82116 KB |
n=200000 |
113 |
Correct |
936 ms |
75944 KB |
n=200000 |
114 |
Correct |
1050 ms |
70124 KB |
n=200000 |
115 |
Correct |
1035 ms |
73248 KB |
n=200000 |
116 |
Correct |
876 ms |
70800 KB |
n=200000 |
117 |
Correct |
742 ms |
82552 KB |
n=200000 |
118 |
Correct |
1010 ms |
74592 KB |
n=200000 |
119 |
Correct |
859 ms |
70764 KB |
n=200000 |
120 |
Correct |
891 ms |
82424 KB |
n=200000 |
121 |
Correct |
868 ms |
82360 KB |
n=200000 |
122 |
Correct |
859 ms |
82728 KB |
n=200000 |
123 |
Correct |
608 ms |
72036 KB |
n=200000 |
124 |
Correct |
305 ms |
42648 KB |
n=25264 |