Submission #154891

# Submission time Handle Problem Language Result Execution time Memory
154891 2019-09-25T12:46:40 Z theboatman Birthday gift (IZhO18_treearray) C++17
0 / 100
2 ms 376 KB
#include <bits/stdc++.h>

#define y1 theboatman
#define make_struct(args...) {args}

using namespace std;

vector <int> tin, tout;
vector <vector <int> > g, up;

int timer;
void dfs(int v, int from) {
    tin[v] = timer++;

    up[v][0] = from;
    for(int i = 1; i < 20; i++) {
        up[v][i] = up[up[v][i - 1]][i - 1];
    }

    for(auto to : g[v]) {
        if (to != from) {
            dfs(to, v);
        }
    }

    tout[v] = timer++;
}

bool upper(int a, int b) {
    return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}

int lca(int a, int b) {
    if (upper(a, b)) {
        return a;
    }

    if (upper(a, b)) {
        return b;
    }

    for(int i = 19; i > -1; i--) {
        if (!upper(up[a][i], b)) {
            a = up[a][i];
        }
    }

    return up[a][0];
}

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

    int n, m, q;
    cin >> n >> m >> q;

    g.resize(n + 1);
    for(int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;

        g[x].push_back(y);
        g[y].push_back(x);
    }

    vector <int> a(m + 1);
    for(int i = 1; i <= m; i++) {
        cin >> a[i];
    }

    tin.resize(n + 1);
    tout.resize(n + 1);
    up.resize(n + 1, vector <int> (20, 1));

    dfs(1, 1);

    for(int it = 0; it < q; it++) {
        int type;
        cin >> type;

        if (type == 1) {
            int pos, v;
            cin >> pos >> v;

            a[pos] = v;
        }
        else {
            int l, r, v;
            cin >> l >> r >> v;

            int L = -1, R = -1;


            int mn = tin[v], mx = tout[v];
            for(int i = l; i <= r; i++) {
                if (mn <= tin[a[i]] && tin[a[i]] <= mx) {
                    int start = i;
                    while(i <= r && mn <= tin[a[i]] && tin[a[i]] <= mx) {
                        i++;
                    }
                    i--;

                    if (lca(a[start], a[i]) == v) {
                        L = start, R = i;
                    }
                }
            }

            cout << L << " " << R << "\n";
        }
    }

    return 0;
}
/*
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=5
2 Incorrect 2 ms 376 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=5
2 Incorrect 2 ms 376 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=5
2 Incorrect 2 ms 376 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=5
2 Incorrect 2 ms 376 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -