/**
* 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];
build(0, 1, m);
while (q--) {
int type;
cin >> type;
if (type == 1) {
int pos, v;
cin >> pos >> v;
upd(pos, v, 0, 1, m);
} else {
int l, r, v;
cin >> l >> r >> v;
bool found = 0;
for (int i = l; i <= r; i++) {
if (a[i] == v) {
cout << i << ' ' << i << '\n';
found = 1;
break;
}
}
if (found) {
continue;
}
for (int i = l; i <= r; i++) {
for (int j = i; j <= r; j++) {
if (get(i, j, 0, 1, m) == v) {
cout << i << ' ' << j << '\n';
found = 1;
break;
}
}
if (found)
break;
}
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:211:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
211 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:212:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
212 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n=5 |
2 |
Incorrect |
5 ms |
332 KB |
Wrong range. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n=5 |
2 |
Incorrect |
5 ms |
332 KB |
Wrong range. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n=5 |
2 |
Incorrect |
5 ms |
332 KB |
Wrong range. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n=5 |
2 |
Incorrect |
5 ms |
332 KB |
Wrong range. |
3 |
Halted |
0 ms |
0 KB |
- |