Submission #380838

# Submission time Handle Problem Language Result Execution time Memory
380838 2021-03-23T08:58:37 Z 2qbingxuan Birthday gift (IZhO18_treearray) C++14
100 / 100
1499 ms 99224 KB
#include <bits/stdc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) qqbx(#a, a)
#define pary(a...) danb(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
    int cnt = sizeof...(T);
    ((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
    std::cerr << "\033[1;32m[ " << s << " ] = [ ";
    for (auto it = L; it != R; ++it)
        std::cerr << *it << ' ';
    std::cerr << "]\033[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) ((void)0)
#define pary(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define pb emplace_back
#define sort_uni(v) sort(all(v)),v.erase(unique(all(v)), v.end())
#define get_pos(u,v) int(lower_bound(all(u), v) - u.begin())

using namespace std;
using ll = int64_t;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using max_heap = priority_queue<T, vector<T>, less<T>>;
const int maxn = 300025, inf = 1e9, MOD = 1000000007;
const ll INF = 1e18;

vector<int> g[maxn];
int pa[20][maxn], dep[maxn];
void dfs(int i, int p) {
    for (int j: g[i]) {
        if (j != p) {
            dep[j] = dep[i] + 1;
            pa[0][j] = i;
            for (int L = 1; L < 20; L++)
                pa[L][j] = pa[L-1][pa[L-1][j]];
            dfs(j, i);
        }
    }
}
int lca(int a, int b) {
    if (dep[a] < dep[b]) swap(a, b);
    int dif = dep[a] - dep[b];
    for (int i = 19; i >= 0; i--) if (dif >> i & 1) a = pa[i][a];
    if (a == b) return a;
    for (int i = 19; i >= 0; i--) if (pa[i][a] != pa[i][b]) a = pa[i][a], b = pa[i][b];
    return pa[0][a];
}
set<int> seg[2][maxn];
pair<int,int> query(int x, int l, int r) { // [l, r]

    if (auto it = seg[0][x].lower_bound(l); it != seg[0][x].end() && *it <= r) {
        return {*it, *it};
    }

    if (auto it = seg[1][x].lower_bound(l); it != seg[1][x].end() && *it < r) {
        return {*it, *it+1};
    }
    return {-1, -1};
    
}
int v[maxn];
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    dfs(1, 0);
    for (int i = 1; i <= m; i++) cin >> v[i];
    for (int i = 1; i <= m; i++)
        seg[0][v[i]].insert(i);
    for (int i = 1; i < m; i++)
        seg[1][lca(v[i], v[i+1])].insert(i);
    safe;
    
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            int i, x;
            cin >> i >> x;
            seg[0][v[i]].erase(i);
            if (i < m)
                seg[1][lca(v[i], v[i+1])].erase(i);
            if (i > 1)
                seg[1][lca(v[i], v[i-1])].erase(i-1);
            v[i] = x;
            seg[0][v[i]].insert(i);
            if (i < m)
                seg[1][lca(v[i], v[i+1])].insert(i);
            if (i > 1)
                seg[1][lca(v[i], v[i-1])].insert(i-1);
        } else if (t == 2) {
            int l, r, x;
            cin >> l >> r >> x;
            auto [a, b] = query(x, l, r);
            cout << a << ' ' << b << '\n';
        }
    }
}
/*
5 4 4
1   2
3   1
3   4
5   3
4 5 2 3
2 1 3 1
1 3 5 
2 1 3 1
2 3 4 5
 */

Compilation message

treearray.cpp: In function 'std::pair<int, int> query(int, int, int)':
treearray.cpp:57:9: warning: init-statement in selection statements only available with '-std=c++17' or '-std=gnu++17'
   57 |     if (auto it = seg[0][x].lower_bound(l); it != seg[0][x].end() && *it <= r) {
      |         ^~~~
treearray.cpp:61:9: warning: init-statement in selection statements only available with '-std=c++17' or '-std=gnu++17'
   61 |     if (auto it = seg[1][x].lower_bound(l); it != seg[1][x].end() && *it < r) {
      |         ^~~~
treearray.cpp: In function 'int main()':
treearray.cpp:106:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  106 |             auto [a, b] = query(x, l, r);
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 27 ms 35692 KB n=5
2 Correct 28 ms 35692 KB n=100
3 Correct 26 ms 35692 KB n=100
4 Correct 29 ms 35692 KB n=100
5 Correct 25 ms 35692 KB n=100
6 Correct 25 ms 35692 KB n=100
7 Correct 25 ms 35692 KB n=100
8 Correct 25 ms 35820 KB n=100
9 Correct 24 ms 35692 KB n=100
10 Correct 30 ms 35692 KB n=100
11 Correct 24 ms 35692 KB n=100
12 Correct 26 ms 35692 KB n=100
13 Correct 30 ms 35692 KB n=100
14 Correct 24 ms 35692 KB n=100
15 Correct 24 ms 35692 KB n=100
16 Correct 24 ms 35692 KB n=100
17 Correct 24 ms 35692 KB n=100
18 Correct 26 ms 35692 KB n=100
19 Correct 28 ms 35692 KB n=100
20 Correct 26 ms 35692 KB n=100
21 Correct 24 ms 35692 KB n=100
22 Correct 25 ms 35692 KB n=100
23 Correct 24 ms 35692 KB n=100
24 Correct 26 ms 35820 KB n=100
25 Correct 24 ms 35692 KB n=100
26 Correct 27 ms 35692 KB n=12
27 Correct 24 ms 35692 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 27 ms 35692 KB n=5
2 Correct 28 ms 35692 KB n=100
3 Correct 26 ms 35692 KB n=100
4 Correct 29 ms 35692 KB n=100
5 Correct 25 ms 35692 KB n=100
6 Correct 25 ms 35692 KB n=100
7 Correct 25 ms 35692 KB n=100
8 Correct 25 ms 35820 KB n=100
9 Correct 24 ms 35692 KB n=100
10 Correct 30 ms 35692 KB n=100
11 Correct 24 ms 35692 KB n=100
12 Correct 26 ms 35692 KB n=100
13 Correct 30 ms 35692 KB n=100
14 Correct 24 ms 35692 KB n=100
15 Correct 24 ms 35692 KB n=100
16 Correct 24 ms 35692 KB n=100
17 Correct 24 ms 35692 KB n=100
18 Correct 26 ms 35692 KB n=100
19 Correct 28 ms 35692 KB n=100
20 Correct 26 ms 35692 KB n=100
21 Correct 24 ms 35692 KB n=100
22 Correct 25 ms 35692 KB n=100
23 Correct 24 ms 35692 KB n=100
24 Correct 26 ms 35820 KB n=100
25 Correct 24 ms 35692 KB n=100
26 Correct 27 ms 35692 KB n=12
27 Correct 24 ms 35692 KB n=100
28 Correct 25 ms 35820 KB n=500
29 Correct 25 ms 35820 KB n=500
30 Correct 30 ms 35820 KB n=500
31 Correct 25 ms 35820 KB n=500
32 Correct 25 ms 35820 KB n=500
33 Correct 25 ms 35820 KB n=500
34 Correct 26 ms 35820 KB n=500
35 Correct 25 ms 35820 KB n=500
36 Correct 25 ms 35820 KB n=500
37 Correct 25 ms 35820 KB n=500
38 Correct 26 ms 35820 KB n=500
39 Correct 29 ms 35820 KB n=500
40 Correct 25 ms 35820 KB n=500
41 Correct 25 ms 35820 KB n=500
42 Correct 27 ms 35820 KB n=500
43 Correct 26 ms 35820 KB n=500
44 Correct 26 ms 35820 KB n=500
45 Correct 26 ms 35820 KB n=500
46 Correct 28 ms 35820 KB n=500
47 Correct 27 ms 35740 KB n=500
48 Correct 25 ms 35820 KB n=500
49 Correct 25 ms 35820 KB n=500
50 Correct 26 ms 35820 KB n=500
51 Correct 25 ms 35820 KB n=500
52 Correct 25 ms 35820 KB n=500
53 Correct 30 ms 35820 KB n=500
54 Correct 26 ms 35820 KB n=500
55 Correct 25 ms 35820 KB n=278
56 Correct 26 ms 35820 KB n=500
57 Correct 25 ms 35820 KB n=500
58 Correct 25 ms 35820 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 27 ms 35692 KB n=5
2 Correct 28 ms 35692 KB n=100
3 Correct 26 ms 35692 KB n=100
4 Correct 29 ms 35692 KB n=100
5 Correct 25 ms 35692 KB n=100
6 Correct 25 ms 35692 KB n=100
7 Correct 25 ms 35692 KB n=100
8 Correct 25 ms 35820 KB n=100
9 Correct 24 ms 35692 KB n=100
10 Correct 30 ms 35692 KB n=100
11 Correct 24 ms 35692 KB n=100
12 Correct 26 ms 35692 KB n=100
13 Correct 30 ms 35692 KB n=100
14 Correct 24 ms 35692 KB n=100
15 Correct 24 ms 35692 KB n=100
16 Correct 24 ms 35692 KB n=100
17 Correct 24 ms 35692 KB n=100
18 Correct 26 ms 35692 KB n=100
19 Correct 28 ms 35692 KB n=100
20 Correct 26 ms 35692 KB n=100
21 Correct 24 ms 35692 KB n=100
22 Correct 25 ms 35692 KB n=100
23 Correct 24 ms 35692 KB n=100
24 Correct 26 ms 35820 KB n=100
25 Correct 24 ms 35692 KB n=100
26 Correct 27 ms 35692 KB n=12
27 Correct 24 ms 35692 KB n=100
28 Correct 25 ms 35820 KB n=500
29 Correct 25 ms 35820 KB n=500
30 Correct 30 ms 35820 KB n=500
31 Correct 25 ms 35820 KB n=500
32 Correct 25 ms 35820 KB n=500
33 Correct 25 ms 35820 KB n=500
34 Correct 26 ms 35820 KB n=500
35 Correct 25 ms 35820 KB n=500
36 Correct 25 ms 35820 KB n=500
37 Correct 25 ms 35820 KB n=500
38 Correct 26 ms 35820 KB n=500
39 Correct 29 ms 35820 KB n=500
40 Correct 25 ms 35820 KB n=500
41 Correct 25 ms 35820 KB n=500
42 Correct 27 ms 35820 KB n=500
43 Correct 26 ms 35820 KB n=500
44 Correct 26 ms 35820 KB n=500
45 Correct 26 ms 35820 KB n=500
46 Correct 28 ms 35820 KB n=500
47 Correct 27 ms 35740 KB n=500
48 Correct 25 ms 35820 KB n=500
49 Correct 25 ms 35820 KB n=500
50 Correct 26 ms 35820 KB n=500
51 Correct 25 ms 35820 KB n=500
52 Correct 25 ms 35820 KB n=500
53 Correct 30 ms 35820 KB n=500
54 Correct 26 ms 35820 KB n=500
55 Correct 25 ms 35820 KB n=278
56 Correct 26 ms 35820 KB n=500
57 Correct 25 ms 35820 KB n=500
58 Correct 25 ms 35820 KB n=500
59 Correct 32 ms 36204 KB n=2000
60 Correct 28 ms 36332 KB n=2000
61 Correct 29 ms 36332 KB n=2000
62 Correct 28 ms 36204 KB n=2000
63 Correct 28 ms 36204 KB n=2000
64 Correct 30 ms 36204 KB n=2000
65 Correct 28 ms 36204 KB n=2000
66 Correct 28 ms 36332 KB n=2000
67 Correct 29 ms 36204 KB n=2000
68 Correct 29 ms 36352 KB n=2000
69 Correct 28 ms 36204 KB n=2000
70 Correct 27 ms 36204 KB n=2000
71 Correct 28 ms 36204 KB n=2000
72 Correct 28 ms 36204 KB n=2000
73 Correct 27 ms 36204 KB n=2000
74 Correct 27 ms 36204 KB n=1844
75 Correct 28 ms 36204 KB n=2000
76 Correct 29 ms 36204 KB n=2000
77 Correct 28 ms 36204 KB n=2000
78 Correct 29 ms 36272 KB n=2000
79 Correct 29 ms 36204 KB n=2000
80 Correct 29 ms 36332 KB n=2000
81 Correct 28 ms 36204 KB n=2000
82 Correct 32 ms 36204 KB n=2000
83 Correct 28 ms 36332 KB n=2000
84 Correct 28 ms 36204 KB n=2000
85 Correct 28 ms 36204 KB n=2000
86 Correct 28 ms 36204 KB n=2000
87 Correct 28 ms 36204 KB n=2000
88 Correct 28 ms 36332 KB n=2000
89 Correct 29 ms 36332 KB n=2000
90 Correct 29 ms 36332 KB n=2000
91 Correct 30 ms 36204 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 27 ms 35692 KB n=5
2 Correct 28 ms 35692 KB n=100
3 Correct 26 ms 35692 KB n=100
4 Correct 29 ms 35692 KB n=100
5 Correct 25 ms 35692 KB n=100
6 Correct 25 ms 35692 KB n=100
7 Correct 25 ms 35692 KB n=100
8 Correct 25 ms 35820 KB n=100
9 Correct 24 ms 35692 KB n=100
10 Correct 30 ms 35692 KB n=100
11 Correct 24 ms 35692 KB n=100
12 Correct 26 ms 35692 KB n=100
13 Correct 30 ms 35692 KB n=100
14 Correct 24 ms 35692 KB n=100
15 Correct 24 ms 35692 KB n=100
16 Correct 24 ms 35692 KB n=100
17 Correct 24 ms 35692 KB n=100
18 Correct 26 ms 35692 KB n=100
19 Correct 28 ms 35692 KB n=100
20 Correct 26 ms 35692 KB n=100
21 Correct 24 ms 35692 KB n=100
22 Correct 25 ms 35692 KB n=100
23 Correct 24 ms 35692 KB n=100
24 Correct 26 ms 35820 KB n=100
25 Correct 24 ms 35692 KB n=100
26 Correct 27 ms 35692 KB n=12
27 Correct 24 ms 35692 KB n=100
28 Correct 25 ms 35820 KB n=500
29 Correct 25 ms 35820 KB n=500
30 Correct 30 ms 35820 KB n=500
31 Correct 25 ms 35820 KB n=500
32 Correct 25 ms 35820 KB n=500
33 Correct 25 ms 35820 KB n=500
34 Correct 26 ms 35820 KB n=500
35 Correct 25 ms 35820 KB n=500
36 Correct 25 ms 35820 KB n=500
37 Correct 25 ms 35820 KB n=500
38 Correct 26 ms 35820 KB n=500
39 Correct 29 ms 35820 KB n=500
40 Correct 25 ms 35820 KB n=500
41 Correct 25 ms 35820 KB n=500
42 Correct 27 ms 35820 KB n=500
43 Correct 26 ms 35820 KB n=500
44 Correct 26 ms 35820 KB n=500
45 Correct 26 ms 35820 KB n=500
46 Correct 28 ms 35820 KB n=500
47 Correct 27 ms 35740 KB n=500
48 Correct 25 ms 35820 KB n=500
49 Correct 25 ms 35820 KB n=500
50 Correct 26 ms 35820 KB n=500
51 Correct 25 ms 35820 KB n=500
52 Correct 25 ms 35820 KB n=500
53 Correct 30 ms 35820 KB n=500
54 Correct 26 ms 35820 KB n=500
55 Correct 25 ms 35820 KB n=278
56 Correct 26 ms 35820 KB n=500
57 Correct 25 ms 35820 KB n=500
58 Correct 25 ms 35820 KB n=500
59 Correct 32 ms 36204 KB n=2000
60 Correct 28 ms 36332 KB n=2000
61 Correct 29 ms 36332 KB n=2000
62 Correct 28 ms 36204 KB n=2000
63 Correct 28 ms 36204 KB n=2000
64 Correct 30 ms 36204 KB n=2000
65 Correct 28 ms 36204 KB n=2000
66 Correct 28 ms 36332 KB n=2000
67 Correct 29 ms 36204 KB n=2000
68 Correct 29 ms 36352 KB n=2000
69 Correct 28 ms 36204 KB n=2000
70 Correct 27 ms 36204 KB n=2000
71 Correct 28 ms 36204 KB n=2000
72 Correct 28 ms 36204 KB n=2000
73 Correct 27 ms 36204 KB n=2000
74 Correct 27 ms 36204 KB n=1844
75 Correct 28 ms 36204 KB n=2000
76 Correct 29 ms 36204 KB n=2000
77 Correct 28 ms 36204 KB n=2000
78 Correct 29 ms 36272 KB n=2000
79 Correct 29 ms 36204 KB n=2000
80 Correct 29 ms 36332 KB n=2000
81 Correct 28 ms 36204 KB n=2000
82 Correct 32 ms 36204 KB n=2000
83 Correct 28 ms 36332 KB n=2000
84 Correct 28 ms 36204 KB n=2000
85 Correct 28 ms 36204 KB n=2000
86 Correct 28 ms 36204 KB n=2000
87 Correct 28 ms 36204 KB n=2000
88 Correct 28 ms 36332 KB n=2000
89 Correct 29 ms 36332 KB n=2000
90 Correct 29 ms 36332 KB n=2000
91 Correct 30 ms 36204 KB n=2000
92 Correct 1119 ms 86368 KB n=200000
93 Correct 1307 ms 93016 KB n=200000
94 Correct 1286 ms 97384 KB n=200000
95 Correct 1102 ms 86172 KB n=200000
96 Correct 1055 ms 86580 KB n=200000
97 Correct 1378 ms 91628 KB n=200000
98 Correct 1076 ms 86232 KB n=200000
99 Correct 1451 ms 86424 KB n=200000
100 Correct 1128 ms 86280 KB n=200000
101 Correct 1257 ms 99224 KB n=200000
102 Correct 595 ms 87532 KB n=200000
103 Correct 586 ms 87532 KB n=200000
104 Correct 574 ms 87404 KB n=200000
105 Correct 587 ms 87788 KB n=200000
106 Correct 589 ms 87916 KB n=200000
107 Correct 595 ms 87836 KB n=200000
108 Correct 1155 ms 86372 KB n=200000
109 Correct 1146 ms 86588 KB n=200000
110 Correct 1149 ms 86508 KB n=200000
111 Correct 992 ms 85988 KB n=200000
112 Correct 1182 ms 97900 KB n=200000
113 Correct 1279 ms 91728 KB n=200000
114 Correct 1002 ms 86008 KB n=200000
115 Correct 1499 ms 89056 KB n=200000
116 Correct 1032 ms 86752 KB n=200000
117 Correct 1167 ms 98284 KB n=200000
118 Correct 1293 ms 90348 KB n=200000
119 Correct 1014 ms 86444 KB n=200000
120 Correct 1106 ms 98412 KB n=200000
121 Correct 1108 ms 98284 KB n=200000
122 Correct 1136 ms 98524 KB n=200000
123 Correct 650 ms 87660 KB n=200000
124 Correct 258 ms 50988 KB n=25264