이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
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];
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].push_back(v);
g[v].push_back(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 < n) st[lca(a[pos], a[pos + 1])].erase(pos + 1);
a[pos] = v;
poses[a[pos]].insert(pos);
if(pos > 1) st[lca(a[pos], a[pos - 1])].insert(pos);
if(pos < n) st[lca(a[pos], a[pos + 1])].insert(pos + 1);
} else {
int l, r, v;
cin >> l >> r >> v;
if(poses[v].size()) {
auto it = poses[v].lower_bound(l);
if(it != poses[v].end()) {
int k = *it;
if(l <= k && k <= r) {
cout << k << ' ' << k << '\n';
continue;
}
}
}
auto it = st[v].upper_bound(l);
int ans1 = -1, ans2 = -1;
if(it != st[v].end()) {
int cur = *it;
if(l < cur && 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... |