Submission #1369004

#TimeUsernameProblemLanguageResultExecution timeMemory
1369004kawhietBirthday gift (IZhO18_treearray)C++20
12 / 100
4093 ms560 KiB
#include <bits/stdc++.h>
using namespace std;

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

constexpr int N = 2e5;

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

void init(int u, int p) {
    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;
        }
        lvl[v] = lvl[u] + 1;
        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];
}

struct SegmentTree {
    int n;
    vector<int> t;

    SegmentTree(int _n) {
        n = _n;
        t.resize(4 * n);
    }

    int merge(int x, int y) {
        if (x == -1) return y;
        if (y == -1) return x;
        return lca(x, y);
    }

    void update(int id, int tl, int tr, int i, int v) {
        if (tl == tr) {
            t[id] = v;
            return;
        }
        int x = (id << 1) + 1, y = x + 1, tm = (tl + tr) >> 1;
        if (i <= tm) {
            update(x, tl, tm, i, v);
        } else {
            update(y, tm + 1, tr, i, v);
        }
        t[id] = merge(t[x], t[y]);
    }

    int get(int id, int tl, int tr, int l, int r) {
        if (r < tl || tr < l) return -1;
        if (l <= tl && tr <= r) return t[id];
        int x = (id << 1) + 1, y = x + 1, tm = (tl + tr) >> 1;
        return merge(get(x, tl, tm, l, r), get(y, tm + 1, tr, l, r));
    }

    void update(int i, int v) { update(0, 0, n - 1, i, v); }
    int get(int l, int r) { return get(0, 0, n - 1, l, r); }
};

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;
        x--; y--;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    init(0, 0);
    vector<int> a(m);
    for (int i = 0; i < m; i++) {
        cin >> a[i];
        a[i]--;
    }
    SegmentTree t(m);
    for (int i = 0; i < m; i++) {
        t.update(i, a[i]);
    }
    while (q--) {
        int w;
        cin >> w;
        if (w == 1) {
            int pos, v;
            cin >> pos >> v;
            pos--; v--;
            t.update(pos, v);
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            l--; r--; v--;
            bool ok = false;
            for (int x = l; x <= r; x++) {
                for (int y = x; y <= r; y++) {
                    if (t.get(x, y) == v) {
                        ok = true;
                        cout << x + 1 << ' ' << y + 1 << '\n';
                        break;
                    }
                }
                if (ok) {
                    break;
                }
            }
            if (!ok) {
                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...