제출 #378966

#제출 시각아이디문제언어결과실행 시간메모리
378966abc864197532Birthday gift (IZhO18_treearray)C++17
56 / 100
4062 ms35984 KiB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define pii pair<int, int>
#define pll pair<lli, lli>
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define test(x) cout << #x << ' ' << x << endl
#define printv(x) {\
    for (auto a : x) cout << a << ' ';\
    cout << endl;\
}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int N = 200000, logN = 17;

vector <int> adj[N];
int in[N], out[N], jump[N][logN], _t;

void dfs(int v, int pa) {
    in[v] = _t++;
    jump[v][0] = pa;
    for (int u : adj[v]) if (u != pa) {
        dfs(u, v);
    }
    out[v] = _t++;
}

bool anc(int u, int v) {
    return in[u] <= in[v] && out[u] >= out[v];
}

int lca(int u, int v) {
    if (anc(u, v)) return u;
    if (anc(v, u)) return v;
    for (int i = logN - 1; ~i; --i) {
        int k = jump[u][i];
        if (~k && !anc(k, v)) u = k;
    }
    return jump[u][0];
}


int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0, u, v; i < n - 1; ++i) {
        cin >> u >> v, --u, --v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for (int i = 0; i < logN; ++i) for (int j = 0; j < n; ++j) jump[j][i] = -1;
    dfs(0, -1);
    for (int j = 1; j < logN; ++j) {
        for (int i = 0; i < n; ++i) {
            int k = jump[i][j - 1];
            if (~k) jump[i][j] = jump[k][j - 1];
        }
    }
    vector <int> a(m);
    for (int i = 0; i < m; ++i) cin >> a[i], --a[i];
    while (q--) {
        int t, l, r, v;
        cin >> t >> l >> r, --l;
        if (t == 1) {
            a[l] = --r;
        } else {
            cin >> v, --v;
            int curlca = -1, from = -1;
            pii ans = mp(-2, -2);
            for (int i = l; i < r; ++i) {
                if (anc(v, a[i])) {
                    if (curlca == -1) {
                        from = i;
                        curlca = a[i];
                    } else {
                        curlca = lca(curlca, a[i]);
                    }
                } else {
                    curlca = from = -1;
                }
                if (curlca == v) {
                    ans = mp(from, i);
                    break;
                }
            }
            cout << ans.X + 1 << ' ' << ans.Y + 1 << '\n';
        }
    }
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...