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 <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
using namespace std;
const int l = 18;
set <int> v[2 * 100002];
set<int> p[2 * 100002];
int a[2 * 100002];
int up[2 * 100002][l + 1], tin[2 * 100002], tout[2 * 100002], t;
vector<int> g[2 * 100002];
void Dfs(int v, int p)
{
tin[v] = ++t;
up[v][0] = p;
for (int i = 1; i <= l; i++)
if (up[v][i - 1] != -1)
up[v][i] = up[up[v][i - 1]][i - 1];
for (int to : g[v])
if (to != p)
Dfs(to, v);
tout[v] = t;
}
bool IsParent(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int Lca(int a, int b)
{
if (IsParent(a, b))return a;
if (IsParent(b, a))return b;
for (int i = l; i >= 0; --i)
if (up[a][i] != -1 && !IsParent(up[a][i], b))
a = up[a][i];
return up[a][0];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < n - 1; i++)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++)
for (int j = 0; j <= l; j++)
up[i][j] = -1;
Dfs(1, -1);
for (int i = 0; i < m; i++)
{
cin >> a[i];
v[a[i]].insert(i);
if (i > 0)
p[Lca(a[i], a[i - 1])].insert(i - 1);
}
while (q--)
{
int type;
cin >> type;
if (type == 1)
{
int pos, V;
cin >> pos >> V;
pos--;
v[a[pos]].erase(pos);
if (pos - 1 >= 0)
{
p[Lca(a[pos], a[pos - 1])].erase(pos - 1);
p[Lca(V, a[pos - 1])].insert(pos - 1);
}
if (pos + 1 < m)
{
p[Lca(a[pos], a[pos + 1])].erase(pos);
p[Lca(V, a[pos + 1])].insert(pos);
}
a[pos] = V;
v[V].insert(pos);
}
else
{
bool ok = false;
int l, r, V;
cin >> l >> r >> V;
l--;
r--;
auto it = v[V].lower_bound(l);
if (it != v[V].end() && *it <= r)
{
cout << *it + 1 << ' ' << *it + 1 << '\n';
ok = true;
}
if (!ok)
{
auto it = p[V].lower_bound(l);
if (it != p[V].end() && *it + 1 <= r)
{
cout << *it + 1 << ' ' << *it + 2 << '\n';
ok = true;
}
}
if (!ok)
cout << -1 << ' ' << -1 << '\n';
}
}
return 0;
}
# | 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... |