이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/// tree bends in youth
/// 24 .10.2023
/// success is doing same thing in every single day!!!
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define F first
#define S second
using namespace std;
const ll N =1e5+ 5;
const ll NN =2e6 + 5;
const ll INF = -1e1;
const ll MOD = 1e9 + 7;
const ll LG = 18;
const ll k = 316;
vector <int> g[N];
int tin[N],tout[N],a[N],cnt,used[N];
int p[N][18];
void dfs(int v){
cnt++;
used[v] = 1;
tin[v] = cnt;
for(int to : g[v]){
if(used[to] == 0){
p[to][0] = v;
dfs(to);
}
}
tout[v] = cnt;
}
bool upper(int x,int y){
if(tin[x] <= tin[y] && tout[x] >= tout[y])return 1;
else return 0;
}
int lca(int x,int y){
if(upper(x,y) == 1){
return x;
}
if(upper(y,x) == 1){
return y;
}
for(int j = 16;j >= 0;j--){
int h = p[x][j];
if(upper(h,y) == 0){
x = h;
}
}
return p[x][0];
}
int get_lca(int l,int r){
if(l == r)return a[l];
int mid = (l + r) / 2;
int z = get_lca(l,mid);
int x = get_lca(mid + 1,r);
return lca(z,x);
}
void solve(){
int n,m,q;
cin >> n >> m >> q;
for(int i = 1;i < n;i++){
int u,v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
p[1][0] = 1;
dfs(1);
for(int i = 1;i <= 16;i++){
for(int j = 1;j <= n;j++){
p[j][i] = p[p[j][i - 1]][i - 1];
}
}
for(int i = 1;i <= m;i++){
cin >> a[i];
}
bool was = 1;
while(q--){
int tp,l,r,x;
cin >> tp >> l >> r;
if(tp == 1){
a[l] = r;
}
else{
cin >> x;
bool ok = 0;
for(int i = l;i <= r;i++){
int ql = i,qr = r;
while(ql < qr){
int mid = (ql + qr + 1) / 2;
int lc = get_lca(ql,mid);
if(upper(x,lc) == 1)ql = mid;
else qr = mid - 1;
}
if(get_lca(l,qr) == x){
cout << l << ' ' << qr << '\n';
ok = 1;
break;
}
}
if(!ok)cout << "-1 -1\n";
}
}
}
main (){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll abd= 1;
// cin >> abd;
for(ll i = 1;i <= abd;i++){
// cout << "Case " << i << ":\n";
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
treearray.cpp: In function 'void solve()':
treearray.cpp:79:10: warning: unused variable 'was' [-Wunused-variable]
79 | bool was = 1;
| ^~~
treearray.cpp: At global scope:
treearray.cpp:108:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
108 | main (){
| ^~~~
# | 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... |