Submission #155258

#TimeUsernameProblemLanguageResultExecution timeMemory
155258srvltBirthday gift (IZhO18_treearray)C++14
56 / 100
379 ms892 KiB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse2,avx")
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
//#define int long long
using namespace std;

void dout() {
    cerr << endl;
}

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
    cerr << H << ' ';
    dout(T...);
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2003, K = 12;
int n, m, k, a[N], tin[N], tout[N], timer, up[K][N], h[N];
vector <int> q[N];

void dfs(int v, int p = 1) {
    if (v != 1) {
        h[v] = h[p] + 1;
    }
    tin[v] = ++timer;
    up[0][v] = p;
    for (int i = 1; i < K; i++) {
        up[i][v] = up[i - 1][up[i - 1][v]];
    }
    for (auto i : q[v]) {
        if (i == p) {
            continue;
        }
        dfs(i, v);
    }
    tout[v] = timer;
}

int w(int x, int y) {
    assert(tin[x] >= tin[y] && tout[x] <= tout[y]);
    for (int i = K - 1; i >= 0; i--) {
        if (h[up[i][x]] > h[y]) {
            x = up[i][x];
        }
    }
    return x;
}

void output(int x, int y) {
    if (x > y) {
        swap(x, y);
    }
    cout << x << " " << y << endl;
}

void getans(int l, int r, int v) {
    int x = -1, x2 = -1, y = -1, y2 = -1;
    for (int j = l; j <= r; j++) {
        int i = a[j];
        if (tin[i] < tin[v] || tout[i] > tout[v]) {
            if (a[x] == v && a[y] == v) {
                output(x, y);
                return;
            }
            if (a[x2] == v && a[y2] == v) {
                output(x2, y2);
                return;
            }
            if (x != -1 && y != -1) {
                if (tin[a[y]] < tin[w(a[x], v)]) {
                    output(x, y);
                    return;
                }
            }
            if (x2 != -1 && y2 != -1) {
                if (tout[a[y2]] > tout[w(a[x2], v)]) {
                    output(x2, y2);
                    return;
                }
            }
            x = -1, x2 = -1, y = -1, y2 = -1;
        }   else {
            if (y == -1 || tin[a[y]] > tin[i]) {
                y = j;
            }
            if (y2 == -1 || tout[a[y2]] < tout[i]) {
                y2 = j;
            }
            if (x == -1 || tin[w(a[x], v)] < tin[w(i, v)]) {
                x = j;
            }
            if (x2 == -1 || tout[w(a[x2], v)] > tout[w(i, v)]) {
                x2 = j;
            }
        }
    }

    if (a[x] == v && a[y] == v) {
        output(x, y);
        return;
    }
    if (a[x2] == v && a[y2] == v) {
        output(x2, y2);
        return;
    }
    if (x != -1 && y != -1) {
        if (tin[a[y]] < tin[w(a[x], v)]) {
            output(x, y);
            return;
        }
    }
    if (x2 != -1 && y2 != -1) {
        if (tout[a[y2]] > tout[w(a[x2], v)]) {
            output(x2, y2);
            return;
        }
    }

    cout << "-1 -1" << endl;
    return;
}

void solve(int tc) {
    cin >> n >> m >> k;
    for (int i = 1; i <= n - 1; i++) {
        int x, y;
        cin >> x >> y;
        q[x].pb(y);
        q[y].pb(x);
    }
    dfs(1);
    for (int i = 1; i <= m; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= k; i++) {
        int tp;
        cin >> tp;
        if (tp == 1) {
            int x, y;
            cin >> x >> y;
            a[x] = y;
        }   else {
            int l, r, v;
            cin >> l >> r >> v;
            getans(l, r, v);
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int tc = 1;
//    cin >> tc;
    for (int i = 0; i < tc; i++) {
        solve(i);
//        cleanup();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...