답안 #1113706

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1113706 2024-11-17T07:54:01 Z Icelast Birthday gift (IZhO18_treearray) C++17
100 / 100
656 ms 142864 KB
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
struct Tree{
    int n, root;
    vector<int> depth, head, sz, pa;
    vector<int> f;
    Tree(int n, int root, vector<vector<int>> &adj): n(n), root(root){
        depth.resize(n+1, -1);
        head.resize(n+1);
        sz.resize(n+1, 0);
        pa.resize(n+1, -1);
        auto rootTree = [&](auto rootTree, int u, int p) -> void{
            depth[u] = depth[p]+1;
            pa[u] = p;
            sz[u] = 1;
            for(int v : adj[u]){
                if(v == p) continue;
                rootTree(rootTree, v, u);
                sz[u] += sz[v];
            }
        };
        rootTree(rootTree, root, 0);
        auto decompose = [&](auto decompose, int u, int h) -> void{
            head[u] = h;
            int heavy = -1;
            for(int v : adj[u]){
                if(v == pa[u]) continue;
                if(heavy == -1 || sz[heavy] < sz[v]) heavy = v;
            }
            if(heavy != -1) decompose(decompose, heavy, h);
            for(int v : adj[u]){
                if(v == pa[u] || v == heavy) continue;
                decompose(decompose, v, v);
            }
        };
        decompose(decompose, root, root);
        f = [&]{
            vector<array<int, 2>> first(n+1);
            vector<array<int, 2>> second(n+1);
            function<void(int, int)> dfs0 = [&](int u, int p) {
                first[u] = second[u] = {0, -1};
                for (int v : adj[u]) {
                    if (v == p) {
                        continue;
                    }
                    dfs0(v, u);
                    auto fetch = first[v];
                    fetch[0] += 1;
                    fetch[1] = v;
                    if (fetch > first[u]) {
                        swap(fetch, first[u]);
                    }
                    if (fetch > second[u]) {
                        swap(fetch, second[u]);
                    }
                }
            };
            dfs0(1, 0);

            function<void(int, int)> dfs = [&](int u, int p) {
                for (int v : adj[u]) {
                    if (v == p) {
                        continue;
                    }
                    auto fetch = first[u][1] == v ? second[u] : first[u];
                    fetch[0] += 1;
                    fetch[1] = u;
                    if (fetch > first[v]) {
                        swap(fetch, first[v]);
                    }
                    if (fetch > second[v]) {
                        swap(fetch, second[v]);
                    }
                    dfs(v, u);
                }
            };
            dfs(1, 0);
            vector<int> f(n+1);
            for (int u = 1; u <= n; u++) {
                f[u] = first[u][0];
            }
            return f;
        }();
    };
    int lca(int u, int v){
        for(; head[u] != head[v]; v = pa[head[v]]){
            if(depth[head[u]] > depth[head[v]]) swap(u, v);
        }
        if(depth[u] > depth[v]) swap(u, v);
        return u;
    }
    int dist(int u, int v) {
        return depth[u]+depth[v]-2*depth[lca(u, v)];
    }
};
void solve(){
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> adj(n+1);
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    vector<int> a(m+1);
    for(int i = 1; i <= m; i++){
        cin >> a[i];
    }
    vector<set<int>> s(n+1), s1(n+1);
    Tree Y(n, 1, adj);
    for(int i = 1; i <= m; i++){
        s1[a[i]].insert(i);
        if(i < m){
            int lca = Y.lca(a[i], a[i+1]);
            s[lca].insert(i);
        }
    }
    for(int i = 1; i <= q; i++){
        int t, p, v, l, r;
        cin >> t;
        if(t == 1){
            cin >> p >> v;
            s1[a[p]].erase(p);
            s1[v].insert(p);
            if(p > 1){
                s[Y.lca(a[p-1], a[p])].erase(p-1);
                s[Y.lca(a[p-1], v)].insert(p-1);
            }
            if(p < m){
                s[Y.lca(a[p+1], a[p])].erase(p);
                s[Y.lca(a[p+1], v)].insert(p);
            }
            a[p] = v;
        }else{
            cin >> l >> r >> v;
            auto one = s1[v].lower_bound(l);
            auto two = s[v].lower_bound(l);
            if(one != s1[v].end() && *one <= r){
                cout << *one << " " << *one << "\n";
            }else if(two != s[v].end() && *two < r){
                cout << *two << " " << *two+1 << "\n";
            }else{
                cout << "-1 -1\n";
            }
        }
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 2 ms 336 KB n=100
9 Correct 1 ms 336 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 336 KB n=100
14 Correct 1 ms 592 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 504 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 508 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 2 ms 336 KB n=100
26 Correct 1 ms 504 KB n=12
27 Correct 1 ms 336 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 2 ms 336 KB n=100
9 Correct 1 ms 336 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 336 KB n=100
14 Correct 1 ms 592 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 504 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 508 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 2 ms 336 KB n=100
26 Correct 1 ms 504 KB n=12
27 Correct 1 ms 336 KB n=100
28 Correct 1 ms 592 KB n=500
29 Correct 1 ms 592 KB n=500
30 Correct 2 ms 756 KB n=500
31 Correct 1 ms 592 KB n=500
32 Correct 2 ms 592 KB n=500
33 Correct 1 ms 760 KB n=500
34 Correct 2 ms 592 KB n=500
35 Correct 1 ms 592 KB n=500
36 Correct 2 ms 844 KB n=500
37 Correct 2 ms 592 KB n=500
38 Correct 1 ms 592 KB n=500
39 Correct 2 ms 592 KB n=500
40 Correct 1 ms 592 KB n=500
41 Correct 2 ms 592 KB n=500
42 Correct 2 ms 592 KB n=500
43 Correct 2 ms 468 KB n=500
44 Correct 1 ms 592 KB n=500
45 Correct 1 ms 592 KB n=500
46 Correct 1 ms 592 KB n=500
47 Correct 1 ms 592 KB n=500
48 Correct 1 ms 760 KB n=500
49 Correct 1 ms 592 KB n=500
50 Correct 1 ms 764 KB n=500
51 Correct 1 ms 592 KB n=500
52 Correct 1 ms 592 KB n=500
53 Correct 1 ms 592 KB n=500
54 Correct 1 ms 592 KB n=500
55 Correct 1 ms 592 KB n=278
56 Correct 1 ms 592 KB n=500
57 Correct 1 ms 760 KB n=500
58 Correct 1 ms 592 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 2 ms 336 KB n=100
9 Correct 1 ms 336 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 336 KB n=100
14 Correct 1 ms 592 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 504 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 508 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 2 ms 336 KB n=100
26 Correct 1 ms 504 KB n=12
27 Correct 1 ms 336 KB n=100
28 Correct 1 ms 592 KB n=500
29 Correct 1 ms 592 KB n=500
30 Correct 2 ms 756 KB n=500
31 Correct 1 ms 592 KB n=500
32 Correct 2 ms 592 KB n=500
33 Correct 1 ms 760 KB n=500
34 Correct 2 ms 592 KB n=500
35 Correct 1 ms 592 KB n=500
36 Correct 2 ms 844 KB n=500
37 Correct 2 ms 592 KB n=500
38 Correct 1 ms 592 KB n=500
39 Correct 2 ms 592 KB n=500
40 Correct 1 ms 592 KB n=500
41 Correct 2 ms 592 KB n=500
42 Correct 2 ms 592 KB n=500
43 Correct 2 ms 468 KB n=500
44 Correct 1 ms 592 KB n=500
45 Correct 1 ms 592 KB n=500
46 Correct 1 ms 592 KB n=500
47 Correct 1 ms 592 KB n=500
48 Correct 1 ms 760 KB n=500
49 Correct 1 ms 592 KB n=500
50 Correct 1 ms 764 KB n=500
51 Correct 1 ms 592 KB n=500
52 Correct 1 ms 592 KB n=500
53 Correct 1 ms 592 KB n=500
54 Correct 1 ms 592 KB n=500
55 Correct 1 ms 592 KB n=278
56 Correct 1 ms 592 KB n=500
57 Correct 1 ms 760 KB n=500
58 Correct 1 ms 592 KB n=500
59 Correct 4 ms 848 KB n=2000
60 Correct 4 ms 1616 KB n=2000
61 Correct 3 ms 1528 KB n=2000
62 Correct 4 ms 1104 KB n=2000
63 Correct 3 ms 848 KB n=2000
64 Correct 3 ms 1360 KB n=2000
65 Correct 3 ms 848 KB n=2000
66 Correct 3 ms 1616 KB n=2000
67 Correct 4 ms 1020 KB n=2000
68 Correct 4 ms 1360 KB n=2000
69 Correct 3 ms 848 KB n=2000
70 Correct 3 ms 1028 KB n=2000
71 Correct 3 ms 848 KB n=2000
72 Correct 2 ms 848 KB n=2000
73 Correct 3 ms 860 KB n=2000
74 Correct 3 ms 848 KB n=1844
75 Correct 3 ms 848 KB n=2000
76 Correct 3 ms 848 KB n=2000
77 Correct 4 ms 848 KB n=2000
78 Correct 4 ms 848 KB n=2000
79 Correct 4 ms 848 KB n=2000
80 Correct 4 ms 1616 KB n=2000
81 Correct 4 ms 1532 KB n=2000
82 Correct 3 ms 848 KB n=2000
83 Correct 4 ms 1360 KB n=2000
84 Correct 3 ms 848 KB n=2000
85 Correct 4 ms 1360 KB n=2000
86 Correct 3 ms 1104 KB n=2000
87 Correct 3 ms 848 KB n=2000
88 Correct 4 ms 1872 KB n=2000
89 Correct 5 ms 1616 KB n=2000
90 Correct 5 ms 1812 KB n=2000
91 Correct 4 ms 848 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 2 ms 336 KB n=100
9 Correct 1 ms 336 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 336 KB n=100
14 Correct 1 ms 592 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 504 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 508 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 2 ms 336 KB n=100
26 Correct 1 ms 504 KB n=12
27 Correct 1 ms 336 KB n=100
28 Correct 1 ms 592 KB n=500
29 Correct 1 ms 592 KB n=500
30 Correct 2 ms 756 KB n=500
31 Correct 1 ms 592 KB n=500
32 Correct 2 ms 592 KB n=500
33 Correct 1 ms 760 KB n=500
34 Correct 2 ms 592 KB n=500
35 Correct 1 ms 592 KB n=500
36 Correct 2 ms 844 KB n=500
37 Correct 2 ms 592 KB n=500
38 Correct 1 ms 592 KB n=500
39 Correct 2 ms 592 KB n=500
40 Correct 1 ms 592 KB n=500
41 Correct 2 ms 592 KB n=500
42 Correct 2 ms 592 KB n=500
43 Correct 2 ms 468 KB n=500
44 Correct 1 ms 592 KB n=500
45 Correct 1 ms 592 KB n=500
46 Correct 1 ms 592 KB n=500
47 Correct 1 ms 592 KB n=500
48 Correct 1 ms 760 KB n=500
49 Correct 1 ms 592 KB n=500
50 Correct 1 ms 764 KB n=500
51 Correct 1 ms 592 KB n=500
52 Correct 1 ms 592 KB n=500
53 Correct 1 ms 592 KB n=500
54 Correct 1 ms 592 KB n=500
55 Correct 1 ms 592 KB n=278
56 Correct 1 ms 592 KB n=500
57 Correct 1 ms 760 KB n=500
58 Correct 1 ms 592 KB n=500
59 Correct 4 ms 848 KB n=2000
60 Correct 4 ms 1616 KB n=2000
61 Correct 3 ms 1528 KB n=2000
62 Correct 4 ms 1104 KB n=2000
63 Correct 3 ms 848 KB n=2000
64 Correct 3 ms 1360 KB n=2000
65 Correct 3 ms 848 KB n=2000
66 Correct 3 ms 1616 KB n=2000
67 Correct 4 ms 1020 KB n=2000
68 Correct 4 ms 1360 KB n=2000
69 Correct 3 ms 848 KB n=2000
70 Correct 3 ms 1028 KB n=2000
71 Correct 3 ms 848 KB n=2000
72 Correct 2 ms 848 KB n=2000
73 Correct 3 ms 860 KB n=2000
74 Correct 3 ms 848 KB n=1844
75 Correct 3 ms 848 KB n=2000
76 Correct 3 ms 848 KB n=2000
77 Correct 4 ms 848 KB n=2000
78 Correct 4 ms 848 KB n=2000
79 Correct 4 ms 848 KB n=2000
80 Correct 4 ms 1616 KB n=2000
81 Correct 4 ms 1532 KB n=2000
82 Correct 3 ms 848 KB n=2000
83 Correct 4 ms 1360 KB n=2000
84 Correct 3 ms 848 KB n=2000
85 Correct 4 ms 1360 KB n=2000
86 Correct 3 ms 1104 KB n=2000
87 Correct 3 ms 848 KB n=2000
88 Correct 4 ms 1872 KB n=2000
89 Correct 5 ms 1616 KB n=2000
90 Correct 5 ms 1812 KB n=2000
91 Correct 4 ms 848 KB n=2000
92 Correct 445 ms 62112 KB n=200000
93 Correct 499 ms 102496 KB n=200000
94 Correct 532 ms 131552 KB n=200000
95 Correct 440 ms 61832 KB n=200000
96 Correct 416 ms 61952 KB n=200000
97 Correct 432 ms 94620 KB n=200000
98 Correct 443 ms 61932 KB n=200000
99 Correct 656 ms 62224 KB n=200000
100 Correct 519 ms 61980 KB n=200000
101 Correct 464 ms 142436 KB n=200000
102 Correct 355 ms 63120 KB n=200000
103 Correct 338 ms 62992 KB n=200000
104 Correct 351 ms 62992 KB n=200000
105 Correct 329 ms 63504 KB n=200000
106 Correct 337 ms 63504 KB n=200000
107 Correct 340 ms 63392 KB n=200000
108 Correct 569 ms 61968 KB n=200000
109 Correct 561 ms 61968 KB n=200000
110 Correct 546 ms 61992 KB n=200000
111 Correct 482 ms 61332 KB n=200000
112 Correct 469 ms 133904 KB n=200000
113 Correct 581 ms 94476 KB n=200000
114 Correct 455 ms 61540 KB n=200000
115 Correct 531 ms 76268 KB n=200000
116 Correct 414 ms 62084 KB n=200000
117 Correct 500 ms 138512 KB n=200000
118 Correct 495 ms 85936 KB n=200000
119 Correct 379 ms 62028 KB n=200000
120 Correct 426 ms 142636 KB n=200000
121 Correct 412 ms 142568 KB n=200000
122 Correct 430 ms 142864 KB n=200000
123 Correct 314 ms 63248 KB n=200000
124 Correct 104 ms 24356 KB n=25264