Submission #1114260

#TimeUsernameProblemLanguageResultExecution timeMemory
1114260MisterReaperBirthday gift (IZhO18_treearray)C++17
100 / 100
863 ms84404 KiB
#include <bits/stdc++.h>

using i64 = long long;

#ifdef DEBUG 
    #include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
    #define debug(...) void(23)
#endif

constexpr int max_N = int(2E5) + 5;
constexpr int LG = 20;

int N, M, Q;
std::vector<int> adj[max_N];
int par[LG][max_N], tin[max_N], tout[max_N], dep[max_N], timer;

void dfs(int v) {
    tin[v] = timer++;
    for (auto u : adj[v]) {
        adj[u].erase(std::find(adj[u].begin(), adj[u].end(), v));
        par[0][u] = v;
        dep[u] = dep[v] + 1;
        dfs(u);
    }
    tout[v] = timer;
}

bool is_in_subtree_of(int u, int v) {
    return tin[v] <= tin[u] && tout[u] <= tout[v];
}
int lca(int a, int b) {
    if (is_in_subtree_of(a, b)) {
        return b;
    } else if (is_in_subtree_of(b, a)) {
        return a;
    }
    for (int lg = LG - 1; lg >= 0; --lg) {
        if (!is_in_subtree_of(b, par[lg][a])) {
            a = par[lg][a];
        }
    }
    return par[0][a];
}

int A[max_N];
std::set<int> s[max_N], t[max_N];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> N >> M >> Q;
    for (int i = 1; i < N; ++i) {
        int X, Y;
        std::cin >> X >> Y;
        adj[X].emplace_back(Y);
        adj[Y].emplace_back(X);
    }
    for (int i = 0; i < M; ++i) {
        std::cin >> A[i];
    }

    par[0][1] = 1;
    dfs(1);

    for (int lg = 0; lg + 1 < LG; ++lg) {
        for (int i = 1; i <= N; ++i) {
            par[lg + 1][i] = par[lg][par[lg][i]];
        }
    }

    for (int i = 0; i < M; ++i) {
        s[A[i]].emplace(i);
    }
    for (int i = 0; i + 1 < M; ++i) {
        t[lca(A[i], A[i + 1])].emplace(i);
    }

    while (Q--) {
        int TP;
        std::cin >> TP;
        if (TP == 1) {
            int P, X;
            std::cin >> P >> X;
            --P;
            s[A[P]].erase(P);
            if (0 <= P - 1) {
                t[lca(A[P - 1], A[P])].erase(P - 1);
            }
            if (P + 1 < M) {
                t[lca(A[P], A[P + 1])].erase(P);
            }
            A[P] = X;
            s[A[P]].emplace(P);
            if (0 <= P - 1) {
                t[lca(A[P - 1], A[P])].emplace(P - 1);
            }
            if (P + 1 < M) {
                t[lca(A[P], A[P + 1])].emplace(P);
            }
        } else {
            int L, R, X; 
            std::cin >> L >> R >> X;
            --L, --R;
            auto it = s[X].lower_bound(L);
            if (it != s[X].end() && *it <= R) {
                std::cout << *it + 1 << ' ' << *it + 1 << '\n';
            } else {
                it = t[X].lower_bound(L);
                if (it != t[X].end() && *it + 1 <= R) {
                    std::cout << *it + 1 << ' ' << *it + 2 << '\n';
                } else {
                    std::cout << "-1 -1\n";
                }
            }
        }
        debug();
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...