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 pb push_back
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(data) data.begin() , data.end()
#define endl '\n'
//freopen("nenokku_easy.in", "r", stdin);
//freopen("nenokku_easy.out", "w", stdout);
//#define int long long
#define pii pair < int, int >
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int lg;
int n, m, q, sz, a[N], deep[N], mn[23][N * 2], pos[23][N * 2], tin[N], tout[N], nw;
vector < int > g[N], euler, depth;
void dfs(int v, int pr) {
tin[v] = ++sz;
euler.pb(v);
depth.pb(deep[v]);
for(int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if(to != pr) {
deep[to] = deep[v] + 1;
dfs(to, v);
depth.pb(deep[v]);
euler.pb(v);
}
}
tout[v] = ++sz;
}
void calc() {
for (int l = 0; l < lg - 1; l++) {
for (int i = 1; i + (1 << l) <= nw; i++) {
mn[l + 1][i] = min(mn[l][i], mn[l][i + (1 << l)]);
if(mn[l][i] == mn[l + 1][i]) pos[l + 1][i] = pos[l][i];
else pos[l + 1][i] = pos[l][i + (1 << l)];
}
}
}
int lca(int l, int r) {
if(tin[l] > tin[r]) swap(l, r);
l = tout[l];
r = tin[r];
int t = __lg(r - l), z = min(mn[t][l], mn[t][r - (1 << t)]);
if(z == mn[t][l]) return euler[pos[t][l]];
else return euler[pos[t][r - (1 << t)]];
}
main() {
/*ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);*/
cin >> n >> m >> q;
while((1 << lg) <= n) {
lg++;
}
for(int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
euler.pb(0); depth.pb(0);
dfs(1, 1);
nw = euler.size() - 1;
for(int i = 1; i <= nw; i++) {
mn[0][i] = depth[i];
pos[0][i] = i;
}
calc();
for(int i = 1; i <= m; i++) {
cin >> a[i];
}
while(q--) {
int tp, pos, l, r, v;
cin >> tp;
if(tp % 2) {
cin >> pos >> v;
a[pos] = v;
} else {
cin >> l >> r >> v;
bool ok = 0;
for(int i = l; i <= r; i++) {
int lc = a[i];
if(lc == v) {
cout << i << " " << i << endl;
ok = 1;
break;
}
for(int j = i + 1; j <= r; j++) {
if(tin[lc] <= tin[a[j]] && tout[lc] >= a[j]) {
;
}
else if(tin[lc] >= tin[a[j]] && tout[lc] <= a[j]) lc = a[j];
else lc = lca(lc, a[j]);
if(lc == v) {
ok = 1;
cout << i << " " << j << endl;
break;
}
}
if(ok) break;
}
if(!ok) cout << "-1 -1\n";
}
}
}
/*
5 4 2
1 2
3 1
3 4
5 3
4 5 2 3
1 3 5
2 1 3 1
*/
Compilation message (stderr)
treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < g[v].size(); i++) {
| ~~^~~~~~~~~~~~~
treearray.cpp: At global scope:
treearray.cpp:55:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
55 | main() {
| ^~~~
# | 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... |