답안 #824702

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
824702 2023-08-14T08:59:17 Z dimashhh Birthday gift (IZhO18_treearray) C++17
100 / 100
687 ms 85572 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 2e5 + 1,MOD = 998244353;

int n,m,q,a[N],up[N][20],tin[N],tout[N],timer = 0,b[N];
vector<int> g[N];
set<int> t[N],f[N];
int ans;
void dfs(int v,int pr = 1){
    tin[v] = ++timer;
    up[v][0] = pr;
    for(int i = 1;i <= 18;i++){
        up[v][i] = up[up[v][i - 1]][i - 1];
    }
    for(int to:g[v]){
        if(to != pr) dfs(to,v);
    }
    tout[v] = timer;
}
bool is(int v,int u){
    return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int v,int u){
    if(is(v,u)) return v;
    if(is(u,v)) return u;
    for(int i = 18;i >= 0;i--){
        if(!is(up[v][i],u)){
            v = up[v][i];
        }
    }
    return up[v][0];
}
void test(){
    cin >> n >> m >> q;
    for(int i = 1;i <= n - 1;i++){
        int x,y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1);
    for(int i = 1;i <= m;i++){
        cin >> a[i];
        t[a[i]].insert(i);
        if(i > 1) b[i - 1] = lca(a[i - 1],a[i]);
        f[b[i - 1]].insert(i - 1);
    }
    while(q--){
        int tp;
        cin >> tp;
        if(tp == 1){
            int pos,v;
            cin >> pos >> v;
            t[a[pos]].erase(pos);
            if(pos < m){
                int x = lca(v,a[pos + 1]);
                f[b[pos]].erase(pos);
                b[pos] = x;
                f[b[pos]].insert(pos);
            }
            if(pos > 1){
                int x = lca(a[pos - 1],v);
                f[b[pos - 1]].erase(pos - 1);                
                b[pos - 1] = x;
                f[b[pos - 1]].insert(pos - 1);                
            }
            a[pos] = v;
            t[a[pos]].insert(pos);
        }else{
            int l,r,v;
            cin >> l >> r >> v;
            if(l == r){
                if(v == a[l]){
                    cout << l << ' ' << r << '\n';
                }else{
                    cout << -1 << ' ' << -1 << '\n';
                }
                continue;
            }
            auto it = f[v].lower_bound(l);
            if(it != f[v].end() && *it < r){
                cout << *it << ' ' << *it + 1 << '\n'; 
            }else{
                auto it = t[v].lower_bound(l);
                if(it != t[v].end() && *it <= r){
                    cout << *it << ' ' << *it << '\n';
                    continue;
                }
                cout << -1 << ' ' << -1 << '\n';
            }
        }
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t = 1;
    //cin >> t;
    while(t--) test();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB n=5
2 Correct 11 ms 23764 KB n=100
3 Correct 11 ms 23760 KB n=100
4 Correct 11 ms 23764 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23784 KB n=100
7 Correct 12 ms 23764 KB n=100
8 Correct 13 ms 23764 KB n=100
9 Correct 11 ms 23852 KB n=100
10 Correct 13 ms 23808 KB n=100
11 Correct 11 ms 23852 KB n=100
12 Correct 11 ms 23784 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 11 ms 23780 KB n=100
15 Correct 11 ms 23764 KB n=100
16 Correct 11 ms 23728 KB n=100
17 Correct 11 ms 23836 KB n=100
18 Correct 11 ms 23784 KB n=100
19 Correct 11 ms 23764 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 12 ms 23764 KB n=100
22 Correct 11 ms 23764 KB n=100
23 Correct 11 ms 23796 KB n=100
24 Correct 11 ms 23764 KB n=100
25 Correct 11 ms 23764 KB n=100
26 Correct 11 ms 23764 KB n=12
27 Correct 11 ms 23792 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB n=5
2 Correct 11 ms 23764 KB n=100
3 Correct 11 ms 23760 KB n=100
4 Correct 11 ms 23764 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23784 KB n=100
7 Correct 12 ms 23764 KB n=100
8 Correct 13 ms 23764 KB n=100
9 Correct 11 ms 23852 KB n=100
10 Correct 13 ms 23808 KB n=100
11 Correct 11 ms 23852 KB n=100
12 Correct 11 ms 23784 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 11 ms 23780 KB n=100
15 Correct 11 ms 23764 KB n=100
16 Correct 11 ms 23728 KB n=100
17 Correct 11 ms 23836 KB n=100
18 Correct 11 ms 23784 KB n=100
19 Correct 11 ms 23764 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 12 ms 23764 KB n=100
22 Correct 11 ms 23764 KB n=100
23 Correct 11 ms 23796 KB n=100
24 Correct 11 ms 23764 KB n=100
25 Correct 11 ms 23764 KB n=100
26 Correct 11 ms 23764 KB n=12
27 Correct 11 ms 23792 KB n=100
28 Correct 12 ms 23936 KB n=500
29 Correct 12 ms 23892 KB n=500
30 Correct 12 ms 23948 KB n=500
31 Correct 12 ms 23892 KB n=500
32 Correct 12 ms 23936 KB n=500
33 Correct 12 ms 23892 KB n=500
34 Correct 11 ms 23880 KB n=500
35 Correct 12 ms 23956 KB n=500
36 Correct 12 ms 23944 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 13 ms 23892 KB n=500
39 Correct 11 ms 23892 KB n=500
40 Correct 12 ms 23900 KB n=500
41 Correct 12 ms 23828 KB n=500
42 Correct 12 ms 23944 KB n=500
43 Correct 12 ms 23848 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 12 ms 23892 KB n=500
46 Correct 11 ms 23892 KB n=500
47 Correct 12 ms 23956 KB n=500
48 Correct 11 ms 23892 KB n=500
49 Correct 11 ms 23892 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 12 ms 23892 KB n=500
52 Correct 12 ms 23892 KB n=500
53 Correct 12 ms 23892 KB n=500
54 Correct 11 ms 23892 KB n=500
55 Correct 11 ms 23908 KB n=278
56 Correct 11 ms 23892 KB n=500
57 Correct 12 ms 23892 KB n=500
58 Correct 12 ms 23832 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB n=5
2 Correct 11 ms 23764 KB n=100
3 Correct 11 ms 23760 KB n=100
4 Correct 11 ms 23764 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23784 KB n=100
7 Correct 12 ms 23764 KB n=100
8 Correct 13 ms 23764 KB n=100
9 Correct 11 ms 23852 KB n=100
10 Correct 13 ms 23808 KB n=100
11 Correct 11 ms 23852 KB n=100
12 Correct 11 ms 23784 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 11 ms 23780 KB n=100
15 Correct 11 ms 23764 KB n=100
16 Correct 11 ms 23728 KB n=100
17 Correct 11 ms 23836 KB n=100
18 Correct 11 ms 23784 KB n=100
19 Correct 11 ms 23764 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 12 ms 23764 KB n=100
22 Correct 11 ms 23764 KB n=100
23 Correct 11 ms 23796 KB n=100
24 Correct 11 ms 23764 KB n=100
25 Correct 11 ms 23764 KB n=100
26 Correct 11 ms 23764 KB n=12
27 Correct 11 ms 23792 KB n=100
28 Correct 12 ms 23936 KB n=500
29 Correct 12 ms 23892 KB n=500
30 Correct 12 ms 23948 KB n=500
31 Correct 12 ms 23892 KB n=500
32 Correct 12 ms 23936 KB n=500
33 Correct 12 ms 23892 KB n=500
34 Correct 11 ms 23880 KB n=500
35 Correct 12 ms 23956 KB n=500
36 Correct 12 ms 23944 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 13 ms 23892 KB n=500
39 Correct 11 ms 23892 KB n=500
40 Correct 12 ms 23900 KB n=500
41 Correct 12 ms 23828 KB n=500
42 Correct 12 ms 23944 KB n=500
43 Correct 12 ms 23848 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 12 ms 23892 KB n=500
46 Correct 11 ms 23892 KB n=500
47 Correct 12 ms 23956 KB n=500
48 Correct 11 ms 23892 KB n=500
49 Correct 11 ms 23892 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 12 ms 23892 KB n=500
52 Correct 12 ms 23892 KB n=500
53 Correct 12 ms 23892 KB n=500
54 Correct 11 ms 23892 KB n=500
55 Correct 11 ms 23908 KB n=278
56 Correct 11 ms 23892 KB n=500
57 Correct 12 ms 23892 KB n=500
58 Correct 12 ms 23832 KB n=500
59 Correct 14 ms 24276 KB n=2000
60 Correct 13 ms 24276 KB n=2000
61 Correct 14 ms 24276 KB n=2000
62 Correct 13 ms 24288 KB n=2000
63 Correct 14 ms 24284 KB n=2000
64 Correct 14 ms 24304 KB n=2000
65 Correct 14 ms 24276 KB n=2000
66 Correct 13 ms 24276 KB n=2000
67 Correct 14 ms 24148 KB n=2000
68 Correct 13 ms 24304 KB n=2000
69 Correct 13 ms 24236 KB n=2000
70 Correct 13 ms 24276 KB n=2000
71 Correct 13 ms 24276 KB n=2000
72 Correct 13 ms 24260 KB n=2000
73 Correct 13 ms 24220 KB n=2000
74 Correct 13 ms 24200 KB n=1844
75 Correct 13 ms 24208 KB n=2000
76 Correct 14 ms 24204 KB n=2000
77 Correct 14 ms 24168 KB n=2000
78 Correct 14 ms 24200 KB n=2000
79 Correct 14 ms 24280 KB n=2000
80 Correct 14 ms 24276 KB n=2000
81 Correct 15 ms 24204 KB n=2000
82 Correct 14 ms 24268 KB n=2000
83 Correct 13 ms 24332 KB n=2000
84 Correct 14 ms 24284 KB n=2000
85 Correct 14 ms 24304 KB n=2000
86 Correct 14 ms 24276 KB n=2000
87 Correct 14 ms 24276 KB n=2000
88 Correct 14 ms 24280 KB n=2000
89 Correct 13 ms 24360 KB n=2000
90 Correct 14 ms 24276 KB n=2000
91 Correct 15 ms 24184 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB n=5
2 Correct 11 ms 23764 KB n=100
3 Correct 11 ms 23760 KB n=100
4 Correct 11 ms 23764 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23784 KB n=100
7 Correct 12 ms 23764 KB n=100
8 Correct 13 ms 23764 KB n=100
9 Correct 11 ms 23852 KB n=100
10 Correct 13 ms 23808 KB n=100
11 Correct 11 ms 23852 KB n=100
12 Correct 11 ms 23784 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 11 ms 23780 KB n=100
15 Correct 11 ms 23764 KB n=100
16 Correct 11 ms 23728 KB n=100
17 Correct 11 ms 23836 KB n=100
18 Correct 11 ms 23784 KB n=100
19 Correct 11 ms 23764 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 12 ms 23764 KB n=100
22 Correct 11 ms 23764 KB n=100
23 Correct 11 ms 23796 KB n=100
24 Correct 11 ms 23764 KB n=100
25 Correct 11 ms 23764 KB n=100
26 Correct 11 ms 23764 KB n=12
27 Correct 11 ms 23792 KB n=100
28 Correct 12 ms 23936 KB n=500
29 Correct 12 ms 23892 KB n=500
30 Correct 12 ms 23948 KB n=500
31 Correct 12 ms 23892 KB n=500
32 Correct 12 ms 23936 KB n=500
33 Correct 12 ms 23892 KB n=500
34 Correct 11 ms 23880 KB n=500
35 Correct 12 ms 23956 KB n=500
36 Correct 12 ms 23944 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 13 ms 23892 KB n=500
39 Correct 11 ms 23892 KB n=500
40 Correct 12 ms 23900 KB n=500
41 Correct 12 ms 23828 KB n=500
42 Correct 12 ms 23944 KB n=500
43 Correct 12 ms 23848 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 12 ms 23892 KB n=500
46 Correct 11 ms 23892 KB n=500
47 Correct 12 ms 23956 KB n=500
48 Correct 11 ms 23892 KB n=500
49 Correct 11 ms 23892 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 12 ms 23892 KB n=500
52 Correct 12 ms 23892 KB n=500
53 Correct 12 ms 23892 KB n=500
54 Correct 11 ms 23892 KB n=500
55 Correct 11 ms 23908 KB n=278
56 Correct 11 ms 23892 KB n=500
57 Correct 12 ms 23892 KB n=500
58 Correct 12 ms 23832 KB n=500
59 Correct 14 ms 24276 KB n=2000
60 Correct 13 ms 24276 KB n=2000
61 Correct 14 ms 24276 KB n=2000
62 Correct 13 ms 24288 KB n=2000
63 Correct 14 ms 24284 KB n=2000
64 Correct 14 ms 24304 KB n=2000
65 Correct 14 ms 24276 KB n=2000
66 Correct 13 ms 24276 KB n=2000
67 Correct 14 ms 24148 KB n=2000
68 Correct 13 ms 24304 KB n=2000
69 Correct 13 ms 24236 KB n=2000
70 Correct 13 ms 24276 KB n=2000
71 Correct 13 ms 24276 KB n=2000
72 Correct 13 ms 24260 KB n=2000
73 Correct 13 ms 24220 KB n=2000
74 Correct 13 ms 24200 KB n=1844
75 Correct 13 ms 24208 KB n=2000
76 Correct 14 ms 24204 KB n=2000
77 Correct 14 ms 24168 KB n=2000
78 Correct 14 ms 24200 KB n=2000
79 Correct 14 ms 24280 KB n=2000
80 Correct 14 ms 24276 KB n=2000
81 Correct 15 ms 24204 KB n=2000
82 Correct 14 ms 24268 KB n=2000
83 Correct 13 ms 24332 KB n=2000
84 Correct 14 ms 24284 KB n=2000
85 Correct 14 ms 24304 KB n=2000
86 Correct 14 ms 24276 KB n=2000
87 Correct 14 ms 24276 KB n=2000
88 Correct 14 ms 24280 KB n=2000
89 Correct 13 ms 24360 KB n=2000
90 Correct 14 ms 24276 KB n=2000
91 Correct 15 ms 24184 KB n=2000
92 Correct 576 ms 76028 KB n=200000
93 Correct 626 ms 80988 KB n=200000
94 Correct 459 ms 84272 KB n=200000
95 Correct 569 ms 75788 KB n=200000
96 Correct 571 ms 75828 KB n=200000
97 Correct 635 ms 80048 KB n=200000
98 Correct 542 ms 75844 KB n=200000
99 Correct 663 ms 75984 KB n=200000
100 Correct 558 ms 75964 KB n=200000
101 Correct 400 ms 85572 KB n=200000
102 Correct 352 ms 77076 KB n=200000
103 Correct 335 ms 77192 KB n=200000
104 Correct 339 ms 77100 KB n=200000
105 Correct 334 ms 77548 KB n=200000
106 Correct 337 ms 77516 KB n=200000
107 Correct 358 ms 77512 KB n=200000
108 Correct 556 ms 75980 KB n=200000
109 Correct 544 ms 76036 KB n=200000
110 Correct 546 ms 75972 KB n=200000
111 Correct 546 ms 75328 KB n=200000
112 Correct 408 ms 84604 KB n=200000
113 Correct 576 ms 79964 KB n=200000
114 Correct 558 ms 75356 KB n=200000
115 Correct 687 ms 77908 KB n=200000
116 Correct 533 ms 76224 KB n=200000
117 Correct 421 ms 84848 KB n=200000
118 Correct 642 ms 79056 KB n=200000
119 Correct 534 ms 76068 KB n=200000
120 Correct 382 ms 84592 KB n=200000
121 Correct 372 ms 84508 KB n=200000
122 Correct 348 ms 84812 KB n=200000
123 Correct 373 ms 77316 KB n=200000
124 Correct 105 ms 39096 KB n=25264