이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,bmi,bmi2,lzcnt,popcnt")
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
int n, m, q, depth[maxn], up[maxn][LOG];
vector<int> graph[maxn], val(maxn);
void dfs(int u, int p) {
for(int i=1; i<LOG; i++) up[u][i] = up[up[u][i-1]][i-1];
for(int &v : graph[u]) {
if(v == p) continue;
up[v][0] = u;
depth[v] = depth[u] + 1;
dfs(v, u);
}
}
int jmp(int x, int d) {
for(int j=LOG-1; j>=0; j--)
if(d & (1 << j)) x = up[x][j];
return x;
}
int get_lca(int a, int b) {
if(a == 1e9) return b;
if(b == 1e9) return a;
if(depth[a] < depth[b]) swap(a, b);
a = jmp(a, depth[a] - depth[b]);
if(a == b) return a;
for(int j=LOG-1; j>=0; j--)
if(up[a][j] != up[b][j])
a = up[a][j], b = up[b][j];
return up[a][0];
}
int32_t main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
cin >> n >> m >> q;
for(int i=0; i<n-1; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(1, 0);
for(int i=1; i<=m; i++) {
cin >> val[i];
}
while(q--) {
int t;
cin >> t;
if(t == 1) {
int p, v;
cin >> p >> v;
val[p] = v;
} else {
int l, r, v;
cin >> l >> r >> v;
bool ok = 0;
for(int i=l; i<=r; i++) {
if(val[i] == v && !ok) {
ok = 1;
cout << i << " " << i << '\n';
}
if(i + 1 <= r && get_lca(val[i], val[i+1]) == v && !ok) {
ok = 1;
cout << i << " " << i + 1 << '\n';
}
}
if(!ok) 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... |