답안 #637949

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
637949 2022-09-03T16:57:55 Z stevancv Birthday gift (IZhO18_treearray) C++14
56 / 100
675 ms 68108 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int mod = 1e9 + 7;
vector<int> g[N];
int nxt[N][18], in[N], out[N], tsz;
void Dfs(int s, int e) {
    in[s] = ++tsz;
    nxt[s][0] = e;
    for (int i = 1; i < 18; i++) nxt[s][i] = nxt[nxt[s][i - 1]][i - 1];
    for (int u : g[s]) {
        if (u != e) Dfs(u, s);
    }
    out[s] = tsz;
}
bool Ancestor(int u, int v) {
    return in[u] <= in[v] && out[v] <= out[u];
}
int Lca(int u, int v) {
    if (Ancestor(u, v)) return u;
    if (Ancestor(v, u)) return v;
    for (int i = 17; i >= 0; i--) {
        if (nxt[u][i] != -1 && !Ancestor(nxt[u][i], v)) u = nxt[u][i];
    }
    return nxt[u][0];
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        u -= 1; v -= 1;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    Dfs(0, -1);
    vector<int> a(m), b(m - 1);
    for (int i = 0; i < m; i++) {
        cin >> a[i];
        a[i] -= 1;
    }
    for (int i = 0; i < m - 1; i++) b[i] = Lca(a[i], a[i + 1]);
    for (int i = 0; i < m - 1; i++) assert(0 <= b[i] && b[i] < n);
    vector<set<int>> sa(n), sb(n);
    for (int i = 0; i < m; i++) sa[a[i]].insert(i);
    for (int i = 0; i < m - 1; i++) sb[b[i]].insert(i);
    while (q--) {
        int ty; cin >> ty;
        if (ty == 1) {
            int p, v; cin >> p >> v;
            p -= 1; v -= 1;
            sa[a[p]].erase(p);
            if (p > 0) sb[b[p - 1]].erase(p - 1);
            if (p < m - 1) sb[b[p]].erase(p);
            a[p] = v;
            sa[a[p]].insert(p);
            if (p > 0) {
                b[p - 1] = Lca(a[p - 1], a[p]);
                sb[b[p - 1]].insert(p - 1);
            }
            if (p < m - 1) {
                b[p] = Lca(a[p], a[p + 1]);
                sb[b[p]].insert(p);
            }
            continue;
        }
        int l, r, p; cin >> l >> r >> p;
        l -= 1; r -= 1; p -= 1;
        auto it = sa[p].lower_bound(l);
        if (it != sa[p].end() && (*it) <= r) {
            int x = *it + 1;
            cout << x << sp << x << en;
            continue;
        }
        it = sb[p].lower_bound(l);
        if (it != sb[p].end() && (*it) < r) {
            int x = *it + 1;
            cout << x << sp << x + 1 << en;
            continue;
        }
        cout << -1 << sp << -1 << en;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB n=5
2 Correct 2 ms 4948 KB n=100
3 Correct 2 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 4 ms 4948 KB n=100
6 Correct 2 ms 4948 KB n=100
7 Correct 2 ms 4948 KB n=100
8 Correct 3 ms 4948 KB n=100
9 Correct 2 ms 4948 KB n=100
10 Correct 2 ms 4948 KB n=100
11 Correct 2 ms 4948 KB n=100
12 Correct 2 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 2 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 2 ms 4948 KB n=100
17 Correct 2 ms 4948 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 2 ms 4948 KB n=100
23 Correct 2 ms 4948 KB n=100
24 Correct 4 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 2 ms 4948 KB n=12
27 Correct 4 ms 4948 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB n=5
2 Correct 2 ms 4948 KB n=100
3 Correct 2 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 4 ms 4948 KB n=100
6 Correct 2 ms 4948 KB n=100
7 Correct 2 ms 4948 KB n=100
8 Correct 3 ms 4948 KB n=100
9 Correct 2 ms 4948 KB n=100
10 Correct 2 ms 4948 KB n=100
11 Correct 2 ms 4948 KB n=100
12 Correct 2 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 2 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 2 ms 4948 KB n=100
17 Correct 2 ms 4948 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 2 ms 4948 KB n=100
23 Correct 2 ms 4948 KB n=100
24 Correct 4 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 2 ms 4948 KB n=12
27 Correct 4 ms 4948 KB n=100
28 Correct 3 ms 5076 KB n=500
29 Correct 3 ms 5204 KB n=500
30 Correct 4 ms 5076 KB n=500
31 Correct 4 ms 5076 KB n=500
32 Correct 4 ms 5076 KB n=500
33 Correct 4 ms 5076 KB n=500
34 Correct 3 ms 5076 KB n=500
35 Correct 3 ms 5076 KB n=500
36 Correct 3 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5076 KB n=500
39 Correct 3 ms 5076 KB n=500
40 Correct 3 ms 5076 KB n=500
41 Correct 3 ms 5076 KB n=500
42 Correct 3 ms 5076 KB n=500
43 Correct 4 ms 5140 KB n=500
44 Correct 3 ms 5076 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 4 ms 5204 KB n=500
47 Correct 3 ms 5076 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 3 ms 5076 KB n=500
50 Correct 3 ms 5076 KB n=500
51 Correct 3 ms 5076 KB n=500
52 Correct 3 ms 5076 KB n=500
53 Correct 3 ms 5076 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 3 ms 5076 KB n=500
57 Correct 3 ms 5204 KB n=500
58 Correct 3 ms 5076 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB n=5
2 Correct 2 ms 4948 KB n=100
3 Correct 2 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 4 ms 4948 KB n=100
6 Correct 2 ms 4948 KB n=100
7 Correct 2 ms 4948 KB n=100
8 Correct 3 ms 4948 KB n=100
9 Correct 2 ms 4948 KB n=100
10 Correct 2 ms 4948 KB n=100
11 Correct 2 ms 4948 KB n=100
12 Correct 2 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 2 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 2 ms 4948 KB n=100
17 Correct 2 ms 4948 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 2 ms 4948 KB n=100
23 Correct 2 ms 4948 KB n=100
24 Correct 4 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 2 ms 4948 KB n=12
27 Correct 4 ms 4948 KB n=100
28 Correct 3 ms 5076 KB n=500
29 Correct 3 ms 5204 KB n=500
30 Correct 4 ms 5076 KB n=500
31 Correct 4 ms 5076 KB n=500
32 Correct 4 ms 5076 KB n=500
33 Correct 4 ms 5076 KB n=500
34 Correct 3 ms 5076 KB n=500
35 Correct 3 ms 5076 KB n=500
36 Correct 3 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5076 KB n=500
39 Correct 3 ms 5076 KB n=500
40 Correct 3 ms 5076 KB n=500
41 Correct 3 ms 5076 KB n=500
42 Correct 3 ms 5076 KB n=500
43 Correct 4 ms 5140 KB n=500
44 Correct 3 ms 5076 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 4 ms 5204 KB n=500
47 Correct 3 ms 5076 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 3 ms 5076 KB n=500
50 Correct 3 ms 5076 KB n=500
51 Correct 3 ms 5076 KB n=500
52 Correct 3 ms 5076 KB n=500
53 Correct 3 ms 5076 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 3 ms 5076 KB n=500
57 Correct 3 ms 5204 KB n=500
58 Correct 3 ms 5076 KB n=500
59 Correct 5 ms 5588 KB n=2000
60 Correct 5 ms 5716 KB n=2000
61 Correct 5 ms 5648 KB n=2000
62 Correct 5 ms 5588 KB n=2000
63 Correct 5 ms 5588 KB n=2000
64 Correct 8 ms 5588 KB n=2000
65 Correct 5 ms 5660 KB n=2000
66 Correct 5 ms 5716 KB n=2000
67 Correct 8 ms 5588 KB n=2000
68 Correct 6 ms 5588 KB n=2000
69 Correct 5 ms 5588 KB n=2000
70 Correct 5 ms 5588 KB n=2000
71 Correct 5 ms 5588 KB n=2000
72 Correct 5 ms 5588 KB n=2000
73 Correct 4 ms 5588 KB n=2000
74 Correct 5 ms 5588 KB n=1844
75 Correct 7 ms 5588 KB n=2000
76 Correct 6 ms 5656 KB n=2000
77 Correct 5 ms 5588 KB n=2000
78 Correct 9 ms 5588 KB n=2000
79 Correct 9 ms 5588 KB n=2000
80 Correct 5 ms 5716 KB n=2000
81 Correct 5 ms 5676 KB n=2000
82 Correct 8 ms 5588 KB n=2000
83 Correct 5 ms 5588 KB n=2000
84 Correct 6 ms 5588 KB n=2000
85 Correct 8 ms 5588 KB n=2000
86 Correct 8 ms 5588 KB n=2000
87 Correct 6 ms 5656 KB n=2000
88 Correct 7 ms 5716 KB n=2000
89 Correct 5 ms 5716 KB n=2000
90 Correct 5 ms 5716 KB n=2000
91 Correct 8 ms 5588 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB n=5
2 Correct 2 ms 4948 KB n=100
3 Correct 2 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 4 ms 4948 KB n=100
6 Correct 2 ms 4948 KB n=100
7 Correct 2 ms 4948 KB n=100
8 Correct 3 ms 4948 KB n=100
9 Correct 2 ms 4948 KB n=100
10 Correct 2 ms 4948 KB n=100
11 Correct 2 ms 4948 KB n=100
12 Correct 2 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 2 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 2 ms 4948 KB n=100
17 Correct 2 ms 4948 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 2 ms 4948 KB n=100
23 Correct 2 ms 4948 KB n=100
24 Correct 4 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 2 ms 4948 KB n=12
27 Correct 4 ms 4948 KB n=100
28 Correct 3 ms 5076 KB n=500
29 Correct 3 ms 5204 KB n=500
30 Correct 4 ms 5076 KB n=500
31 Correct 4 ms 5076 KB n=500
32 Correct 4 ms 5076 KB n=500
33 Correct 4 ms 5076 KB n=500
34 Correct 3 ms 5076 KB n=500
35 Correct 3 ms 5076 KB n=500
36 Correct 3 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5076 KB n=500
39 Correct 3 ms 5076 KB n=500
40 Correct 3 ms 5076 KB n=500
41 Correct 3 ms 5076 KB n=500
42 Correct 3 ms 5076 KB n=500
43 Correct 4 ms 5140 KB n=500
44 Correct 3 ms 5076 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 4 ms 5204 KB n=500
47 Correct 3 ms 5076 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 3 ms 5076 KB n=500
50 Correct 3 ms 5076 KB n=500
51 Correct 3 ms 5076 KB n=500
52 Correct 3 ms 5076 KB n=500
53 Correct 3 ms 5076 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 3 ms 5076 KB n=500
57 Correct 3 ms 5204 KB n=500
58 Correct 3 ms 5076 KB n=500
59 Correct 5 ms 5588 KB n=2000
60 Correct 5 ms 5716 KB n=2000
61 Correct 5 ms 5648 KB n=2000
62 Correct 5 ms 5588 KB n=2000
63 Correct 5 ms 5588 KB n=2000
64 Correct 8 ms 5588 KB n=2000
65 Correct 5 ms 5660 KB n=2000
66 Correct 5 ms 5716 KB n=2000
67 Correct 8 ms 5588 KB n=2000
68 Correct 6 ms 5588 KB n=2000
69 Correct 5 ms 5588 KB n=2000
70 Correct 5 ms 5588 KB n=2000
71 Correct 5 ms 5588 KB n=2000
72 Correct 5 ms 5588 KB n=2000
73 Correct 4 ms 5588 KB n=2000
74 Correct 5 ms 5588 KB n=1844
75 Correct 7 ms 5588 KB n=2000
76 Correct 6 ms 5656 KB n=2000
77 Correct 5 ms 5588 KB n=2000
78 Correct 9 ms 5588 KB n=2000
79 Correct 9 ms 5588 KB n=2000
80 Correct 5 ms 5716 KB n=2000
81 Correct 5 ms 5676 KB n=2000
82 Correct 8 ms 5588 KB n=2000
83 Correct 5 ms 5588 KB n=2000
84 Correct 6 ms 5588 KB n=2000
85 Correct 8 ms 5588 KB n=2000
86 Correct 8 ms 5588 KB n=2000
87 Correct 6 ms 5656 KB n=2000
88 Correct 7 ms 5716 KB n=2000
89 Correct 5 ms 5716 KB n=2000
90 Correct 5 ms 5716 KB n=2000
91 Correct 8 ms 5588 KB n=2000
92 Incorrect 675 ms 68108 KB Wrong range.
93 Halted 0 ms 0 KB -