# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
166771 | abil | Birthday gift (IZhO18_treearray) | C++14 | 2518 ms | 84984 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>
#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
//#define int long long
using namespace std;
const int N = (2e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);
vector<int > g[N];
int up[20][N], tin[N], tout[N], timer, a[N];
void dfs(int v,int pr){
tin[v] = ++timer;
up[0][v] = pr;
for(int i = 1;i <= 19; i++){
up[i][v] = up[i - 1][up[i - 1][v]];
}
for(auto to : g[v]){
if(to != pr){
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(!upper(up[i][a], b)){
a = up[i][a];
}
}
a = up[0][a];
return a;
}
set<int > a_pos[N], lca_pos[N];
main()
{
int n, m, q, v, u;
cin >> n >> m >> q;
for(int i = 1;i <= n - 1; i++){
scanf("%d%d", &v, &u);
g[u].pb(v);
g[v].pb(u);
}
dfs(1, 1);
for(int i = 1;i <= m; i++){
scanf("%d", &a[i]);
a_pos[a[i]].insert(i);
if(i > 1){
lca_pos[lca(a[i - 1], a[i])].insert(i - 1);
}
}
int type, l, r, x;
while(q--){
scanf("%d", &type);
if(type == 1){
scanf("%d%d", &x, &v);
a_pos[a[x]].erase(x);
a_pos[v].insert(x);
if(x > 1){
lca_pos[lca(a[x - 1], a[x])].erase(x - 1);
lca_pos[lca(a[x - 1], v)].insert(x - 1);
}
if(x < m){
lca_pos[lca(a[x], a[x + 1])].erase(x);
lca_pos[lca(a[x + 1], v)].insert(x);
}
a[x] = v;
}
else{
scanf("%d%d%d",&l, &r, &v);
auto it = a_pos[v].lower_bound(l);
if(it != a_pos[v].end() && *it <= r){
printf("%d %d\n", *it, *it);
continue;
}
it = lca_pos[v].lower_bound(l);
if(r > l && it != lca_pos[v].end() && *it < r){
printf("%d %d\n", *it, *it + 1);
continue;
}
printf("%d %d\n", -1, -1);
}
}
}
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... |