This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 2e5, cbit = 17;
int f[N + 1][cbit + 1], depth[N + 1];
vector<int> adj[N + 1];
set<int> single[N + 1], dual[N + 1];
int n, m, q, a[N + 1];
void read()
{
cin >> n >> m >> q;
for (int i = 1; i < n; ++ i)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
}
void DFS(int u, int p)
{
for (int v : adj[u])
{
if (v == p)
{
continue;
}
depth[v] = depth[u] + 1;
f[v][0] = u;
for (int i = 1; i <= cbit; ++ i)
{
f[v][i] = f[f[v][i - 1]][i - 1];
}
DFS(v, u);
}
}
int LCA(int u, int v)
{
if (depth[v] < depth[u])
{
swap(u, v);
}
int k = depth[v] - depth[u];
for (int i = cbit; i >= 0; -- i)
{
if ((k >> i) & 1)
{
v = f[v][i];
}
}
if (u == v)
{
return u;
}
for (int i = cbit; i >= 0; -- i)
{
if (f[u][i] != f[v][i])
{
u = f[u][i];
v = f[v][i];
}
}
return f[u][0];
}
void solve()
{
DFS(1, 0);
for (int i = 1; i <= m; ++ i)
{
cin >> a[i];
single[a[i]].insert(i);
if (i > 1)
{
dual[LCA(a[i], a[i - 1])].insert(i - 1);
}
}
while(q--)
{
int t;
cin >> t;
if (t == 1)
{
int pos, v;
cin >> pos >> v;
single[a[pos]].erase(pos);
single[v].insert(pos);
if (pos > 1)
{
dual[LCA(a[pos], a[pos - 1])].erase(pos - 1);
dual[LCA(v, a[pos - 1])].insert(pos - 1);
}
if (pos < m)
{
dual[LCA(a[pos], a[pos + 1])].erase(pos);
dual[LCA(v, a[pos + 1])].insert(pos);
}
a[pos] = v;
}
else
{
int l, r, u;
cin >> l >> r >> u;
auto it = single[u].lower_bound(l);
if (it != single[u].end() && *it <= r)
{
cout << *it << ' ' << *it << '\n';
continue;
}
it = dual[u].lower_bound(l);
if (it != dual[u].end() && *it < r)
{
cout << *it << ' ' << *it + 1 << '\n';
continue;
}
cout << -1 << ' ' << -1 << '\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);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
Compilation message (stderr)
treearray.cpp: In function 'int main()':
treearray.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |