이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
const ll mod = 1e9 + 7,inf = 1e18;
#define pb push_back
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
void setIO()
{
fast;
}
int n = 2e5 + 5,m,q,TIMER;
vvi adj(n);
vvi anc(n,vi(21));
vi in(n),out(n);
void dfs(int u,int p)
{
in[u] = ++TIMER;
anc[u][0] = p;
for (int i = 1;i<=20;i++)anc[u][i] = anc[anc[u][i - 1]][i - 1];
for (int v:adj[u]){
if (v == p)continue;
dfs(v,u);
}
out[u] = ++TIMER;
}
bool is_ancestor(int u,int v)
{
return in[u] <= in[v] && out[u] >= out[v];
}
int LCA(int u,int v)
{
if (in[u] > in[v])swap(u,v);
if (is_ancestor(u,v))return u;
//cout<<u<<" "<<v<<endl;
for (int i = 20;i>=0;i--){
if (!is_ancestor(anc[v][i],u))v = anc[v][i];
}
return anc[v][0];
}
int main()
{
setIO();
cin>>n>>m>>q;
vi a(m + 1);
vector<set<int>>lcas(n + 1);
vector<set<int>>positions(n + 1);
for (int i = 1;i<=n - 1;i++){
int u,v;
cin>>u>>v;
adj[u].pb(v),adj[v].pb(u);
}
for (int i = 1;i<=m;i++)cin>>a[i],positions[a[i]].insert(i);
dfs(1,1);
for (int i = 1;i<=m - 1;i++)
lcas[LCA(a[i],a[i + 1])].insert(i);
// for (int i = 1;i<=n;i++){
// cout<<lcas[i].size()<<endl;
// for (auto it:lcas[i])cout<<it<<" ";
// cout<<endl;
// }
while (q--){
int type,v;
cin>>type;
if (type == 1){
int pos;
cin>>pos>>v;
if (pos >= 2)lcas[LCA(a[pos - 1],a[pos])].erase(pos - 1);
if (pos + 1 <= m)lcas[LCA(a[pos],a[pos + 1])].erase(pos);
positions[a[pos]].erase(pos);
a[pos] = v;
positions[a[pos]].insert(pos);
if (pos >= 2)lcas[LCA(a[pos - 1],a[pos])].insert(pos - 1);
if (pos + 1 <= m)lcas[LCA(a[pos],a[pos + 1])].insert(pos);
}
else{
int l,r;
cin>>l>>r>>v;
auto it = positions[v].lower_bound(l);
auto it2 = lcas[v].lower_bound(l);
if (it != positions[v].end() && *it + 1<= r)
cout<<*it<<" "<<*it + 1<<'\n';
else if (it2 != lcas[v].end() && *it + 1 <= r)
cout<<*it2<<" "<<*it2 + 1<<'\n';
else cout<<"-1 -1"<<'\n';
}
}
return 0;
}
# | 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... |