/**
* LES GREATEABLES BRO TEAM
**/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
string to_string(const string s) {
return '"' + s + '"';
}
string to_string(const char c) {
return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
return to_string(string(s));
}
string to_string(bool f) {
return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
string res = "{"; bool f = true;
for (auto x: v)
res += (f ? to_string(x) : ", " + to_string(x)), f = false;
return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
cerr << to_string(x);
if (sizeof... (y))
cerr << ", ";
debug_args(y...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif
const int N = 3e3 + 14;
struct New {
int v, l, r, i;
};
int n, m, q, level[N], up[15][N], a[N], t[4 * N];
vector<vector<int>> g(N);
void dfs(int v, int pr) {
level[v] = level[pr] + 1;
up[0][v] = pr;
for (int i = 1; i <= 9; i++)
up[i][v] = up[i - 1][up[i - 1][v]];
for (int to: g[v]) {
if (to == pr)
continue;
dfs(to, v);
}
}
int lca(int x, int y) {
if (level[x] > level[y])
swap(x, y);
for (int i = 9; i >= 0; i--) {
if (level[up[i][y]] >= level[x])
y = up[i][y];
}
if (x == y)
return x;
for (int i = 9; i >= 0; i--) {
if (up[i][x] != up[i][y])
x = up[i][x], y = up[i][y];
}
return up[0][x];
}
int not_lca(int x, int y) {
if (level[x] > level[y])
swap(x, y);
for (int i = 9; i >= 0; i--) {
if (level[up[i][y]] > level[x])
y = up[i][y];
}
if (up[0][y] == x)
return y;
return -1;
}
void build(int x, int lx, int rx) {
if (lx == rx)
t[x] = a[lx];
else {
int mid = (lx + rx) / 2;
build(2 * x + 1, lx, mid);
build(2 * x + 2, mid + 1, rx);
t[x] = lca(t[2 * x + 1], t[2 * x + 2]);
}
}
int get(int l, int r, int x, int lx, int rx) {
if (rx < l || r < lx) return -1;
if (l <= lx && rx <= r) return t[x];
int mid = (lx + rx) / 2;
int r1 = get(l, r, 2 * x + 1, lx, mid);
int r2 = get(l, r, 2 * x + 2, mid + 1, rx);
if (r1 == -1 && r2 == -1)
return -1;
if (r1 == -1)
return r2;
if (r2 == -1)
return r1;
return lca(r1, r2);
}
void upd(int i, int v, int x, int lx, int rx) {
if (lx == rx)
t[x] = v;
else {
int mid = (lx + rx) / 2;
if (i <= mid)
upd(i, v, 2 * x + 1, lx, mid);
else
upd(i, v, 2 * x + 2, mid + 1, rx);
t[x] = lca(t[2 * x + 1], t[2 * x + 2]);
}
}
void solve() {
cin >> n >> m >> q;
for (int i = 1, x, y; i < n; i++) {
cin >> x >> y;
g[x].push_back(y), g[y].push_back(x);
}
dfs(1, 0);
for (int i = 1; i <= m; i++)
cin >> a[i];
vector<int> b(m + 1);
set<int> s1[n + 1], s2[n + 1];
for (int i = 1; i < m; i++) {
b[i] = lca(a[i], a[i + 1]);
s1[b[i]].insert(i);
s2[a[i]].insert(i);
}
while (q--) {
int type;
cin >> type;
if (type == 1) {
int pos, v;
cin >> pos >> v;
s2[a[pos]].erase(pos);
a[pos] = v;
s2[a[pos]].insert(pos);
if (pos + 1 <= m) {
s1[b[pos]].erase(pos);
b[pos] = lca(a[pos], a[pos + 1]);
s1[b[pos]].insert(pos);
}
if (pos - 1 >= 1) {
s1[b[pos - 1]].erase(pos - 1);
b[pos - 1] = lca(a[pos - 1], a[pos]);
s1[b[pos - 1]].insert(pos - 1);
}
} else {
int l, r, v;
cin >> l >> r >> v;
bool found = 0;
if (sz(s2[v])) {
auto it = s2[v].lower_bound(l);
if (it != s2[v].end() && *it <= r) {
cout << *it << ' ' << *it << '\n';
found = 1;
}
}
if (!found && sz(s1[v])) {
auto it = s1[v].lower_bound(l);
if (it != s1[v].end() && *it <= r - 1) {
cout << *it << ' ' << (*it) + 1 << '\n';
found = 1;
}
}
if (!found)
cout << "-1 -1\n";
}
}
}
signed main() {
setIO();
int tt = 1;
if (FLAG) {
cin >> tt;
}
while (tt--) {
solve();
}
return 0;
}
void setIO(const string &f) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen((f + ".in").c_str(), "r")) {
freopen((f + ".in").c_str(), "r", stdin);
freopen((f + ".out").c_str(), "w", stdout);
}
}
Compilation message
treearray.cpp: In function 'void setIO(const string&)':
treearray.cpp:222:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
222 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:223:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
223 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n=5 |
2 |
Correct |
1 ms |
460 KB |
n=100 |
3 |
Correct |
1 ms |
460 KB |
n=100 |
4 |
Correct |
1 ms |
460 KB |
n=100 |
5 |
Correct |
1 ms |
460 KB |
n=100 |
6 |
Correct |
1 ms |
460 KB |
n=100 |
7 |
Correct |
1 ms |
460 KB |
n=100 |
8 |
Correct |
1 ms |
460 KB |
n=100 |
9 |
Correct |
1 ms |
460 KB |
n=100 |
10 |
Correct |
1 ms |
460 KB |
n=100 |
11 |
Correct |
1 ms |
460 KB |
n=100 |
12 |
Correct |
1 ms |
460 KB |
n=100 |
13 |
Correct |
1 ms |
460 KB |
n=100 |
14 |
Correct |
1 ms |
460 KB |
n=100 |
15 |
Correct |
1 ms |
460 KB |
n=100 |
16 |
Correct |
1 ms |
460 KB |
n=100 |
17 |
Incorrect |
1 ms |
460 KB |
Jury has the answer but participant has not |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n=5 |
2 |
Correct |
1 ms |
460 KB |
n=100 |
3 |
Correct |
1 ms |
460 KB |
n=100 |
4 |
Correct |
1 ms |
460 KB |
n=100 |
5 |
Correct |
1 ms |
460 KB |
n=100 |
6 |
Correct |
1 ms |
460 KB |
n=100 |
7 |
Correct |
1 ms |
460 KB |
n=100 |
8 |
Correct |
1 ms |
460 KB |
n=100 |
9 |
Correct |
1 ms |
460 KB |
n=100 |
10 |
Correct |
1 ms |
460 KB |
n=100 |
11 |
Correct |
1 ms |
460 KB |
n=100 |
12 |
Correct |
1 ms |
460 KB |
n=100 |
13 |
Correct |
1 ms |
460 KB |
n=100 |
14 |
Correct |
1 ms |
460 KB |
n=100 |
15 |
Correct |
1 ms |
460 KB |
n=100 |
16 |
Correct |
1 ms |
460 KB |
n=100 |
17 |
Incorrect |
1 ms |
460 KB |
Jury has the answer but participant has not |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n=5 |
2 |
Correct |
1 ms |
460 KB |
n=100 |
3 |
Correct |
1 ms |
460 KB |
n=100 |
4 |
Correct |
1 ms |
460 KB |
n=100 |
5 |
Correct |
1 ms |
460 KB |
n=100 |
6 |
Correct |
1 ms |
460 KB |
n=100 |
7 |
Correct |
1 ms |
460 KB |
n=100 |
8 |
Correct |
1 ms |
460 KB |
n=100 |
9 |
Correct |
1 ms |
460 KB |
n=100 |
10 |
Correct |
1 ms |
460 KB |
n=100 |
11 |
Correct |
1 ms |
460 KB |
n=100 |
12 |
Correct |
1 ms |
460 KB |
n=100 |
13 |
Correct |
1 ms |
460 KB |
n=100 |
14 |
Correct |
1 ms |
460 KB |
n=100 |
15 |
Correct |
1 ms |
460 KB |
n=100 |
16 |
Correct |
1 ms |
460 KB |
n=100 |
17 |
Incorrect |
1 ms |
460 KB |
Jury has the answer but participant has not |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n=5 |
2 |
Correct |
1 ms |
460 KB |
n=100 |
3 |
Correct |
1 ms |
460 KB |
n=100 |
4 |
Correct |
1 ms |
460 KB |
n=100 |
5 |
Correct |
1 ms |
460 KB |
n=100 |
6 |
Correct |
1 ms |
460 KB |
n=100 |
7 |
Correct |
1 ms |
460 KB |
n=100 |
8 |
Correct |
1 ms |
460 KB |
n=100 |
9 |
Correct |
1 ms |
460 KB |
n=100 |
10 |
Correct |
1 ms |
460 KB |
n=100 |
11 |
Correct |
1 ms |
460 KB |
n=100 |
12 |
Correct |
1 ms |
460 KB |
n=100 |
13 |
Correct |
1 ms |
460 KB |
n=100 |
14 |
Correct |
1 ms |
460 KB |
n=100 |
15 |
Correct |
1 ms |
460 KB |
n=100 |
16 |
Correct |
1 ms |
460 KB |
n=100 |
17 |
Incorrect |
1 ms |
460 KB |
Jury has the answer but participant has not |
18 |
Halted |
0 ms |
0 KB |
- |