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 taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 2e5 + 1;
int n, m, q;
int lift[maxN][20];
int depth[maxN];
int a[maxN];
vector <int> adj[maxN];
void dfs(int u, int p){
lift[u][0] = p;
for (int i = 1; i < 20; ++i){
lift[u][i] = lift[lift[u][i - 1]][i - 1];
}
depth[u] = depth[p] + 1;
for (int i: adj[u]){
if (i == p) continue;
dfs(i, u);
}
}
int lca(int u, int v){
if (depth[u] < depth[v]) swap(u, v);
int k = depth[u] - depth[v];
while (k){
int t = k & -k;
u = lift[u][__lg(t)];
k ^= t;
}
if (u == v) return u;
for (int i = 19; i >= 0; --i){
if (lift[u][i] != lift[v][i]){
u = lift[u][i];
v = lift[v][i];
}
}
return lift[u][0];
}
set <int> S[maxN];
set <int> Q[maxN];
void Init(){
cin >> n >> m >> q;
for (int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1, 0);
for (int i = 1; i <= m; ++i){
cin >> a[i];
Q[a[i]].insert(i);
}
for (int i = 1; i < m; ++i){
int t = lca(a[i], a[i + 1]);
S[t].insert(i);
}
while (q--){
int t; cin >> t;
if (t == 1){
int pos, v;
cin >> pos >> v;
Q[a[pos]].erase(pos);
Q[v].insert(pos);
if (pos > 1 && pos < m){
int l = lca(a[pos], a[pos - 1]);
int r = lca(a[pos], a[pos + 1]);
a[pos] = v;
S[l].erase(pos - 1);
S[r].erase(pos);
l = lca(a[pos], a[pos - 1]);
r = lca(a[pos], a[pos + 1]);
S[l].insert(pos - 1);
S[r].insert(pos);
}
if (pos == 1){
int r = lca(a[pos], a[pos + 1]);
a[pos] = v;
S[r].erase(pos);
r = lca(a[pos], a[pos + 1]);
S[r].insert(pos);
}
if (pos == m){
int l = lca(a[pos], a[pos - 1]);
a[pos] = v;
S[l].erase(pos - 1);
l = lca(a[pos], a[pos - 1]);
S[l].insert(pos - 1);
}
}
else{
int l, r, v;
cin >> l >> r >> v;
if (l == r){
if (a[l] != v) cout << "-1 -1\n";
else cout << l << " " << r << "\n";
}
else{
auto k = S[v].lower_bound(l);
if (k == S[v].end() || *k >= r){
auto kk = Q[v].lower_bound(l);
if (kk == Q[v].end() || *kk > r) cout << "-1 -1\n";
else cout << *kk << " " << *kk << "\n";
}
else cout << *k << " " << *k + 1 << "\n";
}
}
}
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message (stderr)
treearray.cpp: In function 'int main()':
treearray.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen(taskname".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... |