# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1127205 | guessme | Birthday gift (IZhO18_treearray) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define RRH(v) v.resize(unique(all(v)) - v.begin())
using namespace std;
const int N = 1e6+7;
const int M = 1e9+7;
const ll oo = 3e18;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long GetRandom(long long l, long long r) {
return uniform_int_distribution<long long> (l, r)(rng);
}
int n, m, q, a[N];
vector <int> g[N];
int h[N], par[20][N];
int timedfs = 0, tin[N], tout[N];
bool isAnc(int u, int v) {
return ((tin[u] >= tin[v]) && (tin[u] <= tout[v]));
}
void DFS(int u) {
tin[u] = ++timedfs;
for (int v: g[u]) {
if (v == par[0][u]) continue;
par[0][v] = u;
h[v] = h[u] + 1;
DFS(v);
}
tout[u] = timedfs;
}
int LCA(int u, int v) {
if (h[u] < h[v]) swap(u, v);
for (int i = h[u] - h[v]; i; i -= i& - i) {
u = par[__builtin_ctz(i)][u];
}
// cout << u << " " << v << '\n';
if (u == v) return u;
for (int i = __lg(h[u]); i >= 0; i --) {
if (par[i][u] != par[i][v]) u = par[i][u], v = par[i][v];
}
return par[0][v];
}
set <int> s[N], s2[N];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
#define TASK ""
if (fopen (".inp", "r")) {
freopen (".inp", "r", stdin);
freopen (".out", "w", stdout);
}
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> m >> q;
For (i, 1, n - 1) {
int x, y; cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
DFS(1);
for (int j = 1; (1 << j) <= n; j++) For (i, 1, n) par[j][i] = par[j - 1][par[j - 1][i]];
For (i, 1, m) cin >> a[i];
For (i, 1, m) {
s[a[i]].insert(i);
if (i < m) s[LCA(a[i], a[i + 1])].insert(i);
}
while (q--) {
int t; cin >> t;
if (t == 2) {
bool ok = 0;
int l, r, v; cin >> l >> r >> v;
auto it = s[v].lower_bound(l);
if (it != s[v].end()) {
if (*it >= l && *it <= r) {
cout << *it << " " << *it << '\n';
ok = 1;
}
}
if (ok == 1) continue;
auto it = s2[v].lower_bound(l);
if (it != s2[v].end()) {
if (*it + 1 <= r) {
cout << *it << " " << *it + 1 << '\n';
ok = 1;
}
}
if (ok == 0) cout << -1 << ' ' << -1 << '\n';
}
else {
int pos, v; cin >> pos >> v;
a[pos] = v;
}
}
cerr << "Time elapsed: " << TIME << " s.\n";
return 0;
}