이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const int MAXN=200005,LOG=18;
vector<int> adj[MAXN];
int depth[MAXN],pa[LOG][MAXN],a[MAXN];
set<int> sendiri[MAXN],berdua[MAXN];
void dfs(int now,int par){
for(auto nxt : adj[now]){
if(nxt==par)continue;
depth[nxt]=depth[now]+1;
pa[0][nxt]=now;
for(int i=1;i<LOG;i++)pa[i][nxt]=pa[i-1][pa[i-1][nxt]];
dfs(nxt,now);
}
}
int LCA(int u,int v){
if(depth[u]<depth[v])swap(u,v);
int diff=depth[u]-depth[v];
for(int i=LOG-1;i>=0;i--){
if(diff&(1<<i))u=pa[i][u];
}
if(u==v)return u;
for(int i=LOG-1;i>=0;i--){
if(pa[i][u]!=pa[i][v]){
u=pa[i][u];
v=pa[i][v];
}
}
return pa[0][u];
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,m,q,op,x,y,u,v;
cin >> n >> m >> q;
for(int i=1;i<n;i++){
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1,-1);
for(int i=1;i<=m;i++){
cin >> a[i];
sendiri[a[i]].insert(i);
if(i>=2){
berdua[LCA(a[i-1],a[i])].insert(i-1);
}
}
while(q--){
cin >> op >> x >> y;
if(op==1){
sendiri[a[x]].erase(x);
if(x>=2)berdua[LCA(a[x],a[x-1])].erase(x-1);
if(x<=n-1)berdua[LCA(a[x],a[x+1])].erase(x);
a[x]=y;
sendiri[a[x]].insert(x);
if(x>=2)berdua[LCA(a[x],a[x-1])].insert(x-1);
if(x<=n-1)berdua[LCA(a[x],a[x+1])].insert(x);
}
else{
cin >> v;
bool sudah=false;
auto it=sendiri[v].lower_bound(x);
if(it!=sendiri[v].end() && *it<=y){
cout << *it << " " << *it << '\n';
sudah=true;
}
else{
it=berdua[v].lower_bound(x);
if(it!=berdua[v].end() && *it<=y-1){
cout << *it << " " << (*it)+1 << '\n';
assert(*it>=x && *it<=y && (*it)+1>=x && (*it)+1<=y);
sudah=true;
}
}
if(!sudah){
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... |