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 ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) __builtin_popcountll(a)
#define bithigh(a) 63-__builtin_clzll(a)
#define lg bithigh
ll highpow(ll a) { return 1LL << (ll)lg(a); }
using namespace std;
class Lca{
private:
int n;
vector<vector<int> > g, par;
vector<int> in, out, depth;
int dfs(int s, int p = -1, int d = 0, int t = 0){
par[s][0] = p;
depth[s] = d;
in[s] = t;
for (int u : g[s])
if (u^p)
t = dfs(u, s, d+1, t+1);
return out[s] = t;
}
public:
Lca(int n){
this->n = n;
g = vector<vector<int> >(n);
par = vector<vector<int> >(n, vector<int>(lg(n)+1, -1));
in = out = depth = vector<int>(n);
}
void edge(int u, int v){
g[u].pb(v);
g[v].pb(u);
}
void build(int root = 0){
dfs(root);
for (int d = 1; (1<<d) <= n; d++)
for (int s = 0; s < n; s++)
if (~par[s][d-1])
par[s][d] = par[par[s][d-1]][d-1];
}
bool Ancestor(int s, int p) const { return in[s] >= in[p] && out[s] <= out[p]; }
int Par(int s, int d) const {
if (!d) return s;
return Par(par[s][lg(d)], d-highpow(d));
}
int lca(int u, int v) const {
if (depth[u] > depth[v]) swap(u, v);
if (Ancestor(v, u)) return u;
v = Par(v, depth[v] - depth[u]);
for (int d = lg(n); ~d; d--){
if (par[u][d]^par[v][d]){
u = par[u][d];
v = par[v][d];
}
}
return par[u][0];
}
};
const ll LINF = 4e18;
const int mxN = 2e5+10, INF = 2e9, mod = (1 ? 1e9+7 : 998244353);
ll n, m, q, a[mxN];
set<int> idx[mxN][2];
Lca* lc;
void Solve(){
cin >> n >> m >> q;
lc = new Lca(n);
for (int i = 1; i < n; i++){
ri(u); ri(v);
u--; v--;
lc->edge(u, v);
}
lc->build();
for (int i = 0; i < m; i++){
cin >> a[i];
a[i]--;
idx[a[i]][0].insert(i);
}
for (int i = 0; i < m-1; i++){
int s = lc->lca(a[i], a[i+1]);
idx[s][1].insert(i);
}
while (q--){
ri(t); t--;
if (t){
ri(l); ri(r); ri(s);
l--; r--; s--;
auto it1 = idx[s][0].lower_bound(l);
auto it2 = idx[s][1].lower_bound(l);
if (it1 != idx[s][0].end() && *it1 <= r) cout << *it1 + 1 << sp << *it1 + 1 << en;
else if (it2 != idx[s][1].end() && *it2 < r) cout << *it2 + 1 << sp << *it2 + 2 << en;
else cout << -1 << sp << -1 << en;
}
else{
ri(i); ri(s);
i--; s--;
idx[a[i]][0].erase(i);
idx[s][0].insert(i);
if (i){
idx[lc->lca(a[i], a[i-1])][1].erase(i-1);
idx[lc->lca(s, a[i-1])][1].insert(i-1);
}
if (i < m-1){
idx[lc->lca(a[i], a[i+1])][1].erase(i);
idx[lc->lca(s, a[i+1])][1].insert(i);
}
a[i] = s;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0); cerr.tie(0);
cout << setprecision(12) << fixed;
cerr << setprecision(12) << fixed;
cerr << "Started!" << endl;
int t = 1;
//cin >> t;
while (t--)
Solve();
return 0;
}
Compilation message (stderr)
treearray.cpp: In function 'long long int highpow(long long int)':
treearray.cpp:26:22: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
26 | #define bithigh(a) 63-__builtin_clzll(a)
| ^
treearray.cpp:27:12: note: in expansion of macro 'bithigh'
27 | #define lg bithigh
| ^~~~~~~
treearray.cpp:28:38: note: in expansion of macro 'lg'
28 | ll highpow(ll a) { return 1LL << (ll)lg(a); }
| ^~
# | 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... |