Submission #1325766

#TimeUsernameProblemLanguageResultExecution timeMemory
1325766sh_qaxxorov_571Birthday gift (IZhO18_treearray)C++20
0 / 100
1 ms332 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 200005;
const int LOGN = 20;

vector<int> adj[MAXN];
int up[MAXN][LOGN], tin[MAXN], tout[MAXN], timer;
int a[MAXN], n, m, q;

// 1. Daraxtni aylanib chiqish va Binary Lifting uchun jadval tuzish
void dfs(int v, int p) {
    tin[v] = ++timer;
    up[v][0] = p;
    for (int i = 1; i < LOGN; i++)
        up[v][i] = up[up[v][i - 1]][i - 1];

    for (int u : adj[v]) {
        if (u != p) dfs(u, v);
    }
    tout[v] = timer;
}

// v tugun u ning ajdodimi?
bool is_ancestor(int v, int u) {
    return tin[v] <= tin[u] && tout[v] >= tout[u];
}

// Ikki tugun uchun LCA topish
int get_lca(int u, int v) {
    if (u == 0) return v;
    if (v == 0) return u;
    if (is_ancestor(u, v)) return u;
    if (is_ancestor(v, u)) return v;
    for (int i = LOGN - 1; i >= 0; i--) {
        if (!is_ancestor(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}

// 2. Segment Tree: Berilgan oraliqdagi barcha elementlarning LCA sini saqlaydi
int tree[4 * MAXN];

void build(int node, int start, int end) {
    if (start == end) {
        tree[node] = a[start];
        return;
    }
    int mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);
    tree[node] = get_lca(tree[2 * node], tree[2 * node + 1]);
}

void update(int node, int start, int end, int idx, int val) {
    if (start == end) {
        tree[node] = val;
        a[start] = val;
        return;
    }
    int mid = (start + end) / 2;
    if (idx <= mid) update(2 * node, start, mid, idx, val);
    else update(2 * node + 1, mid + 1, end, idx, val);
    tree[node] = get_lca(tree[2 * node], tree[2 * node + 1]);
}

int query_lca(int node, int start, int end, int l, int r) {
    if (r < start || end < l) return 0;
    if (l <= start && end <= r) return tree[node];
    int mid = (start + end) / 2;
    return get_lca(query_lca(2 * node, start, mid, l, r),
                   query_lca(2 * node + 1, mid + 1, end, l, r));
}

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

    cin >> n >> m >> q;
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs(1, 1);

    for (int i = 1; i <= m; i++) cin >> a[i];
    build(1, 1, m);

    while (q--) {
        int type; cin >> type;
        if (type == 1) {
            int pos, v; cin >> pos >> v;
            update(1, 1, m, pos, v);
        } else {
            int l, r, v; cin >> l >> r, v;
            bool found = false;
            for(int i = l; i <= r && !found; i++) {
                int current_lca = 0;
                for(int j = i; j <= r; j++) {
                    current_lca = get_lca(current_lca, a[j]);
                    if (current_lca == v) {
                        cout << i << " " << j << "\n";
                        found = true; break;
                    }
                    if (is_ancestor(current_lca, v) && current_lca != v) break;
                }
            }
            if (!found) cout << "-1 -1\n";
        }
    }
    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...