# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
89727 | mirbek01 | Birthday gift (IZhO18_treearray) | C++17 | 2375 ms | 79308 KiB |
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;
const int N = 2e5 + 2;
int n, m, q, a[N];
int p[20][N], tin[N], tout[N], timer;
vector <int> g[N];
set <int> s[N], ps[N];
set <int> :: iterator it;
void dfs(int v, int pr = 0){
tin[v] = ++ timer;
p[0][v] = pr;
for(int i = 1; i <= 19; i ++)
p[i][v] = p[i - 1][ p[i - 1][v] ];
for(int to : g[v]){
if(to == pr)
continue;
dfs(to, v);
}
tout[v] = timer;
}
bool upper(int a, int b){
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int lca(int a, int b){
if(upper(a, b))
return a;
if(upper(b, a))
return b;
for(int i = 19; i >= 0; i --){
if(p[i][a] && !upper(p[i][a], b))
a = p[i][a];
}
return p[0][a];
}
int main(){
scanf("%d %d %d", &n, &m, &q);
for(int i = 1; i < n; i ++){
int u, v;
scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
for(int i = 1; i <= m; i ++){
scanf("%d", a + i);
if(i > 1){
int lc = lca(a[i - 1], a[i]);
s[lc].insert(i - 1);
}
ps[a[i]].insert(i);
}
while(q --){
int l, r, v, type;
scanf("%d", &type);
scanf("%d %d", &l, &r);
if(type == 1){
ps[ a[l] ].erase(l);
ps[ r ].insert(l);
if(l + 1 <= m){
int lc = lca(a[l], a[l + 1]);
s[lc].erase(l);
lc = lca(r, a[l + 1]);
s[lc].insert(l);
}
if(l - 1 >= 1){
int lc = lca(a[l], a[l - 1]);
s[lc].erase(l - 1);
lc = lca(r, a[l - 1]);
s[lc].insert(l - 1);
}
a[l] = r;
} else {
scanf("%d", &v);
if(l == r){
if(a[l] == v){
cout << l << " " << l << endl;
} else {
puts("-1 -1");
}
} else {
if(!ps[v].empty()){
it = ps[v].lower_bound(l);
if(it != ps[v].end() && (*it) <= r){
cout << (*it) << " " << (*it) << endl;
continue;
}
}
if(s[v].empty()){
puts("-1 -1");
continue;
}
it = s[v].lower_bound(l);
if(it == s[v].end() || (*it) >= r){
puts("-1 -1");
continue;
}
cout << *it << " " << (*it) + 1 << endl;
}
}
}
}
Compilation message (stderr)
# | 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... |