Submission #1370074

#TimeUsernameProblemLanguageResultExecution timeMemory
1370074kawhietBirthday gift (IZhO18_treearray)C++20
100 / 100
447 ms76596 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 47
#endif

constexpr int N = 2e5 + 1;

vector<int> g[N];
int to[N][20], lvl[N];

void init(int u, int p) {
    lvl[u] = lvl[p] + 1;
    to[u][0] = p;
    for (int i = 1; i < 20; i++) {
        to[u][i] = to[to[u][i - 1]][i - 1];
    }
    for (int v : g[u]) {
        if (v == p) continue;
        init(v, u);
    }
}

int lca(int x, int y) {
    if (lvl[x] < lvl[y]) swap(x, y);
    int d = lvl[x] - lvl[y];
    for (int i = 0; i < 20; i++) {
        if (d & (1 << i)) {
            x = to[x][i];
        }
    }
    if (x == y) return x;
    for (int i = 19; i >= 0; i--) {
        if (to[x][i] != to[y][i]) {
            x = to[x][i];
            y = to[y][i];
        }
    }
    return to[x][0];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    init(1, 0);
    vector<int> a(m + 1);
    for (int i = 1; i <= m; i++) {
        cin >> a[i];
    }
    vector<set<int>> t1(n + 1), t2(n + 1);
    for (int i = 1; i <= m; i++) {
        t1[a[i]].insert(i);
    }
    for (int i = 1; i < m; i++) {
        int x = lca(a[i], a[i + 1]);
        t2[x].insert(i);
    }
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            int pos, v;
            cin >> pos >> v;
            t1[a[pos]].erase(pos);
            if (pos >= 2) {
                int x = lca(a[pos], a[pos - 1]);
                t2[x].erase(pos - 1);
            }
            if (pos < m) {
                int x = lca(a[pos], a[pos + 1]);
                t2[x].erase(pos);
            }
            a[pos] = v;
            t1[a[pos]].insert(pos);
            if (pos >= 2) {
                int x = lca(a[pos], a[pos - 1]);
                t2[x].insert(pos - 1);
            }
            if (pos < m) {
                int x = lca(a[pos], a[pos + 1]);
                t2[x].insert(pos);
            }
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            auto it = t1[v].lower_bound(l);
            if (it != t1[v].end() && *it <= r) {
                cout << *it << ' ' << *it << '\n';
                continue;
            }
            it = t2[v].lower_bound(l);
            if (it != t2[v].end() && *it < r) {
                cout << *it << ' ' << *it + 1 << '\n';
                continue;
            }
            cout << -1 << ' ' << -1 << '\n';
        }
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...