This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>
using namespace std;
const int N = 5 + 1e5;
const int inf = 7 + 1e9;
int node = 0, logn;
vector <int> num, h;
vector <vector <int>> adj, p;
void dfs(int u) {
num[u] = ++ node;
for (int v : adj[u])
if (!num[v]) {
p[0][v] = u;
h[v] = h[u] + 1;
dfs(v);
}
}
int lca(int u, int v) {
if (h[u] < h[v])
swap(u, v);
for (int si = h[u] - h[v]; si > 0; si ^= si & -si) {
int i = __builtin_ctz(si & -si);
u = p[i][u];
}
if (u == v)
return u;
for (int i = logn - 1; i >= 0; i --)
if (p[i][u] != p[i][v]) {
u = p[i][u];
v = p[i][v];
}
return p[0][u];
}
int main() {
#define TASKNAME ""
ios_base :: sync_with_stdio (0);
cin.tie (0);
if ( fopen( TASKNAME".inp", "r" ) ) {
freopen (TASKNAME".inp", "r", stdin);
freopen (TASKNAME".out", "w", stdout);
}
int n, m, t;
cin >> n >> m >> t;
adj.resize(n + 1);
for (int i = 1; i < n; i ++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (logn = 1; 1 << logn <= n; logn ++);
p.assign(logn, vector <int> (n + 1, 0));
num.assign(n + 1, 0);
h.assign(n + 1, 0);
h[1] = 1;
dfs(1);
for (int i = 1; i < logn; i ++)
for (int u = 1; u <= n; u ++)
p[i][u] = p[i - 1][p[i - 1][u]];
vector <int> a(m + 1, 0), b(m, 0);
vector <set <int>> q(n + 1), g(n + 1);
for (int i = 1; i <= m; i ++) {
cin >> a[i];
g[a[i]].insert(i);
if (i > 1) {
b[i - 1] = num[lca(a[i - 1], a[i])];
q[b[i - 1]].insert(i - 1);
}
}
while (t --) {
int type;
cin >> type;
if (type == 1) {
int pos, val;
cin >> pos >> val;
g[a[pos]].erase(pos);
a[pos] = val;
g[val].insert(pos);
if (pos > 1) {
q[b[pos - 1]].erase(pos - 1);
b[pos - 1] = num[lca(a[pos - 1], a[pos])];
q[b[pos - 1]].insert(pos - 1);
}
if (pos < n) {
q[b[pos]].erase(pos);
b[pos] = num[lca(a[pos], a[pos + 1])];
q[b[pos]].insert(pos);
}
}
else {
int u, v, val;
cin >> u >> v >> val;
auto it = q[num[val]].lower_bound(u), it1 = g[val].lower_bound(u);
if (it != q[num[val]].end() && *it < v)
cout << *it << ' ' << *it + 1 << '\n';
else if (it1 != g[val].end() && *it1 <= v)
cout << *it1 << ' ' << *it1 << '\n';
else
cout << "-1 -1\n";
}
}
return 0;
}
Compilation message (stderr)
treearray.cpp: In function 'int main()':
treearray.cpp:50:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen (TASKNAME".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:51:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen (TASKNAME".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |