답안 #586113

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
586113 2022-06-29T22:52:06 Z KiriLL1ca Birthday gift (IZhO18_treearray) C++14
100 / 100
943 ms 80444 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fr first
#define sc second
#define all(x) (x).begin(), (x).end()
#define pw(x) (1ll << x)
#define sz(x) (int)((x).size())
#define pb push_back
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;


typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

template <typename T> inline bool umin (T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template <typename T> inline bool umax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T>
using oset = tree<T, null_type, less_equal <T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 2e5 + 10;
vector <int> g[N];
int up[N][22], tin[N], tout[N], timer = 0;
inline void dfs (int v, int p) {
        up[v][0] = p; tin[v] = timer++;
        for (int i = 1; i < 20; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
        for (auto u : g[v]) {
                if (u == p) continue;
                dfs(u, v);
        }
        tout[v] = timer++;
}
inline bool upper (int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; }
inline int lca (int a, int b) {
        if (upper(a, b)) return a;
        if (upper(b, a)) return b;
        for (int i = 19; ~i; --i) if (!upper(up[a][i], b)) a = up[a][i];
        return up[a][0];
}

inline void solve () {
        int n, m, q; cin >> n >> m >> q;
        for (int i = 1; i < n; ++i) {
                int u, v; cin >> u >> v; --u; --v;
                g[u].pb(v); g[v].pb(u);
        }
        dfs(0, 0);

        vector <int> a (m);
        for (auto &i : a) cin >> i, --i;
        vector <set <int>> ps (n), pss (n);
        for (int i = 0; i < m; ++i) {
                ps[a[i]].insert(i);
                if (i) pss[lca(a[i - 1], a[i])].insert(i);
        }

        while (q--) {
                int tp; cin >> tp;
                if (tp == 1) {
                        int pos, v; cin >> pos >> v; --pos, --v;
                        ps[a[pos]].erase(pos);
                        if (pos) pss[lca(a[pos - 1], a[pos])].erase(pos);
                        if (pos + 1 < m) pss[lca(a[pos], a[pos + 1])].erase(pos + 1);

                        a[pos] = v;
                        ps[a[pos]].insert(pos);
                        if (pos) pss[lca(a[pos - 1], a[pos])].insert(pos);
                        if (pos + 1 < m) pss[lca(a[pos], a[pos + 1])].insert(pos + 1);
                }
                else {
                        int l, r, v; cin >> l >> r >> v; --l, --r, --v;
                        auto it = ps[v].lower_bound(l);
                        if (it != ps[v].end() && *it <= r) cout << *it + 1 << " " << *it + 1 << endl;
                        else {
                                auto itt = pss[v].lower_bound(l + 1);
                                if (itt != pss[v].end() && *itt <= r) cout << *itt << " " << *itt + 1 << endl;
                                else cout << -1 << " " << -1 << endl;
                        }
                }
        }
}

/// [a [b c]]
/// [[a b] c]

signed main() {
        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        int t = 1; // cin >> t;
        while (t--) solve();
        return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 5024 KB n=100
5 Correct 3 ms 5028 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 5020 KB n=100
8 Correct 2 ms 4948 KB n=100
9 Correct 4 ms 5024 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 5024 KB n=100
14 Correct 2 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 4948 KB n=100
17 Correct 3 ms 5016 KB n=100
18 Correct 3 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 5020 KB n=100
23 Correct 3 ms 4948 KB n=100
24 Correct 2 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 2 ms 4948 KB n=12
27 Correct 3 ms 5020 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 5024 KB n=100
5 Correct 3 ms 5028 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 5020 KB n=100
8 Correct 2 ms 4948 KB n=100
9 Correct 4 ms 5024 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 5024 KB n=100
14 Correct 2 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 4948 KB n=100
17 Correct 3 ms 5016 KB n=100
18 Correct 3 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 5020 KB n=100
23 Correct 3 ms 4948 KB n=100
24 Correct 2 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 2 ms 4948 KB n=12
27 Correct 3 ms 5020 KB n=100
28 Correct 3 ms 5204 KB n=500
29 Correct 3 ms 5204 KB n=500
30 Correct 3 ms 5204 KB n=500
31 Correct 4 ms 5168 KB n=500
32 Correct 3 ms 5204 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 4 ms 5208 KB n=500
35 Correct 3 ms 5164 KB n=500
36 Correct 3 ms 5204 KB n=500
37 Correct 3 ms 5204 KB n=500
38 Correct 3 ms 5204 KB n=500
39 Correct 3 ms 5204 KB n=500
40 Correct 3 ms 5160 KB n=500
41 Correct 3 ms 5204 KB n=500
42 Correct 4 ms 5076 KB n=500
43 Correct 3 ms 5160 KB n=500
44 Correct 4 ms 5164 KB n=500
45 Correct 4 ms 5204 KB n=500
46 Correct 3 ms 5204 KB n=500
47 Correct 3 ms 5204 KB n=500
48 Correct 4 ms 5160 KB n=500
49 Correct 3 ms 5204 KB n=500
50 Correct 4 ms 5160 KB n=500
51 Correct 3 ms 5168 KB n=500
52 Correct 3 ms 5124 KB n=500
53 Correct 3 ms 5172 KB n=500
54 Correct 3 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 3 ms 5204 KB n=500
57 Correct 3 ms 5160 KB n=500
58 Correct 3 ms 5204 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 5024 KB n=100
5 Correct 3 ms 5028 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 5020 KB n=100
8 Correct 2 ms 4948 KB n=100
9 Correct 4 ms 5024 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 5024 KB n=100
14 Correct 2 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 4948 KB n=100
17 Correct 3 ms 5016 KB n=100
18 Correct 3 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 5020 KB n=100
23 Correct 3 ms 4948 KB n=100
24 Correct 2 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 2 ms 4948 KB n=12
27 Correct 3 ms 5020 KB n=100
28 Correct 3 ms 5204 KB n=500
29 Correct 3 ms 5204 KB n=500
30 Correct 3 ms 5204 KB n=500
31 Correct 4 ms 5168 KB n=500
32 Correct 3 ms 5204 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 4 ms 5208 KB n=500
35 Correct 3 ms 5164 KB n=500
36 Correct 3 ms 5204 KB n=500
37 Correct 3 ms 5204 KB n=500
38 Correct 3 ms 5204 KB n=500
39 Correct 3 ms 5204 KB n=500
40 Correct 3 ms 5160 KB n=500
41 Correct 3 ms 5204 KB n=500
42 Correct 4 ms 5076 KB n=500
43 Correct 3 ms 5160 KB n=500
44 Correct 4 ms 5164 KB n=500
45 Correct 4 ms 5204 KB n=500
46 Correct 3 ms 5204 KB n=500
47 Correct 3 ms 5204 KB n=500
48 Correct 4 ms 5160 KB n=500
49 Correct 3 ms 5204 KB n=500
50 Correct 4 ms 5160 KB n=500
51 Correct 3 ms 5168 KB n=500
52 Correct 3 ms 5124 KB n=500
53 Correct 3 ms 5172 KB n=500
54 Correct 3 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 3 ms 5204 KB n=500
57 Correct 3 ms 5160 KB n=500
58 Correct 3 ms 5204 KB n=500
59 Correct 6 ms 5716 KB n=2000
60 Correct 5 ms 5716 KB n=2000
61 Correct 6 ms 5676 KB n=2000
62 Correct 6 ms 5716 KB n=2000
63 Correct 6 ms 5716 KB n=2000
64 Correct 6 ms 5716 KB n=2000
65 Correct 6 ms 5716 KB n=2000
66 Correct 6 ms 5716 KB n=2000
67 Correct 7 ms 5628 KB n=2000
68 Correct 6 ms 5716 KB n=2000
69 Correct 5 ms 5624 KB n=2000
70 Correct 5 ms 5716 KB n=2000
71 Correct 5 ms 5716 KB n=2000
72 Correct 5 ms 5716 KB n=2000
73 Correct 5 ms 5676 KB n=2000
74 Correct 6 ms 5588 KB n=1844
75 Correct 5 ms 5672 KB n=2000
76 Correct 6 ms 5716 KB n=2000
77 Correct 6 ms 5716 KB n=2000
78 Correct 6 ms 5680 KB n=2000
79 Correct 8 ms 5728 KB n=2000
80 Correct 5 ms 5684 KB n=2000
81 Correct 5 ms 5716 KB n=2000
82 Correct 7 ms 5624 KB n=2000
83 Correct 5 ms 5716 KB n=2000
84 Correct 6 ms 5716 KB n=2000
85 Correct 6 ms 5716 KB n=2000
86 Correct 6 ms 5676 KB n=2000
87 Correct 8 ms 5628 KB n=2000
88 Correct 5 ms 5716 KB n=2000
89 Correct 6 ms 5716 KB n=2000
90 Correct 5 ms 5676 KB n=2000
91 Correct 5 ms 5716 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 5024 KB n=100
5 Correct 3 ms 5028 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 5020 KB n=100
8 Correct 2 ms 4948 KB n=100
9 Correct 4 ms 5024 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 5024 KB n=100
14 Correct 2 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 4948 KB n=100
17 Correct 3 ms 5016 KB n=100
18 Correct 3 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 5020 KB n=100
23 Correct 3 ms 4948 KB n=100
24 Correct 2 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 2 ms 4948 KB n=12
27 Correct 3 ms 5020 KB n=100
28 Correct 3 ms 5204 KB n=500
29 Correct 3 ms 5204 KB n=500
30 Correct 3 ms 5204 KB n=500
31 Correct 4 ms 5168 KB n=500
32 Correct 3 ms 5204 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 4 ms 5208 KB n=500
35 Correct 3 ms 5164 KB n=500
36 Correct 3 ms 5204 KB n=500
37 Correct 3 ms 5204 KB n=500
38 Correct 3 ms 5204 KB n=500
39 Correct 3 ms 5204 KB n=500
40 Correct 3 ms 5160 KB n=500
41 Correct 3 ms 5204 KB n=500
42 Correct 4 ms 5076 KB n=500
43 Correct 3 ms 5160 KB n=500
44 Correct 4 ms 5164 KB n=500
45 Correct 4 ms 5204 KB n=500
46 Correct 3 ms 5204 KB n=500
47 Correct 3 ms 5204 KB n=500
48 Correct 4 ms 5160 KB n=500
49 Correct 3 ms 5204 KB n=500
50 Correct 4 ms 5160 KB n=500
51 Correct 3 ms 5168 KB n=500
52 Correct 3 ms 5124 KB n=500
53 Correct 3 ms 5172 KB n=500
54 Correct 3 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 3 ms 5204 KB n=500
57 Correct 3 ms 5160 KB n=500
58 Correct 3 ms 5204 KB n=500
59 Correct 6 ms 5716 KB n=2000
60 Correct 5 ms 5716 KB n=2000
61 Correct 6 ms 5676 KB n=2000
62 Correct 6 ms 5716 KB n=2000
63 Correct 6 ms 5716 KB n=2000
64 Correct 6 ms 5716 KB n=2000
65 Correct 6 ms 5716 KB n=2000
66 Correct 6 ms 5716 KB n=2000
67 Correct 7 ms 5628 KB n=2000
68 Correct 6 ms 5716 KB n=2000
69 Correct 5 ms 5624 KB n=2000
70 Correct 5 ms 5716 KB n=2000
71 Correct 5 ms 5716 KB n=2000
72 Correct 5 ms 5716 KB n=2000
73 Correct 5 ms 5676 KB n=2000
74 Correct 6 ms 5588 KB n=1844
75 Correct 5 ms 5672 KB n=2000
76 Correct 6 ms 5716 KB n=2000
77 Correct 6 ms 5716 KB n=2000
78 Correct 6 ms 5680 KB n=2000
79 Correct 8 ms 5728 KB n=2000
80 Correct 5 ms 5684 KB n=2000
81 Correct 5 ms 5716 KB n=2000
82 Correct 7 ms 5624 KB n=2000
83 Correct 5 ms 5716 KB n=2000
84 Correct 6 ms 5716 KB n=2000
85 Correct 6 ms 5716 KB n=2000
86 Correct 6 ms 5676 KB n=2000
87 Correct 8 ms 5628 KB n=2000
88 Correct 5 ms 5716 KB n=2000
89 Correct 6 ms 5716 KB n=2000
90 Correct 5 ms 5676 KB n=2000
91 Correct 5 ms 5716 KB n=2000
92 Correct 675 ms 77012 KB n=200000
93 Correct 742 ms 77032 KB n=200000
94 Correct 535 ms 79660 KB n=200000
95 Correct 672 ms 74720 KB n=200000
96 Correct 638 ms 74956 KB n=200000
97 Correct 794 ms 76408 KB n=200000
98 Correct 673 ms 74700 KB n=200000
99 Correct 782 ms 74056 KB n=200000
100 Correct 661 ms 74596 KB n=200000
101 Correct 475 ms 80444 KB n=200000
102 Correct 401 ms 75456 KB n=200000
103 Correct 411 ms 75632 KB n=200000
104 Correct 404 ms 75488 KB n=200000
105 Correct 402 ms 75248 KB n=200000
106 Correct 412 ms 75288 KB n=200000
107 Correct 409 ms 75212 KB n=200000
108 Correct 697 ms 74432 KB n=200000
109 Correct 736 ms 74188 KB n=200000
110 Correct 709 ms 74176 KB n=200000
111 Correct 649 ms 74432 KB n=200000
112 Correct 506 ms 79656 KB n=200000
113 Correct 766 ms 76452 KB n=200000
114 Correct 644 ms 74512 KB n=200000
115 Correct 943 ms 74868 KB n=200000
116 Correct 642 ms 74144 KB n=200000
117 Correct 476 ms 79424 KB n=200000
118 Correct 863 ms 75304 KB n=200000
119 Correct 647 ms 74120 KB n=200000
120 Correct 425 ms 79712 KB n=200000
121 Correct 427 ms 79596 KB n=200000
122 Correct 444 ms 80036 KB n=200000
123 Correct 438 ms 74676 KB n=200000
124 Correct 136 ms 22220 KB n=25264