Submission #345446

# Submission time Handle Problem Language Result Execution time Memory
345446 2021-01-07T10:46:32 Z koketsu Birthday gift (IZhO18_treearray) C++14
100 / 100
1432 ms 172216 KB
#include <bits/stdc++.h>
#define pb push_back
#define LL long long
#define Kultivator ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const LL Mxn = 1e6 + 7;
const LL Mod = 1e9 + 7;
const LL Inf = 1e14 + 7;

int N, M, Q, d[Mxn][20], lvl[Mxn], LCA[Mxn], W[Mxn];

vector <int> g[Mxn];

set <int> St[Mxn], Stpos[Mxn];

void Dfs(int x, int p){
    d[x][0] = p;
    lvl[x] = lvl[p] + 1;
    for(int i = 1; i <= 18; i++){
        d[x][i] = d[d[x][i - 1]][i - 1];
    }
    for(int to : g[x]){
        if(to == p) continue;
        Dfs(to, x);
    }
}

int lca(int u, int v){
    if(lvl[u] < lvl[v]) swap(u, v);
    int dif = (lvl[u] - lvl[v]);
    for(int i = 0; i <= 18; i++){
        if((1 << i) & dif) u = d[u][i];
    }
    if(u == v) return u;
    for(int i = 18; i >= 0; i--){
        if(d[u][i] != d[v][i]) u = d[u][i], v = d[v][i];
    }
    return d[u][0];
}

void Replace(int Pos, int v, int type){
    St[lca(W[Pos], W[Pos + type])].erase(min(Pos, Pos + type));
    St[lca(v, W[Pos + type])].insert(min(Pos, Pos + type));
}

int main(){
    Kultivator;
    cin >> N >> M >> Q;
    for(int i = 1, A, B; i < N; i++){
        cin >> A >> B;
        g[A].pb(B);
        g[B].pb(A);
    }
    for(int i = 1; i <= M; i++){
        cin >> W[i];
    }
    Dfs(1, 0);
    for(int i = 1; i < M; i++){
        Stpos[W[i]].insert(i);
        St[lca(W[i], W[i + 1])].insert(i);
    }
    Stpos[W[M]].insert(M);
    while(Q--){
        int T;
        cin >> T;
        if(T == 1){
            int Pos, v;
            cin >> Pos >> v;
            if(Pos != M) Replace(Pos, v, 1);
            if(Pos != 1) Replace(Pos, v, -1);
            Stpos[W[Pos]].erase(Pos);
            Stpos[v].insert(Pos);
            W[Pos] = v;
        } else {
            int L, R, v;
            pair <int, int> Ans = {-1, -1};
            cin >> L >> R >> v;
            set <int> :: iterator it = Stpos[v].lower_bound(L);
            if(it != Stpos[v].end() && L <= *it && *it <= R) Ans.first = *it, Ans.second = *it;
            it = St[v].lower_bound(L);
            if(it != St[v].end() && L <= *it && *it + 1 <= R) Ans.first = *it, Ans.second = (*it + 1);
            cout << Ans.first << ' ' << Ans.second << '\n';
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 89 ms 117724 KB n=5
2 Correct 78 ms 117748 KB n=100
3 Correct 81 ms 117740 KB n=100
4 Correct 81 ms 117804 KB n=100
5 Correct 86 ms 117820 KB n=100
6 Correct 80 ms 117740 KB n=100
7 Correct 91 ms 117740 KB n=100
8 Correct 84 ms 117740 KB n=100
9 Correct 97 ms 117860 KB n=100
10 Correct 89 ms 117740 KB n=100
11 Correct 103 ms 117740 KB n=100
12 Correct 90 ms 117740 KB n=100
13 Correct 85 ms 117740 KB n=100
14 Correct 80 ms 117740 KB n=100
15 Correct 81 ms 117740 KB n=100
16 Correct 87 ms 117740 KB n=100
17 Correct 109 ms 117868 KB n=100
18 Correct 87 ms 117740 KB n=100
19 Correct 87 ms 117740 KB n=100
20 Correct 85 ms 117740 KB n=100
21 Correct 85 ms 117740 KB n=100
22 Correct 86 ms 117860 KB n=100
23 Correct 80 ms 117776 KB n=100
24 Correct 83 ms 117740 KB n=100
25 Correct 94 ms 117740 KB n=100
26 Correct 84 ms 117740 KB n=12
27 Correct 85 ms 117860 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 89 ms 117724 KB n=5
2 Correct 78 ms 117748 KB n=100
3 Correct 81 ms 117740 KB n=100
4 Correct 81 ms 117804 KB n=100
5 Correct 86 ms 117820 KB n=100
6 Correct 80 ms 117740 KB n=100
7 Correct 91 ms 117740 KB n=100
8 Correct 84 ms 117740 KB n=100
9 Correct 97 ms 117860 KB n=100
10 Correct 89 ms 117740 KB n=100
11 Correct 103 ms 117740 KB n=100
12 Correct 90 ms 117740 KB n=100
13 Correct 85 ms 117740 KB n=100
14 Correct 80 ms 117740 KB n=100
15 Correct 81 ms 117740 KB n=100
16 Correct 87 ms 117740 KB n=100
17 Correct 109 ms 117868 KB n=100
18 Correct 87 ms 117740 KB n=100
19 Correct 87 ms 117740 KB n=100
20 Correct 85 ms 117740 KB n=100
21 Correct 85 ms 117740 KB n=100
22 Correct 86 ms 117860 KB n=100
23 Correct 80 ms 117776 KB n=100
24 Correct 83 ms 117740 KB n=100
25 Correct 94 ms 117740 KB n=100
26 Correct 84 ms 117740 KB n=12
27 Correct 85 ms 117860 KB n=100
28 Correct 94 ms 117868 KB n=500
29 Correct 95 ms 117868 KB n=500
30 Correct 82 ms 117868 KB n=500
31 Correct 79 ms 117872 KB n=500
32 Correct 94 ms 117868 KB n=500
33 Correct 96 ms 117964 KB n=500
34 Correct 87 ms 117904 KB n=500
35 Correct 99 ms 117880 KB n=500
36 Correct 98 ms 117868 KB n=500
37 Correct 93 ms 117868 KB n=500
38 Correct 86 ms 118020 KB n=500
39 Correct 79 ms 117964 KB n=500
40 Correct 77 ms 117868 KB n=500
41 Correct 82 ms 117868 KB n=500
42 Correct 95 ms 117868 KB n=500
43 Correct 80 ms 117912 KB n=500
44 Correct 82 ms 117868 KB n=500
45 Correct 84 ms 117868 KB n=500
46 Correct 92 ms 117868 KB n=500
47 Correct 82 ms 117868 KB n=500
48 Correct 85 ms 117868 KB n=500
49 Correct 85 ms 117956 KB n=500
50 Correct 83 ms 117956 KB n=500
51 Correct 81 ms 117968 KB n=500
52 Correct 96 ms 117868 KB n=500
53 Correct 83 ms 117868 KB n=500
54 Correct 92 ms 117868 KB n=500
55 Correct 93 ms 117868 KB n=278
56 Correct 74 ms 117868 KB n=500
57 Correct 88 ms 117892 KB n=500
58 Correct 82 ms 117868 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 89 ms 117724 KB n=5
2 Correct 78 ms 117748 KB n=100
3 Correct 81 ms 117740 KB n=100
4 Correct 81 ms 117804 KB n=100
5 Correct 86 ms 117820 KB n=100
6 Correct 80 ms 117740 KB n=100
7 Correct 91 ms 117740 KB n=100
8 Correct 84 ms 117740 KB n=100
9 Correct 97 ms 117860 KB n=100
10 Correct 89 ms 117740 KB n=100
11 Correct 103 ms 117740 KB n=100
12 Correct 90 ms 117740 KB n=100
13 Correct 85 ms 117740 KB n=100
14 Correct 80 ms 117740 KB n=100
15 Correct 81 ms 117740 KB n=100
16 Correct 87 ms 117740 KB n=100
17 Correct 109 ms 117868 KB n=100
18 Correct 87 ms 117740 KB n=100
19 Correct 87 ms 117740 KB n=100
20 Correct 85 ms 117740 KB n=100
21 Correct 85 ms 117740 KB n=100
22 Correct 86 ms 117860 KB n=100
23 Correct 80 ms 117776 KB n=100
24 Correct 83 ms 117740 KB n=100
25 Correct 94 ms 117740 KB n=100
26 Correct 84 ms 117740 KB n=12
27 Correct 85 ms 117860 KB n=100
28 Correct 94 ms 117868 KB n=500
29 Correct 95 ms 117868 KB n=500
30 Correct 82 ms 117868 KB n=500
31 Correct 79 ms 117872 KB n=500
32 Correct 94 ms 117868 KB n=500
33 Correct 96 ms 117964 KB n=500
34 Correct 87 ms 117904 KB n=500
35 Correct 99 ms 117880 KB n=500
36 Correct 98 ms 117868 KB n=500
37 Correct 93 ms 117868 KB n=500
38 Correct 86 ms 118020 KB n=500
39 Correct 79 ms 117964 KB n=500
40 Correct 77 ms 117868 KB n=500
41 Correct 82 ms 117868 KB n=500
42 Correct 95 ms 117868 KB n=500
43 Correct 80 ms 117912 KB n=500
44 Correct 82 ms 117868 KB n=500
45 Correct 84 ms 117868 KB n=500
46 Correct 92 ms 117868 KB n=500
47 Correct 82 ms 117868 KB n=500
48 Correct 85 ms 117868 KB n=500
49 Correct 85 ms 117956 KB n=500
50 Correct 83 ms 117956 KB n=500
51 Correct 81 ms 117968 KB n=500
52 Correct 96 ms 117868 KB n=500
53 Correct 83 ms 117868 KB n=500
54 Correct 92 ms 117868 KB n=500
55 Correct 93 ms 117868 KB n=278
56 Correct 74 ms 117868 KB n=500
57 Correct 88 ms 117892 KB n=500
58 Correct 82 ms 117868 KB n=500
59 Correct 83 ms 118252 KB n=2000
60 Correct 80 ms 118380 KB n=2000
61 Correct 82 ms 118380 KB n=2000
62 Correct 88 ms 118252 KB n=2000
63 Correct 85 ms 118252 KB n=2000
64 Correct 90 ms 118356 KB n=2000
65 Correct 100 ms 118296 KB n=2000
66 Correct 93 ms 118288 KB n=2000
67 Correct 100 ms 118244 KB n=2000
68 Correct 87 ms 118252 KB n=2000
69 Correct 104 ms 118236 KB n=2000
70 Correct 102 ms 118252 KB n=2000
71 Correct 92 ms 118256 KB n=2000
72 Correct 84 ms 118252 KB n=2000
73 Correct 84 ms 118252 KB n=2000
74 Correct 92 ms 118252 KB n=1844
75 Correct 84 ms 118324 KB n=2000
76 Correct 84 ms 118380 KB n=2000
77 Correct 82 ms 118252 KB n=2000
78 Correct 86 ms 118252 KB n=2000
79 Correct 90 ms 118252 KB n=2000
80 Correct 96 ms 118336 KB n=2000
81 Correct 86 ms 118252 KB n=2000
82 Correct 100 ms 118252 KB n=2000
83 Correct 106 ms 118380 KB n=2000
84 Correct 92 ms 118252 KB n=2000
85 Correct 92 ms 118356 KB n=2000
86 Correct 89 ms 118380 KB n=2000
87 Correct 81 ms 118252 KB n=2000
88 Correct 84 ms 118380 KB n=2000
89 Correct 81 ms 118380 KB n=2000
90 Correct 93 ms 118380 KB n=2000
91 Correct 87 ms 118284 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 89 ms 117724 KB n=5
2 Correct 78 ms 117748 KB n=100
3 Correct 81 ms 117740 KB n=100
4 Correct 81 ms 117804 KB n=100
5 Correct 86 ms 117820 KB n=100
6 Correct 80 ms 117740 KB n=100
7 Correct 91 ms 117740 KB n=100
8 Correct 84 ms 117740 KB n=100
9 Correct 97 ms 117860 KB n=100
10 Correct 89 ms 117740 KB n=100
11 Correct 103 ms 117740 KB n=100
12 Correct 90 ms 117740 KB n=100
13 Correct 85 ms 117740 KB n=100
14 Correct 80 ms 117740 KB n=100
15 Correct 81 ms 117740 KB n=100
16 Correct 87 ms 117740 KB n=100
17 Correct 109 ms 117868 KB n=100
18 Correct 87 ms 117740 KB n=100
19 Correct 87 ms 117740 KB n=100
20 Correct 85 ms 117740 KB n=100
21 Correct 85 ms 117740 KB n=100
22 Correct 86 ms 117860 KB n=100
23 Correct 80 ms 117776 KB n=100
24 Correct 83 ms 117740 KB n=100
25 Correct 94 ms 117740 KB n=100
26 Correct 84 ms 117740 KB n=12
27 Correct 85 ms 117860 KB n=100
28 Correct 94 ms 117868 KB n=500
29 Correct 95 ms 117868 KB n=500
30 Correct 82 ms 117868 KB n=500
31 Correct 79 ms 117872 KB n=500
32 Correct 94 ms 117868 KB n=500
33 Correct 96 ms 117964 KB n=500
34 Correct 87 ms 117904 KB n=500
35 Correct 99 ms 117880 KB n=500
36 Correct 98 ms 117868 KB n=500
37 Correct 93 ms 117868 KB n=500
38 Correct 86 ms 118020 KB n=500
39 Correct 79 ms 117964 KB n=500
40 Correct 77 ms 117868 KB n=500
41 Correct 82 ms 117868 KB n=500
42 Correct 95 ms 117868 KB n=500
43 Correct 80 ms 117912 KB n=500
44 Correct 82 ms 117868 KB n=500
45 Correct 84 ms 117868 KB n=500
46 Correct 92 ms 117868 KB n=500
47 Correct 82 ms 117868 KB n=500
48 Correct 85 ms 117868 KB n=500
49 Correct 85 ms 117956 KB n=500
50 Correct 83 ms 117956 KB n=500
51 Correct 81 ms 117968 KB n=500
52 Correct 96 ms 117868 KB n=500
53 Correct 83 ms 117868 KB n=500
54 Correct 92 ms 117868 KB n=500
55 Correct 93 ms 117868 KB n=278
56 Correct 74 ms 117868 KB n=500
57 Correct 88 ms 117892 KB n=500
58 Correct 82 ms 117868 KB n=500
59 Correct 83 ms 118252 KB n=2000
60 Correct 80 ms 118380 KB n=2000
61 Correct 82 ms 118380 KB n=2000
62 Correct 88 ms 118252 KB n=2000
63 Correct 85 ms 118252 KB n=2000
64 Correct 90 ms 118356 KB n=2000
65 Correct 100 ms 118296 KB n=2000
66 Correct 93 ms 118288 KB n=2000
67 Correct 100 ms 118244 KB n=2000
68 Correct 87 ms 118252 KB n=2000
69 Correct 104 ms 118236 KB n=2000
70 Correct 102 ms 118252 KB n=2000
71 Correct 92 ms 118256 KB n=2000
72 Correct 84 ms 118252 KB n=2000
73 Correct 84 ms 118252 KB n=2000
74 Correct 92 ms 118252 KB n=1844
75 Correct 84 ms 118324 KB n=2000
76 Correct 84 ms 118380 KB n=2000
77 Correct 82 ms 118252 KB n=2000
78 Correct 86 ms 118252 KB n=2000
79 Correct 90 ms 118252 KB n=2000
80 Correct 96 ms 118336 KB n=2000
81 Correct 86 ms 118252 KB n=2000
82 Correct 100 ms 118252 KB n=2000
83 Correct 106 ms 118380 KB n=2000
84 Correct 92 ms 118252 KB n=2000
85 Correct 92 ms 118356 KB n=2000
86 Correct 89 ms 118380 KB n=2000
87 Correct 81 ms 118252 KB n=2000
88 Correct 84 ms 118380 KB n=2000
89 Correct 81 ms 118380 KB n=2000
90 Correct 93 ms 118380 KB n=2000
91 Correct 87 ms 118284 KB n=2000
92 Correct 1004 ms 164548 KB n=200000
93 Correct 1299 ms 168228 KB n=200000
94 Correct 1224 ms 171432 KB n=200000
95 Correct 967 ms 163484 KB n=200000
96 Correct 978 ms 163512 KB n=200000
97 Correct 1328 ms 166424 KB n=200000
98 Correct 964 ms 163424 KB n=200000
99 Correct 1252 ms 162880 KB n=200000
100 Correct 1096 ms 163540 KB n=200000
101 Correct 1245 ms 171916 KB n=200000
102 Correct 637 ms 164588 KB n=200000
103 Correct 640 ms 164588 KB n=200000
104 Correct 611 ms 164708 KB n=200000
105 Correct 661 ms 164376 KB n=200000
106 Correct 691 ms 164368 KB n=200000
107 Correct 645 ms 164268 KB n=200000
108 Correct 1068 ms 163236 KB n=200000
109 Correct 1098 ms 163352 KB n=200000
110 Correct 1111 ms 163240 KB n=200000
111 Correct 1067 ms 163596 KB n=200000
112 Correct 1234 ms 171040 KB n=200000
113 Correct 1401 ms 166536 KB n=200000
114 Correct 1047 ms 163664 KB n=200000
115 Correct 1432 ms 164348 KB n=200000
116 Correct 918 ms 163416 KB n=200000
117 Correct 1229 ms 171320 KB n=200000
118 Correct 1340 ms 165324 KB n=200000
119 Correct 889 ms 163316 KB n=200000
120 Correct 1169 ms 171756 KB n=200000
121 Correct 1291 ms 171692 KB n=200000
122 Correct 1240 ms 172216 KB n=200000
123 Correct 685 ms 163932 KB n=200000
124 Correct 403 ms 132576 KB n=25264