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>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz size()
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int MAXN = 2e5 + 10;
int n, m, q;
int tin[MAXN], tout[MAXN];
int up[MAXN][19];
vector <int> g[MAXN];
set <int> st[MAXN], poses[MAXN];
int a[MAXN];
int tmr;
void dfs(int v = 1, int p = 0) {
tin[v] = ++tmr;
up[v][0] = p;
for(int i = 1; i < 18; ++i)
if(up[v][i - 1])
up[v][i] = up[up[v][i - 1]][i - 1];
for(auto to : g[v])
if(to != p)
dfs(to, v);
tout[v] = ++tmr;
}
bool upper(int v, int u) {
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int a, int b) {
if(upper(a, b)) return a;
if(upper(b, a)) return b;
for(int i = 17; i >= 0; --i)
if(up[a][i] && !upper(up[a][i], b))
a = up[a][i];
assert(up[a][0] > 0);
return up[a][0];
}
void solve() {
cin >> n >> m >> q;
for(int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs();
for(int i = 1; i <= m; ++i)
cin >> a[i], poses[a[i]].insert(i);
for(int i = 2; i <= m; ++i)
st[lca(a[i], a[i - 1])].insert(i);
for(int i = 1; i <= q; ++i) {
int type;
cin >> type;
if(type == 1) {
int pos, v;
cin >> pos >> v;
poses[a[pos]].erase(pos);
if(pos > 1) st[lca(a[pos], a[pos - 1])].erase(pos);
if(pos < m) st[lca(a[pos], a[pos + 1])].erase(pos + 1);
a[pos] = v;
poses[v].insert(pos);
if(pos > 1) st[lca(v, a[pos - 1])].insert(pos);
if(pos < m) st[lca(v, a[pos + 1])].insert(pos + 1);
} else {
int l, r, v;
cin >> l >> r >> v;
if(!poses[v].empty()) {
auto it = poses[v].lower_bound(l);
if(it != poses[v].end()) {
int k = *it;
if(k <= r) {
cout << k << ' ' << k << '\n';
continue;
}
}
}
if(st[v].empty()) {
cout << "-1 -1" << '\n';
continue;
}
auto it = st[v].upper_bound(l);
int ans1 = -1, ans2 = -1;
if(it != st[v].end()) {
int cur = *it;
if(cur <= r) {
ans1 = cur - 1;
ans2 = cur;
}
}
cout << ans1 << ' ' << ans2 << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
///cin >> T;
while(T--) solve(), cout << '\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... |