답안 #1063202

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1063202 2024-08-17T15:13:52 Z aufan Birthday gift (IZhO18_treearray) C++17
0 / 100
0 ms 348 KB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

int32_t main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        
        int n, m, q;
        cin >> n >> m >> q;

        vector<vector<int>> v(n + 1);
        for (int i = 0; i < n - 1; i++) {
                int a, b;
                cin >> a >> b;

                v[a].push_back(b);
                v[b].push_back(a);
        }

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

        int tim = 0;
        vector<int> tin(n + 1), tout(n + 1);
        vector<vector<int>> p(n + 1, vector<int>(20));

        function<void(int, int)> dfs = [&](int x, int pr) {
                tin[x] = ++tim;
                p[x][0] = pr;
                for (int j = 1; j < 20; j++) p[x][j] = p[p[x][j - 1]][j - 1];

                for (auto z : v[x]) {
                        if (z == pr) continue;

                        dfs(z, x);
                }

                tout[x] = tim;
        };

        dfs(1, 1);

        auto upper = [&](int x, int y) {
                return tin[x] <= tin[y] && tout[y] <= tout[x];
        };

        auto lca = [&](int x, int y) {
                if (upper(x, y)) return x;
                if (upper(y, x)) return y;

                for (int j = 19; j >= 0; j--) {
                        if (!upper(p[x][j], y)) {
                                x = p[x][j];
                        }
                }

                return p[x][0];
        };

        vector<set<int>> pos(n + 1), st(n + 1);
        for (int i = 1; i <= m; i++) pos[a[i]].insert(i);
        for (int i = 1; i <= m - 1; i++) {
                st[lca(a[i], a[i + 1])].insert(i);
        }

        for (int i = 0; i < q; i++) {
                int t;
                cin >> t;

                if (t == 1) {
                        int x, y;
                        cin >> x >> y;

                        pos[a[x]].erase(x);

                        if (x - 1 >= 1) {
                                st[lca(a[x - 1], a[x])].erase(x - 1);
                        }

                        if (x + 1 <= m) {
                                st[lca(a[x], a[x + 1])].erase(x);
                        }

                        a[x] = y;

                        pos[a[x]].insert(x);

                        if (x - 1 >= 1) {
                                st[lca(a[x - 1], a[x])].insert(x - 1);
                        }

                        if (x + 1 <= m) {
                                st[lca(a[x], a[x + 1])].insert(x);
                        }
                } else if (t == 2) {
                        int l, r, x;
                        cin >> l >> r >> x;

                        auto it = pos[x].lower_bound(l);
                        auto it2 = st[x].lower_bound(l);
                        if (it != pos[x].end() && *it <= r) {
                                cout << *it << " " << *it << '\n';
                        } else if (it2 != st[x].end() && *it <= r - 1) {
                                cout << *it2 << " " << *it2 + 1 << '\n';
                        } else {
                                cout << -1 << " " << -1 << '\n';
                        }
                }
        }
        
        return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB n=5
2 Incorrect 0 ms 348 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB n=5
2 Incorrect 0 ms 348 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB n=5
2 Incorrect 0 ms 348 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB n=5
2 Incorrect 0 ms 348 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -