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>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
using namespace std;
#pragma GCC optimize ("unroll-loops,Ofast,O3")
#pragma GCC target("avx,avx2,fma")
#define F first
#define S second
#define sz(x) (int)x.size()
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define NFS ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
const int E = 1e6+777;
const long long inf = 1e18+777;
const int N = 2e5+77;
const int MOD = 1e9+7;
const int LG = 18;
int n, m, que, a[N], timer;
int tin[N], tout[N], up[N][LG+2];
vector<int> g[N];
set<int> st[N], o[N];
void dfs(int v, int p){
tin[v] = ++timer;
up[v][0] = p;
for(int i = 1; i <= LG; ++i){
up[v][i] = up[up[v][i-1]][i-1];
}
for(int to : g[v]){
if(to != p){
dfs(to, v);
}
}
tout[v] = timer;
}
bool upper(int v, int u){
return tin[v] <= tin[u] && tout[u] <= tout[v];
}
int lca(int v, int u){
if(upper(v, u)) return v;
if(upper(u, v)) return u;
for(int i = LG; i >= 0; --i){
if(!upper(up[v][i], u)){
v = up[v][i];
}
}
return up[v][0];
}
inline void update(int pos, int v){
o[a[pos]].erase(pos);
if(pos < m){
if(a[pos] && a[pos+1]){
st[lca(a[pos],a[pos+1])].erase(pos+1);
}
}
if(pos > 1){
if(a[pos] && a[pos-1]){
st[lca(a[pos],a[pos-1])].erase(pos);
}
}
a[pos] = v;
o[a[pos]].insert(pos);
if(pos > 1){
if(a[pos] && a[pos-1]){
st[lca(a[pos],a[pos-1])].insert(pos);
}
}
if(pos < m){
if(a[pos] && a[pos+1]){
st[lca(a[pos],a[pos+1])].insert(pos+1);
}
}
}
inline pair<int,int> get(int L, int R, int v){
auto it = st[v].upper_bound(L);
auto it1 = o[v].lower_bound(L);
if(it1 != o[v].end() && *it1 <= R) return {*it1, *it1};
if(it == st[v].end() || *it > R) return {-1, -1};
return {*it-1, *it};
}
int main(){NFS;
cin >> n >> m >> que;
for(int i = 1, u, v; i < n; ++i){
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1, 1);
for(int i = 1, val; i <= m; ++i){
cin >> val;
update(i, val);
}
for(int type,l,r,v; que--;){
cin >> type;
if(type == 1){
cin >> l >> v;
update(l, v);
}else{
cin >> l >> r >> v;
pair<int,int> res = get(l, r, v);
cout << res.F << ' ' << res.S << '\n';
}
}
}
# | 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... |