Submission #515655

# Submission time Handle Problem Language Result Execution time Memory
515655 2022-01-19T12:26:39 Z Be_dos Birthday gift (IZhO18_treearray) C++17
100 / 100
906 ms 92628 KB
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <unordered_set>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <random>
#include <cfloat>
#include <chrono>
#include <bitset>
#include <complex>
#include <immintrin.h>
#include <cassert>

int32_t* tin, *tout;
int32_t timer_ = 0;
void dfs(int32_t v, int32_t parent, std::vector<int32_t>* tree) {
    tin[v] = timer_++;
    for(int32_t i = 0; i < tree[v].size(); i++)
        if(tree[v][i] != parent)
            dfs(tree[v][i], v, tree);
    tout[v] = timer_;
}

int32_t** ancestors;
void build_ancestors(int32_t v, int32_t parent, std::vector<int32_t>* tree) {
    ancestors[v] = new int32_t[20];
    ancestors[v][0] = v;
    ancestors[v][1] = parent;
    for(int32_t i = 2; i < 20; i++)
        ancestors[v][i] = ancestors[v][i - 1] == -1 ? -1 : ancestors[ancestors[v][i - 1]][i - 1];

    for(int32_t i = 0; i < tree[v].size(); i++)
        if(tree[v][i] != parent)
            build_ancestors(tree[v][i], v, tree);
}

bool is_ancestor(int32_t v1, int32_t v2) {
    return tin[v1] <= tin[v2] && tout[v1] >= tout[v2];
}

int32_t get_lca(int32_t v1, int32_t v2) {
    if(is_ancestor(v1, v2))
        return v1;
    if(is_ancestor(v2, v1))
        return v2;
    int32_t cur = v1;
    for(int32_t i = 19; i >= 0; i--)
        if(ancestors[cur][i] != -1 && !is_ancestor(ancestors[cur][i], v2))
            cur = ancestors[cur][i];
    return ancestors[cur][1];
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    
    int32_t n, m, num_q;
    std::cin >> n >> m >> num_q;

    std::vector<int32_t>* tree = new std::vector<int32_t>[n];
    for(int32_t i = 0; i < n - 1; i++) {
        int32_t src, dst;
        std::cin >> src >> dst;
        src--;
        dst--;

        tree[src].push_back(dst);
        tree[dst].push_back(src);
    }

    int32_t* arr = new int32_t[m];
    for(int32_t i = 0; i < m; i++) {
        std::cin >> arr[i];
        arr[i]--;
    }

    tin = new int32_t[n];
    tout = new int32_t[n];
    dfs(0, -1, tree);

    ancestors = new int32_t*[n];
    build_ancestors(0, -1, tree);

    std::set<int32_t>* ind_per_v = new std::set<int32_t>[n];
    for(int32_t i = 0; i < m; i++)
        ind_per_v[arr[i]].insert(i);
    std::set<int32_t>* ind_per_lca = new std::set<int32_t>[n];
    for(int32_t i = 0; i < m - 1; i++)
        ind_per_lca[get_lca(arr[i], arr[i + 1])].insert(i);
    for(int32_t q = 0; q < num_q; q++) {
        int32_t type;
        std::cin >> type;

        if(type == 1) {
            int32_t pos, v;
            std::cin >> pos >> v;
            pos--;
            v--;

            ind_per_v[arr[pos]].erase(pos);
            if(pos > 0)
                ind_per_lca[get_lca(arr[pos - 1], arr[pos])].erase(pos - 1);
            if(pos < m - 1)
                ind_per_lca[get_lca(arr[pos + 1], arr[pos])].erase(pos);

            arr[pos] = v;
            ind_per_v[arr[pos]].insert(pos);
            if(pos > 0)
                ind_per_lca[get_lca(arr[pos - 1], arr[pos])].insert(pos - 1);
            if(pos < m - 1)
                ind_per_lca[get_lca(arr[pos + 1], arr[pos])].insert(pos);
        } else {
            int32_t left, right, v;
            std::cin >> left >> right >> v;
            left--;
            right--;
            v--;

            auto it = ind_per_v[v].lower_bound(left);
            if(it != ind_per_v[v].end() && *it <= right) {
                std::cout << *it + 1 << " " << *it + 1 << "\n";
                continue;
            }

            auto it2 = ind_per_lca[v].lower_bound(left);
            if(it2 != ind_per_lca[v].end() && *it2 < right) {
                std::cout << *it2 + 1 << " " << *it2 + 2 << "\n";
                continue;
            }
            std::cout << -1 << " " << -1 << "\n";
        }
    }
    return 0;
}






Compilation message

treearray.cpp: In function 'void dfs(int32_t, int32_t, std::vector<int>*)':
treearray.cpp:28:26: warning: comparison of integer expressions of different signedness: 'int32_t' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     for(int32_t i = 0; i < tree[v].size(); i++)
      |                        ~~^~~~~~~~~~~~~~~~
treearray.cpp: In function 'void build_ancestors(int32_t, int32_t, std::vector<int>*)':
treearray.cpp:42:26: warning: comparison of integer expressions of different signedness: 'int32_t' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for(int32_t i = 0; i < tree[v].size(); i++)
      |                        ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n=5
2 Correct 1 ms 332 KB n=100
3 Correct 1 ms 308 KB n=100
4 Correct 1 ms 332 KB n=100
5 Correct 0 ms 332 KB n=100
6 Correct 1 ms 308 KB n=100
7 Correct 0 ms 332 KB n=100
8 Correct 0 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 0 ms 312 KB n=100
12 Correct 1 ms 332 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 308 KB n=100
15 Correct 0 ms 332 KB n=100
16 Correct 1 ms 332 KB n=100
17 Correct 0 ms 332 KB n=100
18 Correct 0 ms 332 KB n=100
19 Correct 0 ms 332 KB n=100
20 Correct 0 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 308 KB n=100
23 Correct 0 ms 332 KB n=100
24 Correct 0 ms 332 KB n=100
25 Correct 0 ms 308 KB n=100
26 Correct 0 ms 204 KB n=12
27 Correct 1 ms 332 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n=5
2 Correct 1 ms 332 KB n=100
3 Correct 1 ms 308 KB n=100
4 Correct 1 ms 332 KB n=100
5 Correct 0 ms 332 KB n=100
6 Correct 1 ms 308 KB n=100
7 Correct 0 ms 332 KB n=100
8 Correct 0 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 0 ms 312 KB n=100
12 Correct 1 ms 332 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 308 KB n=100
15 Correct 0 ms 332 KB n=100
16 Correct 1 ms 332 KB n=100
17 Correct 0 ms 332 KB n=100
18 Correct 0 ms 332 KB n=100
19 Correct 0 ms 332 KB n=100
20 Correct 0 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 308 KB n=100
23 Correct 0 ms 332 KB n=100
24 Correct 0 ms 332 KB n=100
25 Correct 0 ms 308 KB n=100
26 Correct 0 ms 204 KB n=12
27 Correct 1 ms 332 KB n=100
28 Correct 1 ms 460 KB n=500
29 Correct 1 ms 460 KB n=500
30 Correct 1 ms 460 KB n=500
31 Correct 1 ms 448 KB n=500
32 Correct 1 ms 460 KB n=500
33 Correct 1 ms 460 KB n=500
34 Correct 1 ms 460 KB n=500
35 Correct 1 ms 460 KB n=500
36 Correct 1 ms 444 KB n=500
37 Correct 1 ms 460 KB n=500
38 Correct 1 ms 460 KB n=500
39 Correct 1 ms 440 KB n=500
40 Correct 1 ms 552 KB n=500
41 Correct 1 ms 460 KB n=500
42 Correct 1 ms 444 KB n=500
43 Correct 1 ms 448 KB n=500
44 Correct 1 ms 444 KB n=500
45 Correct 1 ms 460 KB n=500
46 Correct 1 ms 460 KB n=500
47 Correct 1 ms 460 KB n=500
48 Correct 1 ms 460 KB n=500
49 Correct 1 ms 460 KB n=500
50 Correct 1 ms 460 KB n=500
51 Correct 1 ms 448 KB n=500
52 Correct 1 ms 460 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 452 KB n=500
55 Correct 1 ms 332 KB n=278
56 Correct 1 ms 444 KB n=500
57 Correct 1 ms 460 KB n=500
58 Correct 1 ms 460 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n=5
2 Correct 1 ms 332 KB n=100
3 Correct 1 ms 308 KB n=100
4 Correct 1 ms 332 KB n=100
5 Correct 0 ms 332 KB n=100
6 Correct 1 ms 308 KB n=100
7 Correct 0 ms 332 KB n=100
8 Correct 0 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 0 ms 312 KB n=100
12 Correct 1 ms 332 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 308 KB n=100
15 Correct 0 ms 332 KB n=100
16 Correct 1 ms 332 KB n=100
17 Correct 0 ms 332 KB n=100
18 Correct 0 ms 332 KB n=100
19 Correct 0 ms 332 KB n=100
20 Correct 0 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 308 KB n=100
23 Correct 0 ms 332 KB n=100
24 Correct 0 ms 332 KB n=100
25 Correct 0 ms 308 KB n=100
26 Correct 0 ms 204 KB n=12
27 Correct 1 ms 332 KB n=100
28 Correct 1 ms 460 KB n=500
29 Correct 1 ms 460 KB n=500
30 Correct 1 ms 460 KB n=500
31 Correct 1 ms 448 KB n=500
32 Correct 1 ms 460 KB n=500
33 Correct 1 ms 460 KB n=500
34 Correct 1 ms 460 KB n=500
35 Correct 1 ms 460 KB n=500
36 Correct 1 ms 444 KB n=500
37 Correct 1 ms 460 KB n=500
38 Correct 1 ms 460 KB n=500
39 Correct 1 ms 440 KB n=500
40 Correct 1 ms 552 KB n=500
41 Correct 1 ms 460 KB n=500
42 Correct 1 ms 444 KB n=500
43 Correct 1 ms 448 KB n=500
44 Correct 1 ms 444 KB n=500
45 Correct 1 ms 460 KB n=500
46 Correct 1 ms 460 KB n=500
47 Correct 1 ms 460 KB n=500
48 Correct 1 ms 460 KB n=500
49 Correct 1 ms 460 KB n=500
50 Correct 1 ms 460 KB n=500
51 Correct 1 ms 448 KB n=500
52 Correct 1 ms 460 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 452 KB n=500
55 Correct 1 ms 332 KB n=278
56 Correct 1 ms 444 KB n=500
57 Correct 1 ms 460 KB n=500
58 Correct 1 ms 460 KB n=500
59 Correct 3 ms 964 KB n=2000
60 Correct 3 ms 1100 KB n=2000
61 Correct 3 ms 1100 KB n=2000
62 Correct 4 ms 1100 KB n=2000
63 Correct 3 ms 1100 KB n=2000
64 Correct 3 ms 1100 KB n=2000
65 Correct 3 ms 1100 KB n=2000
66 Correct 3 ms 1100 KB n=2000
67 Correct 4 ms 972 KB n=2000
68 Correct 3 ms 1100 KB n=2000
69 Correct 4 ms 1012 KB n=2000
70 Correct 3 ms 1088 KB n=2000
71 Correct 3 ms 1100 KB n=2000
72 Correct 3 ms 1088 KB n=2000
73 Correct 3 ms 972 KB n=2000
74 Correct 3 ms 960 KB n=1844
75 Correct 3 ms 1100 KB n=2000
76 Correct 3 ms 972 KB n=2000
77 Correct 3 ms 972 KB n=2000
78 Correct 4 ms 1108 KB n=2000
79 Correct 3 ms 972 KB n=2000
80 Correct 3 ms 1088 KB n=2000
81 Correct 3 ms 1100 KB n=2000
82 Correct 3 ms 972 KB n=2000
83 Correct 3 ms 1100 KB n=2000
84 Correct 3 ms 1100 KB n=2000
85 Correct 3 ms 1100 KB n=2000
86 Correct 3 ms 1100 KB n=2000
87 Correct 3 ms 1100 KB n=2000
88 Correct 2 ms 1100 KB n=2000
89 Correct 3 ms 1100 KB n=2000
90 Correct 3 ms 1092 KB n=2000
91 Correct 3 ms 1100 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n=5
2 Correct 1 ms 332 KB n=100
3 Correct 1 ms 308 KB n=100
4 Correct 1 ms 332 KB n=100
5 Correct 0 ms 332 KB n=100
6 Correct 1 ms 308 KB n=100
7 Correct 0 ms 332 KB n=100
8 Correct 0 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 0 ms 312 KB n=100
12 Correct 1 ms 332 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 308 KB n=100
15 Correct 0 ms 332 KB n=100
16 Correct 1 ms 332 KB n=100
17 Correct 0 ms 332 KB n=100
18 Correct 0 ms 332 KB n=100
19 Correct 0 ms 332 KB n=100
20 Correct 0 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 308 KB n=100
23 Correct 0 ms 332 KB n=100
24 Correct 0 ms 332 KB n=100
25 Correct 0 ms 308 KB n=100
26 Correct 0 ms 204 KB n=12
27 Correct 1 ms 332 KB n=100
28 Correct 1 ms 460 KB n=500
29 Correct 1 ms 460 KB n=500
30 Correct 1 ms 460 KB n=500
31 Correct 1 ms 448 KB n=500
32 Correct 1 ms 460 KB n=500
33 Correct 1 ms 460 KB n=500
34 Correct 1 ms 460 KB n=500
35 Correct 1 ms 460 KB n=500
36 Correct 1 ms 444 KB n=500
37 Correct 1 ms 460 KB n=500
38 Correct 1 ms 460 KB n=500
39 Correct 1 ms 440 KB n=500
40 Correct 1 ms 552 KB n=500
41 Correct 1 ms 460 KB n=500
42 Correct 1 ms 444 KB n=500
43 Correct 1 ms 448 KB n=500
44 Correct 1 ms 444 KB n=500
45 Correct 1 ms 460 KB n=500
46 Correct 1 ms 460 KB n=500
47 Correct 1 ms 460 KB n=500
48 Correct 1 ms 460 KB n=500
49 Correct 1 ms 460 KB n=500
50 Correct 1 ms 460 KB n=500
51 Correct 1 ms 448 KB n=500
52 Correct 1 ms 460 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 452 KB n=500
55 Correct 1 ms 332 KB n=278
56 Correct 1 ms 444 KB n=500
57 Correct 1 ms 460 KB n=500
58 Correct 1 ms 460 KB n=500
59 Correct 3 ms 964 KB n=2000
60 Correct 3 ms 1100 KB n=2000
61 Correct 3 ms 1100 KB n=2000
62 Correct 4 ms 1100 KB n=2000
63 Correct 3 ms 1100 KB n=2000
64 Correct 3 ms 1100 KB n=2000
65 Correct 3 ms 1100 KB n=2000
66 Correct 3 ms 1100 KB n=2000
67 Correct 4 ms 972 KB n=2000
68 Correct 3 ms 1100 KB n=2000
69 Correct 4 ms 1012 KB n=2000
70 Correct 3 ms 1088 KB n=2000
71 Correct 3 ms 1100 KB n=2000
72 Correct 3 ms 1088 KB n=2000
73 Correct 3 ms 972 KB n=2000
74 Correct 3 ms 960 KB n=1844
75 Correct 3 ms 1100 KB n=2000
76 Correct 3 ms 972 KB n=2000
77 Correct 3 ms 972 KB n=2000
78 Correct 4 ms 1108 KB n=2000
79 Correct 3 ms 972 KB n=2000
80 Correct 3 ms 1088 KB n=2000
81 Correct 3 ms 1100 KB n=2000
82 Correct 3 ms 972 KB n=2000
83 Correct 3 ms 1100 KB n=2000
84 Correct 3 ms 1100 KB n=2000
85 Correct 3 ms 1100 KB n=2000
86 Correct 3 ms 1100 KB n=2000
87 Correct 3 ms 1100 KB n=2000
88 Correct 2 ms 1100 KB n=2000
89 Correct 3 ms 1100 KB n=2000
90 Correct 3 ms 1092 KB n=2000
91 Correct 3 ms 1100 KB n=2000
92 Correct 577 ms 80096 KB n=200000
93 Correct 726 ms 86396 KB n=200000
94 Correct 561 ms 90996 KB n=200000
95 Correct 604 ms 79800 KB n=200000
96 Correct 510 ms 79816 KB n=200000
97 Correct 734 ms 85216 KB n=200000
98 Correct 552 ms 79812 KB n=200000
99 Correct 657 ms 80068 KB n=200000
100 Correct 551 ms 79828 KB n=200000
101 Correct 470 ms 92628 KB n=200000
102 Correct 334 ms 81012 KB n=200000
103 Correct 333 ms 81032 KB n=200000
104 Correct 355 ms 81052 KB n=200000
105 Correct 344 ms 81476 KB n=200000
106 Correct 343 ms 81420 KB n=200000
107 Correct 367 ms 81480 KB n=200000
108 Correct 600 ms 79932 KB n=200000
109 Correct 603 ms 80060 KB n=200000
110 Correct 611 ms 79960 KB n=200000
111 Correct 626 ms 79260 KB n=200000
112 Correct 474 ms 91372 KB n=200000
113 Correct 679 ms 85244 KB n=200000
114 Correct 539 ms 79344 KB n=200000
115 Correct 906 ms 82444 KB n=200000
116 Correct 531 ms 80024 KB n=200000
117 Correct 609 ms 91748 KB n=200000
118 Correct 781 ms 83940 KB n=200000
119 Correct 505 ms 79980 KB n=200000
120 Correct 434 ms 91640 KB n=200000
121 Correct 479 ms 91572 KB n=200000
122 Correct 486 ms 91956 KB n=200000
123 Correct 360 ms 81164 KB n=200000
124 Correct 121 ms 19220 KB n=25264